[android] #3276 - android view annotations adapter approach
This commit is contained in:
@@ -15,6 +15,7 @@ import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Spinner;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
@@ -121,7 +122,7 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
|
||||
}
|
||||
}
|
||||
|
||||
private static class LoadBulkMarkerTask extends AsyncTask<Void, Integer, List<MarkerOptions>> {
|
||||
private static class LoadBulkMarkerTask extends AsyncTask<Void, Integer, List<BaseMarkerOptions>> {
|
||||
|
||||
private static final String TAG = "LoadBulkMarkerTask";
|
||||
private MapboxMap mMapboxMap;
|
||||
@@ -138,8 +139,8 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<MarkerOptions> doInBackground(Void... params) {
|
||||
List<MarkerOptions> markerOptions = new ArrayList<>(mAmount);
|
||||
protected List<BaseMarkerOptions> doInBackground(Void... params) {
|
||||
List<BaseMarkerOptions> markerOptions = new ArrayList<>(mAmount);
|
||||
try {
|
||||
DecimalFormat formatter = new DecimalFormat("#.#####");
|
||||
String json = GeoParseUtil.loadStringFromAssets(mAppContext, "points.geojson");
|
||||
@@ -170,7 +171,7 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<MarkerOptions> markerOptions) {
|
||||
protected void onPostExecute(List<BaseMarkerOptions> markerOptions) {
|
||||
super.onPostExecute(markerOptions);
|
||||
mMapboxMap.addMarkers(markerOptions);
|
||||
mProgressDialog.hide();
|
||||
|
||||
@@ -1,29 +1,34 @@
|
||||
package com.mapbox.mapboxsdk.testapp.activity.annotation;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerView;
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
import com.mapbox.mapboxsdk.testapp.view.CustomMarkerView;
|
||||
import com.mapbox.mapboxsdk.testapp.model.annotations.CountryMarker;
|
||||
import com.mapbox.mapboxsdk.testapp.model.annotations.CountryMarkerOptions;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ViewMarkerActivity extends AppCompatActivity implements OnMapReadyCallback {
|
||||
|
||||
private MapView mMapView;
|
||||
private Marker brazil, germany, china, us;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -46,37 +51,46 @@ public class ViewMarkerActivity extends AppCompatActivity implements OnMapReadyC
|
||||
|
||||
@Override
|
||||
public void onMapReady(MapboxMap mapboxMap) {
|
||||
List<BaseMarkerOptions> countries = new ArrayList<>();
|
||||
countries.add(new CountryMarkerOptions().abbrevName("ch").flagRes(R.drawable.ic_china).position(new LatLng(31.230416, 121.473701)));
|
||||
countries.add(new CountryMarkerOptions().abbrevName("us").flagRes(R.drawable.ic_us).position(new LatLng(38.907192, -77.036871)));
|
||||
countries.add(new CountryMarkerOptions().abbrevName("br").flagRes(R.drawable.ic_brazil).position(new LatLng(-15.798200, -47.922363)));
|
||||
countries.add(new CountryMarkerOptions().abbrevName("de").flagRes(R.drawable.ic_germany).position(new LatLng(52.520007, 13.404954)));
|
||||
mapboxMap.addMarkers(countries);
|
||||
|
||||
// adapt GL-marker to View-marker
|
||||
mapboxMap.setMarkerViewAdapter(new MapboxMap.MarkerViewAdapter() {
|
||||
@Nullable
|
||||
@Override
|
||||
public MarkerView getView(@NonNull Marker marker) {
|
||||
if (marker.equals(brazil)) {
|
||||
return createMarkerView("br", R.drawable.ic_brazil);
|
||||
} else if (marker.equals(germany)) {
|
||||
return createMarkerView("de", R.drawable.ic_germany);
|
||||
} else if (marker.equals(china)) {
|
||||
return createMarkerView("ch", R.drawable.ic_china);
|
||||
} else if (marker.equals(us)) {
|
||||
return createMarkerView("us", R.drawable.ic_us);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
// add markers
|
||||
china = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(31.230416, 121.473701)));
|
||||
brazil = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(-15.798200, -47.922363)));
|
||||
us = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(38.907192, -77.036871)));
|
||||
germany = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(52.520007, 13.404954)));
|
||||
mapboxMap.setMarkerViewAdapter(new CountryAdapter(this));
|
||||
}
|
||||
|
||||
private MarkerView createMarkerView(String countryAbbrev, @DrawableRes int countryIconRes) {
|
||||
CustomMarkerView customMarkerView = new CustomMarkerView(this);
|
||||
customMarkerView.setText(countryAbbrev);
|
||||
customMarkerView.setImage(countryIconRes);
|
||||
return customMarkerView;
|
||||
private static class CountryAdapter implements MapboxMap.MarkerViewAdapter<CountryMarker> {
|
||||
|
||||
private LayoutInflater inflater;
|
||||
|
||||
public CountryAdapter(@NonNull Context context) {
|
||||
this.inflater = LayoutInflater.from(context);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View getView(@NonNull CountryMarker marker, @Nullable View convertView, @NonNull ViewGroup parent) {
|
||||
ViewHolder viewHolder;
|
||||
if (convertView == null) {
|
||||
viewHolder = new ViewHolder();
|
||||
convertView = inflater.inflate(R.layout.view_custom_marker, parent, false);
|
||||
viewHolder.flag = (ImageView) convertView.findViewById(R.id.imageView);
|
||||
viewHolder.abbrev = (TextView) convertView.findViewById(R.id.textView);
|
||||
convertView.setTag(viewHolder);
|
||||
} else {
|
||||
viewHolder = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
viewHolder.flag.setImageResource(marker.getFlagRes());
|
||||
viewHolder.abbrev.setText(marker.getAbbrevName());
|
||||
return convertView;
|
||||
}
|
||||
|
||||
private static class ViewHolder {
|
||||
ImageView flag;
|
||||
TextView abbrev;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -119,5 +133,4 @@ public class ViewMarkerActivity extends AppCompatActivity implements OnMapReadyC
|
||||
super.onLowMemory();
|
||||
mMapView.onLowMemory();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.mapbox.mapboxsdk.testapp.model.annotations;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
|
||||
public class CountryMarker extends Marker {
|
||||
|
||||
private String abbrevName;
|
||||
private int flagRes;
|
||||
|
||||
public CountryMarker(BaseMarkerOptions baseMarkerOptions, String abbrevName, int iconRes) {
|
||||
super(baseMarkerOptions);
|
||||
this.abbrevName = abbrevName;
|
||||
this.flagRes = iconRes;
|
||||
}
|
||||
|
||||
public String getAbbrevName() {
|
||||
return abbrevName;
|
||||
}
|
||||
|
||||
public int getFlagRes() {
|
||||
return flagRes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.mapbox.mapboxsdk.testapp.model.annotations;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.IconFactory;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
|
||||
public class CountryMarkerOptions extends BaseMarkerOptions<CountryMarker, CountryMarkerOptions> {
|
||||
|
||||
private String abbrevName;
|
||||
private int flagRes;
|
||||
|
||||
public CountryMarkerOptions abbrevName(String name) {
|
||||
abbrevName = name;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public CountryMarkerOptions flagRes(int imageRes) {
|
||||
flagRes = imageRes;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public CountryMarkerOptions() {
|
||||
}
|
||||
|
||||
private CountryMarkerOptions(Parcel in) {
|
||||
position((LatLng) in.readParcelable(LatLng.class.getClassLoader()));
|
||||
snippet(in.readString());
|
||||
String iconId = in.readString();
|
||||
Bitmap iconBitmap = in.readParcelable(Bitmap.class.getClassLoader());
|
||||
Icon icon = IconFactory.recreate(iconId, iconBitmap);
|
||||
icon(icon);
|
||||
title(in.readString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CountryMarkerOptions getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CountryMarker getMarker() {
|
||||
return new CountryMarker(this, abbrevName, flagRes);
|
||||
}
|
||||
|
||||
public static final Parcelable.Creator<CountryMarkerOptions> CREATOR
|
||||
= new Parcelable.Creator<CountryMarkerOptions>() {
|
||||
public CountryMarkerOptions createFromParcel(Parcel in) {
|
||||
return new CountryMarkerOptions(in);
|
||||
}
|
||||
|
||||
public CountryMarkerOptions[] newArray(int size) {
|
||||
return new CountryMarkerOptions[size];
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel out, int flags) {
|
||||
out.writeParcelable(position, flags);
|
||||
out.writeString(snippet);
|
||||
out.writeString(icon.getId());
|
||||
out.writeParcelable(icon.getBitmap(), flags);
|
||||
out.writeString(title);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.mapbox.mapboxsdk.testapp.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.view.LayoutInflater;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerView;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
|
||||
public class CustomMarkerView extends MarkerView {
|
||||
|
||||
private TextView textView;
|
||||
private ImageView imageView;
|
||||
|
||||
public CustomMarkerView(Context context) {
|
||||
super(context);
|
||||
init(context);
|
||||
}
|
||||
|
||||
private void init(Context context) {
|
||||
LayoutInflater.from(context).inflate(R.layout.view_custom_marker, this);
|
||||
textView = (TextView) findViewById(R.id.textView);
|
||||
imageView = (ImageView) findViewById(R.id.imageView);
|
||||
}
|
||||
|
||||
public void setText(@NonNull String text) {
|
||||
textView.setText(text);
|
||||
}
|
||||
|
||||
public void setImage(@DrawableRes int drawableRes) {
|
||||
imageView.setImageResource(drawableRes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user