gpt4 book ai didi

android - 在 Mapbox [Android] 中向 MarkerOptions 添加新属性的最佳方式是什么?

转载 作者:行者123 更新时间:2023-11-29 17:15:21 24 4
gpt4 key购买 nike

我正在尝试向 Mapbox 中的标记添加新属性,扩展类变得有点困惑。

向标记添加新属性的最佳方法是什么?

最佳答案

4.1.0 开头我们引入了标记 View 。它们扩展了 Android View 类并允许您创建自己的标记适配器。我创建了 a good example这样做你可以在演示应用程序中找到。我将在此处发布一些代码 fragment ,但查看 Github 上的源代码可能更容易。

适配器:

// Custom marker view used for pulsing the background view of marker.
private static class PulseMarkerViewAdapter extends MapboxMap.MarkerViewAdapter<PulseMarkerView> {

private LayoutInflater inflater;

public PulseMarkerViewAdapter(@NonNull Context context) {
super(context);
this.inflater = LayoutInflater.from(context);
}

@Nullable
@Override
public View getView(@NonNull PulseMarkerView marker, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.view_pulse_marker, parent, false);
viewHolder.foregroundImageView = (ImageView) convertView.findViewById(R.id.foreground_imageView);
viewHolder.backgroundImageView = (ImageView) convertView.findViewById(R.id.background_imageview);
convertView.setTag(viewHolder);
}
return convertView;
}

private static class ViewHolder {
ImageView foregroundImageView;
ImageView backgroundImageView;
}
}

脉冲标记 View :

public class PulseMarkerView extends MarkerView {

public PulseMarkerView(BaseMarkerViewOptions baseMarkerViewOptions) {
super(baseMarkerViewOptions);
}
}

最后,PulseMarkerOptions:

public class PulseMarkerViewOptions extends BaseMarkerViewOptions<PulseMarkerView, PulseMarkerViewOptions> {

public PulseMarkerViewOptions() {
}

protected PulseMarkerViewOptions(Parcel in) {
position((LatLng) in.readParcelable(LatLng.class.getClassLoader()));
snippet(in.readString());
title(in.readString());
flat(in.readByte() != 0);
anchor(in.readFloat(), in.readFloat());
selected = in.readByte() != 0;
rotation(in.readFloat());
if (in.readByte() != 0) {
// this means we have an icon
String iconId = in.readString();
Bitmap iconBitmap = in.readParcelable(Bitmap.class.getClassLoader());
Icon icon = IconFactory.recreate(iconId, iconBitmap);
icon(icon);
}
}

@Override
public PulseMarkerViewOptions getThis() {
return this;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel out, int flags) {
out.writeParcelable(getPosition(), flags);
out.writeString(getSnippet());
out.writeString(getTitle());
out.writeByte((byte) (isFlat() ? 1 : 0));
out.writeFloat(getAnchorU());
out.writeFloat(getAnchorV());
out.writeFloat(getInfoWindowAnchorU());
out.writeFloat(getInfoWindowAnchorV());
out.writeByte((byte) (selected ? 1 : 0));
out.writeFloat(getRotation());
Icon icon = getIcon();
out.writeByte((byte) (icon != null ? 1 : 0));
if (icon != null) {
out.writeString(getIcon().getId());
out.writeParcelable(getIcon().getBitmap(), flags);
}
}

@Override
public PulseMarkerView getMarker() {
return new PulseMarkerView(this);
}

public static final Parcelable.Creator<PulseMarkerViewOptions> CREATOR =
new Parcelable.Creator<PulseMarkerViewOptions>() {
public PulseMarkerViewOptions createFromParcel(Parcel in) {
return new PulseMarkerViewOptions(in);
}

public PulseMarkerViewOptions[] newArray(int size) {
return new PulseMarkerViewOptions[size];
}
};
}

本质上,这会暴露 markerViews 背景,以便我们可以对其进行脉动(动画),但您可以设置自己的属性。更多关于这样做的例子可以在 testapp 中找到。 .希望这就是您要找的。

关于android - 在 Mapbox [Android] 中向 MarkerOptions 添加新属性的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39275733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com