2016-09-01 10 views
1

私はMapboxのマーカーに新しい属性を追加しようとしています。クラスを拡張するのはちょっと面倒です。Mapbox [Android]のMarkerOptionsに新しい属性を追加する最も良い方法は何ですか?

マーカーに新しい属性を追加する最も良い方法は何ですか?

+0

どのような属性を追加しますか? –

+0

私は追加したいカスタムクラスを持っています。それはenumと文字列を含んでいます –

答えて

3

から始めて、マーカービューを導入しました。これらはAndroid Viewクラスを拡張し、独自のマーカーアダプタを作成することを可能にします。デモアプリケーションで見つけられるa good exampleを作成しました。ここでコードの一部を投稿しますが、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; 
} 
} 

PulseMarkerView:

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にあります。これがあなたが探していたものだと願っています。

+0

申し訳ありませんが、これは素晴らしい解決策です、問題はパフォーマンスです。これは非常に恐ろしいことです。私のアプリは、指定された時間に100以上のピンを表示することができます。現在、単一都市では100ピンを示しています。それは非常に遅いです。 MarkerOptionだけでこれを実装することができました。しかし、私は非常に私のアプリ(これを使用してエレガントに行うことができます)で非常に重要ないくつかのmetadtaを追加する機能が欠けています。私はMarkerOptionsで同じことをしたかったのです。これがMarkerOptionsでも可能であるかどうかはどうですか? –

+1

MarkerOptionでメタデータを保存する場合は、オブジェクトを拡張して独自の保存方法を追加するか、[Map <>](https://developer.android.com)を使用する必要があります/reference/java/util/Map.html)。 – cammace

+0

うん、ありがとう!私はMarkerOptionのために同じ手順を続けました。それは他の何よりも速いです。 MarkerViewOptionsはプロジェクトでは使用できません。 –

関連する問題