[android] #3276 - introduced separate methods for GL markers versus View markers
This commit is contained in:
@@ -3,15 +3,17 @@ package com.mapbox.mapboxsdk.annotations;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.PointF;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.AnimatorRes;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
|
||||
/**
|
||||
* Abstract builder class for composing custom Marker objects.
|
||||
*
|
||||
* Abstract builder class for composing custom MarkerView objects.
|
||||
* <p>
|
||||
* Extending this class requires implementing Parceable interface.
|
||||
*
|
||||
* @param <U> Type of the marker to be composed
|
||||
* @param <U> Type of the marker view to be composed
|
||||
* @param <T> Type of the builder to be used for composing a custom Marker
|
||||
*/
|
||||
public abstract class BaseMarkerViewOptions<U extends MarkerView, T extends BaseMarkerViewOptions<U, T>> implements Parcelable {
|
||||
@@ -31,7 +33,7 @@ public abstract class BaseMarkerViewOptions<U extends MarkerView, T extends Base
|
||||
infoWindowOffset = new Point();
|
||||
}
|
||||
|
||||
public T position(LatLng position) {
|
||||
public T position(@NonNull LatLng position) {
|
||||
this.position = position;
|
||||
return getThis();
|
||||
}
|
||||
@@ -66,12 +68,12 @@ public abstract class BaseMarkerViewOptions<U extends MarkerView, T extends Base
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public T selectAnimRes(int selectAnimRes) {
|
||||
public T selectAnimatorResource(@AnimatorRes int selectAnimRes) {
|
||||
this.selectAnimRes = selectAnimRes;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public T deselectAnimRes(int deselectAnimRes) {
|
||||
public T deselectAnimatorResource(@AnimatorRes int deselectAnimRes) {
|
||||
this.deselectAnimRes = deselectAnimRes;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
@@ -24,8 +24,8 @@ public class MarkerViewOptions extends BaseMarkerViewOptions<MarkerView, MarkerV
|
||||
flat(in.readByte() != 0);
|
||||
centerOffset((PointF) in.readParcelable(PointF.class.getClassLoader()));
|
||||
infoWindowOffset((Point) in.readParcelable(Point.class.getClassLoader()));
|
||||
selectAnimRes(in.readInt());
|
||||
deselectAnimRes(in.readInt());
|
||||
selectAnimatorResource(in.readInt());
|
||||
deselectAnimatorResource(in.readInt());
|
||||
if (in.readByte() != 0) {
|
||||
// this means we have an icon
|
||||
String iconId = in.readString();
|
||||
|
||||
@@ -804,10 +804,8 @@ public class MapboxMap {
|
||||
*/
|
||||
@UiThread
|
||||
@NonNull
|
||||
public Marker addMarker(@NonNull BaseMarkerViewOptions markerOptions) {
|
||||
Marker marker = markerOptions.getMarker();
|
||||
Icon icon = IconFactory.recreate("markerViewSettings", mViewMarkerBitmap);
|
||||
marker.setIcon(icon);
|
||||
public Marker addMarkerView(@NonNull BaseMarkerViewOptions markerOptions) {
|
||||
Marker marker = prepareViewMarker(markerOptions);
|
||||
long id = mMapView.addMarker(marker);
|
||||
marker.setMapboxMap(this);
|
||||
marker.setId(id);
|
||||
@@ -864,6 +862,58 @@ public class MapboxMap {
|
||||
return markers;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Adds multiple markers to this map.
|
||||
* </p>
|
||||
* The marker's icon is rendered on the map at the location {@code Marker.position}.
|
||||
* If {@code Marker.title} is defined, the map shows an info box with the marker's title and snippet.
|
||||
*
|
||||
* @param markerOptionsList A list of marker options objects that defines how to render the markers.
|
||||
* @return A list of the {@code Marker}s that were added to the map.
|
||||
*/
|
||||
@UiThread
|
||||
@NonNull
|
||||
public List<Marker> addMarkerViews(@NonNull List<? extends BaseMarkerViewOptions> markerOptionsList) {
|
||||
int count = markerOptionsList.size();
|
||||
List<Marker> markers = new ArrayList<>(count);
|
||||
if (count > 0) {
|
||||
BaseMarkerViewOptions markerOptions;
|
||||
Marker marker;
|
||||
for (int i = 0; i < count; i++) {
|
||||
markerOptions = markerOptionsList.get(i);
|
||||
marker = markerOptions.getMarker();
|
||||
Icon icon = IconFactory.recreate("markerViewSettings", mViewMarkerBitmap);
|
||||
marker.setIcon(icon);
|
||||
markers.add(marker);
|
||||
}
|
||||
|
||||
if (markers.size() > 0) {
|
||||
long[] ids = mMapView.addMarkers(markers);
|
||||
|
||||
// if unittests or markers are correctly added to map
|
||||
if (ids == null || ids.length == markers.size()) {
|
||||
long id = 0;
|
||||
Marker m;
|
||||
for (int i = 0; i < markers.size(); i++) {
|
||||
m = markers.get(i);
|
||||
m.setMapboxMap(this);
|
||||
if (ids != null) {
|
||||
id = ids[i];
|
||||
} else {
|
||||
//unit test
|
||||
id++;
|
||||
}
|
||||
m.setId(id);
|
||||
mAnnotations.put(id, m);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
invalidateViewMarkersInBounds();
|
||||
return markers;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Updates a marker on this map. Does nothing if the marker is already added.
|
||||
@@ -1352,20 +1402,22 @@ public class MapboxMap {
|
||||
|
||||
private Marker prepareMarker(BaseMarkerOptions markerOptions) {
|
||||
Marker marker = markerOptions.getMarker();
|
||||
if (marker instanceof MarkerView) {
|
||||
Icon icon = IconFactory.recreate("markerViewSettings", mViewMarkerBitmap);
|
||||
marker.setIcon(icon);
|
||||
} else {
|
||||
Icon icon = mMapView.loadIconForMarker(marker);
|
||||
marker.setTopOffsetPixels(mMapView.getTopOffsetPixelsForIcon(icon));
|
||||
}
|
||||
Icon icon = mMapView.loadIconForMarker(marker);
|
||||
marker.setTopOffsetPixels(mMapView.getTopOffsetPixelsForIcon(icon));
|
||||
return marker;
|
||||
}
|
||||
|
||||
private Marker prepareViewMarker(BaseMarkerViewOptions markerViewOptions) {
|
||||
Marker marker = markerViewOptions.getMarker();
|
||||
Icon icon = IconFactory.recreate("markerViewSettings", mViewMarkerBitmap);
|
||||
marker.setIcon(icon);
|
||||
return marker;
|
||||
}
|
||||
|
||||
public void addMarkerViewAdapter(@Nullable MarkerViewAdapter markerViewAdapter) {
|
||||
if (!mMarkerViewAdapters.contains(markerViewAdapter)) {
|
||||
mMarkerViewAdapters.add(markerViewAdapter);
|
||||
mMapView.invalidateViewMarkers();
|
||||
invalidateViewMarkersInBounds();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package com.mapbox.mapboxsdk.testapp.activity.annotation;
|
||||
|
||||
import android.animation.Animator;
|
||||
import android.animation.AnimatorListenerAdapter;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.StringDef;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
@@ -20,7 +23,6 @@ import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerView;
|
||||
@@ -38,12 +40,14 @@ import java.io.IOException;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class BulkMarkerActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
|
||||
|
||||
private MapboxMap mMapboxMap;
|
||||
private MapView mMapView;
|
||||
private boolean mCustomMarkerView;
|
||||
private List<LatLng> mLocations;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -115,12 +119,68 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
|
||||
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
loadBulkMarkers(position);
|
||||
int amount = Integer.valueOf(getResources().getStringArray(R.array.bulk_marker_list)[position]);
|
||||
if (mLocations == null) {
|
||||
new LoadLocationTask(this, amount).execute();
|
||||
} else {
|
||||
showMarkers(amount);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadBulkMarkers(int selectedSpinnerPosition) {
|
||||
int markersAmount = Integer.valueOf(getResources().getStringArray(R.array.bulk_marker_list)[selectedSpinnerPosition]);
|
||||
new LoadBulkMarkerTask(this, mMapboxMap, markersAmount, mCustomMarkerView).execute();
|
||||
|
||||
private void onLatLngListLoaded(List<LatLng> latLngs, int amount) {
|
||||
mLocations = latLngs;
|
||||
showMarkers(amount);
|
||||
}
|
||||
|
||||
private void showMarkers(int amount) {
|
||||
mMapboxMap.clear();
|
||||
|
||||
if (mLocations.size() < amount) {
|
||||
amount = mLocations.size();
|
||||
}
|
||||
|
||||
if (mCustomMarkerView) {
|
||||
showNativeMarkers(amount);
|
||||
} else {
|
||||
showGlMarkers(amount);
|
||||
}
|
||||
}
|
||||
|
||||
private void showNativeMarkers(int amount) {
|
||||
List<MarkerViewOptions> markerOptionsList = new ArrayList<>();
|
||||
DecimalFormat formatter = new DecimalFormat("#.#####");
|
||||
Random random = new Random();
|
||||
int randomIndex;
|
||||
|
||||
for (int i = 0; i < amount; i++) {
|
||||
randomIndex = random.nextInt(mLocations.size());
|
||||
LatLng latLng = mLocations.get(randomIndex);
|
||||
markerOptionsList.add(new MarkerViewOptions()
|
||||
.position(latLng)
|
||||
.title(String.valueOf(i))
|
||||
.snippet(formatter.format(latLng.getLatitude()) + ", " + formatter.format(latLng.getLongitude())));
|
||||
}
|
||||
|
||||
mMapboxMap.addMarkerViews(markerOptionsList);
|
||||
}
|
||||
|
||||
private void showGlMarkers(int amount) {
|
||||
List<MarkerOptions> markerOptionsList = new ArrayList<>();
|
||||
DecimalFormat formatter = new DecimalFormat("#.#####");
|
||||
Random random = new Random();
|
||||
int randomIndex;
|
||||
|
||||
for (int i = 0; i < amount; i++) {
|
||||
randomIndex = random.nextInt(mLocations.size());
|
||||
LatLng latLng = mLocations.get(randomIndex);
|
||||
markerOptionsList.add(new MarkerOptions()
|
||||
.position(latLng)
|
||||
.title(String.valueOf(i))
|
||||
.snippet(formatter.format(latLng.getLatitude()) + ", " + formatter.format(latLng.getLongitude())));
|
||||
}
|
||||
|
||||
mMapboxMap.addMarkers(markerOptionsList);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -171,12 +231,29 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
|
||||
|
||||
private class FabClickListener implements View.OnClickListener {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
public void onClick(final View v) {
|
||||
if (mMapboxMap != null) {
|
||||
mCustomMarkerView = true;
|
||||
|
||||
v.animate().alpha(0).start();
|
||||
// remove fab
|
||||
v.animate().alpha(0).setListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
super.onAnimationEnd(animation);
|
||||
v.setVisibility(View.GONE);
|
||||
}
|
||||
}).start();
|
||||
|
||||
// reload markers
|
||||
Spinner spinner = (Spinner) findViewById(R.id.spinner);
|
||||
if (spinner != null) {
|
||||
int amount = Integer.valueOf(getResources().getStringArray(R.array.bulk_marker_list)[spinner.getSelectedItemPosition()]);
|
||||
showMarkers(amount);
|
||||
}
|
||||
|
||||
// add adapter
|
||||
mMapboxMap.addMarkerViewAdapter(new TextAdapter(BulkMarkerActivity.this));
|
||||
|
||||
mMapView.addOnMapChangedListener(new MapView.OnMapChangedListener() {
|
||||
@Override
|
||||
public void onMapChanged(@MapView.MapChange int change) {
|
||||
@@ -196,77 +273,38 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
// reload markers
|
||||
Spinner spinner = (Spinner) findViewById(R.id.spinner);
|
||||
if (spinner != null) {
|
||||
loadBulkMarkers(spinner.getSelectedItemPosition());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class LoadBulkMarkerTask extends AsyncTask<Void, Integer, List<BaseMarkerOptions>> {
|
||||
private static class LoadLocationTask extends AsyncTask<Void, Integer, List<LatLng>> {
|
||||
|
||||
private static final String TAG = "LoadBulkMarkerTask";
|
||||
private MapboxMap mMapboxMap;
|
||||
private Context mAppContext;
|
||||
private static final String TAG = "LoadLocationTask";
|
||||
private BulkMarkerActivity mActivity;
|
||||
private ProgressDialog mProgressDialog;
|
||||
private int mAmount;
|
||||
private boolean mMarkerView;
|
||||
|
||||
public LoadBulkMarkerTask(Context context, MapboxMap mapboxMap, int amount, boolean markerView) {
|
||||
mMapboxMap = mapboxMap;
|
||||
mapboxMap.removeAnnotations();
|
||||
mProgressDialog = ProgressDialog.show(context, "Loading", "Fetching markers", false);
|
||||
mAppContext = context.getApplicationContext();
|
||||
public LoadLocationTask(BulkMarkerActivity activity, int amount) {
|
||||
mAmount = amount;
|
||||
mMarkerView = markerView;
|
||||
mActivity = activity;
|
||||
mProgressDialog = ProgressDialog.show(activity, "Loading", "Fetching markers", false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<BaseMarkerOptions> doInBackground(Void... params) {
|
||||
List<BaseMarkerOptions> markerOptions = new ArrayList<>(mAmount);
|
||||
protected List<LatLng> doInBackground(Void... params) {
|
||||
try {
|
||||
DecimalFormat formatter = new DecimalFormat("#.#####");
|
||||
String json = GeoParseUtil.loadStringFromAssets(mAppContext, "points.geojson");
|
||||
List<LatLng> locations = GeoParseUtil.parseGeoJSONCoordinates(json);
|
||||
|
||||
if (locations.size() < mAmount) {
|
||||
mAmount = locations.size();
|
||||
}
|
||||
|
||||
LatLng location;
|
||||
for (int i = 0; i < mAmount; i++) {
|
||||
location = locations.get(i);
|
||||
|
||||
if (mMarkerView) {
|
||||
// markerOptions.add(new MarkerViewOptions()
|
||||
// .position(location)
|
||||
// .title(String.valueOf(i))
|
||||
// .snippet(formatter.format(location.getLatitude()) + ", " + formatter.format(location.getLongitude())));
|
||||
} else {
|
||||
markerOptions.add(new MarkerOptions()
|
||||
.position(location)
|
||||
.title(String.valueOf(i))
|
||||
.snippet(formatter.format(location.getLatitude()) + ", " + formatter.format(location.getLongitude())));
|
||||
}
|
||||
}
|
||||
String json = GeoParseUtil.loadStringFromAssets(mActivity.getApplicationContext(), "points.geojson");
|
||||
return GeoParseUtil.parseGeoJSONCoordinates(json);
|
||||
} catch (IOException | JSONException e) {
|
||||
Log.e(TAG, "Could not add markers,", e);
|
||||
return null;
|
||||
}
|
||||
return markerOptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onProgressUpdate(Integer... values) {
|
||||
super.onProgressUpdate(values);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<BaseMarkerOptions> markerOptions) {
|
||||
super.onPostExecute(markerOptions);
|
||||
mMapboxMap.addMarkers(markerOptions);
|
||||
protected void onPostExecute(List<LatLng> locations) {
|
||||
super.onPostExecute(locations);
|
||||
mActivity.onLatLngListLoaded(locations, mAmount);
|
||||
mProgressDialog.hide();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerView;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerViewOptions;
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
@@ -27,7 +26,6 @@ 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.model.annotations.CountryMarker;
|
||||
import com.mapbox.mapboxsdk.testapp.model.annotations.CountryMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.testapp.model.annotations.CountryMarkerView;
|
||||
import com.mapbox.mapboxsdk.testapp.model.annotations.CountryMarkerViewOptions;
|
||||
@@ -72,12 +70,12 @@ public class ViewMarkerAdapterActivity extends AppCompatActivity {
|
||||
|
||||
// add text markers
|
||||
for (int i = 0; i < LAT_LNGS.length; i++) {
|
||||
mMapboxMap.addMarker(new MarkerViewOptions()
|
||||
mMapboxMap.addMarkerView(new MarkerViewOptions()
|
||||
.position(LAT_LNGS[i])
|
||||
.title(String.valueOf(i))
|
||||
.infoWindowOffset(new Point(0,32))
|
||||
.selectAnimRes(R.animator.scale_up)
|
||||
.deselectAnimRes(R.animator.scale_down)
|
||||
.infoWindowOffset(new Point(0, 86))
|
||||
.selectAnimatorResource(R.animator.scale_up)
|
||||
.deselectAnimatorResource(R.animator.scale_down)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -87,12 +85,12 @@ public class ViewMarkerAdapterActivity extends AppCompatActivity {
|
||||
options.abbrevName("us");
|
||||
options.flagRes(R.drawable.ic_us);
|
||||
options.position(new LatLng(38.899774, -77.023237));
|
||||
options.selectAnimRes(R.animator.rotate_360);
|
||||
options.deselectAnimRes(R.animator.rotate_360);
|
||||
options.selectAnimatorResource(R.animator.rotate_360);
|
||||
options.deselectAnimatorResource(R.animator.rotate_360);
|
||||
options.infoWindowOffset(new Point(0, 64));
|
||||
options.flat(true);
|
||||
|
||||
mapboxMap.addMarker(options);
|
||||
mapboxMap.addMarkerView(options);
|
||||
|
||||
// default GL marker
|
||||
mMapboxMap.addMarker(new CountryMarkerOptions()
|
||||
@@ -147,7 +145,7 @@ public class ViewMarkerAdapterActivity extends AppCompatActivity {
|
||||
} else {
|
||||
viewHolder = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
Log.v(MapboxConstants.TAG,"SSSSST");
|
||||
Log.v(MapboxConstants.TAG, "SSSSST");
|
||||
viewHolder.title.setText(marker.getTitle());
|
||||
return convertView;
|
||||
}
|
||||
@@ -179,7 +177,7 @@ public class ViewMarkerAdapterActivity extends AppCompatActivity {
|
||||
} else {
|
||||
viewHolder = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
Log.v(MapboxConstants.TAG,"TESTSSSSS");
|
||||
Log.v(MapboxConstants.TAG, "TESTSSSSS");
|
||||
viewHolder.flag.setImageResource(marker.getFlagRes());
|
||||
viewHolder.abbrev.setText(marker.getAbbrevName());
|
||||
return convertView;
|
||||
|
||||
@@ -26,8 +26,8 @@ public class CountryMarkerViewOptions extends BaseMarkerViewOptions<CountryMarke
|
||||
flat(in.readByte() != 0);
|
||||
centerOffset((PointF) in.readParcelable(PointF.class.getClassLoader()));
|
||||
infoWindowOffset((Point) in.readParcelable(Point.class.getClassLoader()));
|
||||
selectAnimRes(in.readInt());
|
||||
deselectAnimRes(in.readInt());
|
||||
selectAnimatorResource(in.readInt());
|
||||
deselectAnimatorResource(in.readInt());
|
||||
if (in.readByte() != 0) {
|
||||
// this means we have an icon
|
||||
String iconId = in.readString();
|
||||
|
||||
Reference in New Issue
Block a user