What's the best way to add new attributes to MarkerOptions in Mapbox [Android]?

匆匆过客 提交于 2020-01-02 22:00:08

问题


I am trying to add new attributes to a marker in Mapbox, extending the classes is getting a little messy.

What is the best way to add new attributes to a marker?


回答1:


Starting with 4.1.0 we introduced marker views. These extend the Android View class and allow you to create your own marker adapter. I've created a good example doing this you can find in the demo app. I'll post some of the snippets of code here but it might be easier just looking at the source code on Github.

Adapter:

// 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;
}
}

PulseMarkerView:

public class PulseMarkerView extends MarkerView {

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

and lastly, 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];
  }
};
}

Essentially this exposes the markerViews background so that we can pulse (animate) it but you can setup your own attributes. More examples of this being done can be found in the testapp. Hope this is what you were looking for.



来源:https://stackoverflow.com/questions/39275733/whats-the-best-way-to-add-new-attributes-to-markeroptions-in-mapbox-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!