[android] #5076 - fixed update marker for MarkerView, rewrite update marker jni, cleanup animation api
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package com.mapbox.mapboxsdk.annotations;
|
||||
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.AnimatorRes;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
@@ -26,8 +25,6 @@ public abstract class BaseMarkerViewOptions<U extends MarkerView, T extends Base
|
||||
protected float anchorV = 1f;
|
||||
protected float infoWindowAnchorU = 0.5f;
|
||||
protected float infoWindowAnchorV = 0.0f;
|
||||
protected int selectAnimRes;
|
||||
protected int deselectAnimRes;
|
||||
protected int rotation;
|
||||
protected boolean visible = true;
|
||||
protected boolean selected;
|
||||
@@ -119,28 +116,6 @@ public abstract class BaseMarkerViewOptions<U extends MarkerView, T extends Base
|
||||
return getThis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the animator resource to be used when an MarkerView is selected.
|
||||
*
|
||||
* @param selectAnimRes the used animator resource
|
||||
* @return the object for which the method was called
|
||||
*/
|
||||
public T selectAnimatorResource(@AnimatorRes int selectAnimRes) {
|
||||
this.selectAnimRes = selectAnimRes;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the animator resource to be used when an MarkerView is deselected.
|
||||
*
|
||||
* @param deselectAnimRes the used animator resource
|
||||
* @return the object for which the method was called
|
||||
*/
|
||||
public T deselectAnimatorResource(@AnimatorRes int deselectAnimRes) {
|
||||
this.deselectAnimRes = deselectAnimRes;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the rotation of the MarkerView.
|
||||
*
|
||||
@@ -244,24 +219,6 @@ public abstract class BaseMarkerViewOptions<U extends MarkerView, T extends Base
|
||||
return infoWindowAnchorV;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the animator resource used for selecting the MarkerView.
|
||||
*
|
||||
* @return the animator resource
|
||||
*/
|
||||
public int getSelectAnimRes() {
|
||||
return selectAnimRes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the animator resource used for deselecting the MarkerView.
|
||||
*
|
||||
* @return the animator resource
|
||||
*/
|
||||
public int getDeselectAnimRes() {
|
||||
return deselectAnimRes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the rotation of the MarkerView.
|
||||
*
|
||||
|
||||
@@ -28,8 +28,6 @@ public class MarkerViewOptions extends BaseMarkerViewOptions<MarkerView, MarkerV
|
||||
flat(in.readByte() != 0);
|
||||
anchor(in.readFloat(), in.readFloat());
|
||||
infoWindowAnchor(in.readFloat(), in.readFloat());
|
||||
selectAnimatorResource(in.readInt());
|
||||
deselectAnimatorResource(in.readInt());
|
||||
rotation(in.readInt());
|
||||
visible(in.readByte() != 0);
|
||||
if (in.readByte() != 0) {
|
||||
@@ -61,8 +59,6 @@ public class MarkerViewOptions extends BaseMarkerViewOptions<MarkerView, MarkerV
|
||||
out.writeFloat(getAnchorV());
|
||||
out.writeFloat(getInfoWindowAnchorU());
|
||||
out.writeFloat(getInfoWindowAnchorV());
|
||||
out.writeInt(getSelectAnimRes());
|
||||
out.writeInt(getDeselectAnimRes());
|
||||
out.writeInt(getRotation());
|
||||
out.writeByte((byte) (isVisible() ? 1 : 0));
|
||||
Icon icon = getIcon();
|
||||
|
||||
@@ -445,6 +445,7 @@ public class MapView extends FrameLayout {
|
||||
callback.onMapReady(mMapboxMap);
|
||||
iterator.remove();
|
||||
}
|
||||
mMapboxMap.getMarkerViewManager().scheduleViewMarkerInvalidation();
|
||||
}
|
||||
} else if (change == REGION_IS_CHANGING || change == REGION_DID_CHANGE || change == DID_FINISH_LOADING_MAP) {
|
||||
mMapboxMap.getMarkerViewManager().scheduleViewMarkerInvalidation();
|
||||
@@ -672,11 +673,11 @@ public class MapView extends FrameLayout {
|
||||
return mContentPaddingBottom;
|
||||
}
|
||||
|
||||
int getContentWidth(){
|
||||
int getContentWidth() {
|
||||
return getWidth() - mContentPaddingLeft - mContentPaddingRight;
|
||||
}
|
||||
|
||||
int getContentHeight(){
|
||||
int getContentHeight() {
|
||||
return getHeight() - mContentPaddingBottom - mContentPaddingTop;
|
||||
}
|
||||
|
||||
@@ -993,7 +994,10 @@ public class MapView extends FrameLayout {
|
||||
Log.w(MapboxConstants.TAG, "marker has an id of -1, possibly was not added yet, doing nothing");
|
||||
}
|
||||
|
||||
ensureIconLoaded(updatedMarker);
|
||||
if (!(updatedMarker instanceof MarkerView)) {
|
||||
ensureIconLoaded(updatedMarker);
|
||||
}
|
||||
|
||||
mNativeMapView.updateMarker(updatedMarker);
|
||||
}
|
||||
|
||||
@@ -1021,7 +1025,7 @@ public class MapView extends FrameLayout {
|
||||
}
|
||||
|
||||
long addMarker(@NonNull Marker marker) {
|
||||
if(mDestroyed){
|
||||
if (mDestroyed) {
|
||||
return 0l;
|
||||
}
|
||||
return mNativeMapView.addMarker(marker);
|
||||
|
||||
@@ -20,7 +20,6 @@ import com.mapbox.mapboxsdk.annotations.Annotation;
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerViewOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.IconFactory;
|
||||
import com.mapbox.mapboxsdk.annotations.InfoWindow;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
|
||||
@@ -7,6 +7,7 @@ import android.graphics.RectF;
|
||||
import android.os.Build;
|
||||
import android.view.Surface;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.Polygon;
|
||||
import com.mapbox.mapboxsdk.annotations.Polyline;
|
||||
@@ -371,7 +372,9 @@ final class NativeMapView {
|
||||
}
|
||||
|
||||
public void updateMarker(Marker marker) {
|
||||
nativeUpdateMarker(mNativeMapViewPtr, marker);
|
||||
LatLng position = marker.getPosition();
|
||||
Icon icon = marker.getIcon();
|
||||
nativeUpdateMarker(mNativeMapViewPtr, marker.getId(), position.getLatitude(), position.getLongitude(), icon.getId());
|
||||
}
|
||||
|
||||
public void removeAnnotation(long id) {
|
||||
@@ -462,7 +465,7 @@ final class NativeMapView {
|
||||
nativeRemoveCustomLayer(mNativeMapViewPtr, id);
|
||||
}
|
||||
|
||||
public double[] getCameraValues(){
|
||||
public double[] getCameraValues() {
|
||||
return nativeGetCameraValues(mNativeMapViewPtr);
|
||||
}
|
||||
|
||||
@@ -591,7 +594,7 @@ final class NativeMapView {
|
||||
|
||||
private native long nativeAddMarker(long nativeMapViewPtr, Marker marker);
|
||||
|
||||
private native void nativeUpdateMarker(long nativeMapViewPtr, Marker marker);
|
||||
private native void nativeUpdateMarker(long nativeMapViewPtr, long markerId, double lat, double lon, String iconId);
|
||||
|
||||
private native long[] nativeAddMarkers(long nativeMapViewPtr, List<Marker> markers);
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.animation.AnimatorListenerAdapter;
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.app.ActionBar;
|
||||
@@ -31,17 +32,24 @@ 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.CountryMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.testapp.model.annotations.CountryMarkerView;
|
||||
import com.mapbox.mapboxsdk.testapp.model.annotations.CountryMarkerViewOptions;
|
||||
import com.mapbox.mapboxsdk.testapp.model.annotations.TextMarkerView;
|
||||
import com.mapbox.mapboxsdk.testapp.model.annotations.TextMarkerViewOptions;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class MarkerViewActivity extends AppCompatActivity {
|
||||
|
||||
private MapboxMap mMapboxMap;
|
||||
private MapView mMapView;
|
||||
|
||||
private MarkerView movingMarkerOne, movingMarkerTwo;
|
||||
private Random randomAnimator = new Random();
|
||||
private Handler locationUpdateHandler = new Handler();
|
||||
private Runnable moveMarkerRunnable = new MoveMarkerRunnable();
|
||||
private int rotation = 0;
|
||||
|
||||
private final static LatLng[] LAT_LNGS = new LatLng[]{
|
||||
new LatLng(38.897424, -77.036508),
|
||||
new LatLng(38.909698, -77.029642),
|
||||
@@ -83,8 +91,6 @@ public class MarkerViewActivity extends AppCompatActivity {
|
||||
.position(LAT_LNGS[i])
|
||||
.title(String.valueOf(i))
|
||||
.icon(usFlag)
|
||||
.selectAnimatorResource(R.animator.scale_up)
|
||||
.deselectAnimatorResource(R.animator.scale_down)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -94,8 +100,6 @@ public class MarkerViewActivity extends AppCompatActivity {
|
||||
options.abbrevName("Mapbox");
|
||||
options.title("Hello");
|
||||
options.position(new LatLng(38.899774, -77.023237));
|
||||
options.selectAnimatorResource(R.animator.rotate_360);
|
||||
options.deselectAnimatorResource(R.animator.rotate_360);
|
||||
options.flat(true);
|
||||
mapboxMap.addMarker(options);
|
||||
|
||||
@@ -119,6 +123,7 @@ public class MarkerViewActivity extends AppCompatActivity {
|
||||
.position(new LatLng(38.897642, -77.041980))
|
||||
);
|
||||
|
||||
// if you want to customise a ViewMarker you need to extend ViewMarker and provide an adapter implementation
|
||||
// set adapters for child classes of ViewMarker
|
||||
markerViewManager.addMarkerViewAdapter(new CountryAdapter(MarkerViewActivity.this, mapboxMap));
|
||||
markerViewManager.addMarkerViewAdapter(new TextAdapter(MarkerViewActivity.this, mapboxMap));
|
||||
@@ -143,10 +148,53 @@ public class MarkerViewActivity extends AppCompatActivity {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
movingMarkerOne = mMapboxMap.addMarker(new MarkerViewOptions()
|
||||
.position(CarLocation.CAR_0_LNGS[0])
|
||||
.icon(IconFactory.getInstance(mMapView.getContext())
|
||||
.fromResource(R.drawable.ic_chelsea))
|
||||
);
|
||||
|
||||
movingMarkerTwo = mapboxMap.addMarker(new MarkerViewOptions()
|
||||
.position(CarLocation.CAR_1_LNGS[0])
|
||||
.icon(IconFactory.getInstance(mMapView.getContext())
|
||||
.fromResource(R.drawable.ic_arsenal))
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
loopMarkerMove();
|
||||
}
|
||||
|
||||
private void loopMarkerMove() {
|
||||
locationUpdateHandler.postDelayed(moveMarkerRunnable, randomAnimator.nextInt(3000) + 1000);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
locationUpdateHandler.removeCallbacks(moveMarkerRunnable);
|
||||
}
|
||||
|
||||
private class MoveMarkerRunnable implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
int i = randomAnimator.nextInt(9);
|
||||
if (randomAnimator.nextInt() % 2 == 0) {
|
||||
movingMarkerOne.setPosition(CarLocation.CAR_0_LNGS[i]);
|
||||
movingMarkerOne.setRotation(rotation = rotation + 45);
|
||||
} else {
|
||||
movingMarkerTwo.setPosition(CarLocation.CAR_1_LNGS[i]);
|
||||
movingMarkerTwo.setRotation(rotation = rotation + 90);
|
||||
}
|
||||
loopMarkerMove();
|
||||
}
|
||||
}
|
||||
|
||||
private static class CountryAdapter extends MapboxMap.MarkerViewAdapter<CountryMarkerView> {
|
||||
|
||||
private LayoutInflater inflater;
|
||||
@@ -353,4 +401,32 @@ public class MarkerViewActivity extends AppCompatActivity {
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
private static class CarLocation {
|
||||
|
||||
public static LatLng[] CAR_0_LNGS = new LatLng[]{
|
||||
new LatLng(38.92334425495122, -77.0533673443786),
|
||||
new LatLng(38.9234737236897, -77.05389484528261),
|
||||
new LatLng(38.9257094658146, -76.98819752280579),
|
||||
new LatLng(38.8324328369647, -77.00690648325929),
|
||||
new LatLng(38.87540698725855, -77.0093148713099),
|
||||
new LatLng(38.96499498141065, -77.07707916040054),
|
||||
new LatLng(38.90794910679896, -76.99695304153806),
|
||||
new LatLng(38.86234025281626, -76.9950528034839),
|
||||
new LatLng(38.862930274733635, -76.99647808241964)
|
||||
};
|
||||
|
||||
public static LatLng[] CAR_1_LNGS = new LatLng[]{
|
||||
new LatLng(38.94237975070426, -76.98324549005675),
|
||||
new LatLng(38.941520236084486, -76.98234257804742),
|
||||
new LatLng(38.85972219720714, -76.98955808483929),
|
||||
new LatLng(38.944289166113776, -76.98584257252891),
|
||||
new LatLng(38.94375860578053, -76.98470344318412),
|
||||
new LatLng(38.943167431929645, -76.98373163938666),
|
||||
new LatLng(38.882834728904605, -77.02862535635137),
|
||||
new LatLng(38.882869724926245, -77.02992539231113),
|
||||
new LatLng(38.9371988177896, -76.97786740676564)
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
package com.mapbox.mapboxsdk.testapp.model.annotations;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.PointF;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerViewOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.IconFactory;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerView;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
|
||||
public class CountryMarkerViewOptions extends BaseMarkerViewOptions<CountryMarkerView, CountryMarkerViewOptions> {
|
||||
@@ -27,8 +24,6 @@ public class CountryMarkerViewOptions extends BaseMarkerViewOptions<CountryMarke
|
||||
flat(in.readByte() != 0);
|
||||
anchor(in.readFloat(), in.readFloat());
|
||||
infoWindowAnchor(in.readFloat(), in.readFloat());
|
||||
selectAnimatorResource(in.readInt());
|
||||
deselectAnimatorResource(in.readInt());
|
||||
rotation(in.readInt());
|
||||
if (in.readByte() != 0) {
|
||||
// this means we have an icon
|
||||
@@ -61,10 +56,8 @@ public class CountryMarkerViewOptions extends BaseMarkerViewOptions<CountryMarke
|
||||
out.writeFloat(getAnchorV());
|
||||
out.writeFloat(getInfoWindowAnchorU());
|
||||
out.writeFloat(getInfoWindowAnchorV());
|
||||
out.writeInt(getSelectAnimRes());
|
||||
out.writeInt(getDeselectAnimRes());
|
||||
out.writeInt(getRotation());
|
||||
out.writeByte((byte) (selected ? 1 :0));
|
||||
out.writeByte((byte) (selected ? 1 : 0));
|
||||
Icon icon = getIcon();
|
||||
out.writeByte((byte) (icon != null ? 1 : 0));
|
||||
if (icon != null) {
|
||||
|
||||
@@ -9,7 +9,7 @@ import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.IconFactory;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
|
||||
public class TextMarkerViewOptions extends BaseMarkerViewOptions<TextMarkerView,TextMarkerViewOptions> {
|
||||
public class TextMarkerViewOptions extends BaseMarkerViewOptions<TextMarkerView, TextMarkerViewOptions> {
|
||||
|
||||
private String text;
|
||||
|
||||
@@ -23,8 +23,6 @@ public class TextMarkerViewOptions extends BaseMarkerViewOptions<TextMarkerView,
|
||||
flat(in.readByte() != 0);
|
||||
anchor(in.readFloat(), in.readFloat());
|
||||
infoWindowAnchor(in.readFloat(), in.readFloat());
|
||||
selectAnimatorResource(in.readInt());
|
||||
deselectAnimatorResource(in.readInt());
|
||||
rotation(in.readInt());
|
||||
if (in.readByte() != 0) {
|
||||
// this means we have an icon
|
||||
@@ -56,8 +54,6 @@ public class TextMarkerViewOptions extends BaseMarkerViewOptions<TextMarkerView,
|
||||
out.writeFloat(getAnchorV());
|
||||
out.writeFloat(getInfoWindowAnchorU());
|
||||
out.writeFloat(getInfoWindowAnchorV());
|
||||
out.writeInt(getSelectAnimRes());
|
||||
out.writeInt(getDeselectAnimRes());
|
||||
out.writeInt(getRotation());
|
||||
Icon icon = getIcon();
|
||||
out.writeByte((byte) (icon != null ? 1 : 0));
|
||||
|
||||
@@ -738,27 +738,19 @@ jlong nativeAddMarker(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jn
|
||||
return nativeMapView->getMap().addPointAnnotation(mbgl::PointAnnotation(mbgl::LatLng(latitude, longitude), id));
|
||||
}
|
||||
|
||||
void nativeUpdateMarker(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jobject* marker) {
|
||||
void nativeUpdateMarker(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jlong markerId, jdouble lat, jdouble lon, jni::jstring* jid) {
|
||||
mbgl::Log::Debug(mbgl::Event::JNI, "nativeUpdateMarker");
|
||||
assert(nativeMapViewPtr != 0);
|
||||
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
|
||||
|
||||
jlong markerId = jni::GetField<jlong>(*env, marker, *markerIdId);
|
||||
if (markerId == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
jni::jobject* position = jni::GetField<jni::jobject*>(*env, marker, *markerPositionId);
|
||||
jni::jobject* icon = jni::GetField<jni::jobject*>(*env, marker, *markerIconId);
|
||||
|
||||
jni::jstring* jid = reinterpret_cast<jni::jstring*>(jni::GetField<jni::jobject*>(*env, icon, *iconIdId));
|
||||
std::string iconId = std_string_from_jstring(env, jid);
|
||||
|
||||
jdouble latitude = jni::GetField<jdouble>(*env, position, *latLngLatitudeId);
|
||||
jdouble longitude = jni::GetField<jdouble>(*env, position, *latLngLongitudeId);
|
||||
|
||||
// Because Java only has int, not unsigned int, we need to bump the annotation id up to a long.
|
||||
nativeMapView->getMap().updatePointAnnotation(markerId, mbgl::PointAnnotation(mbgl::LatLng(latitude, longitude), iconId));
|
||||
nativeMapView->getMap().updatePointAnnotation(markerId, mbgl::PointAnnotation(mbgl::LatLng(lat, lon), iconId));
|
||||
}
|
||||
|
||||
jni::jarray<jlong>* nativeAddMarkers(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jobject* jlist) {
|
||||
@@ -1853,7 +1845,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
|
||||
MAKE_NATIVE_METHOD(nativeAddPolylines, "(JLjava/util/List;)[J"),
|
||||
MAKE_NATIVE_METHOD(nativeAddPolygon, "(JLcom/mapbox/mapboxsdk/annotations/Polygon;)J"),
|
||||
MAKE_NATIVE_METHOD(nativeAddPolygons, "(JLjava/util/List;)[J"),
|
||||
MAKE_NATIVE_METHOD(nativeUpdateMarker, "(JLcom/mapbox/mapboxsdk/annotations/Marker;)V"),
|
||||
MAKE_NATIVE_METHOD(nativeUpdateMarker, "(JJDDLjava/lang/String;)V"),
|
||||
MAKE_NATIVE_METHOD(nativeRemoveAnnotation, "(JJ)V"),
|
||||
MAKE_NATIVE_METHOD(nativeRemoveAnnotations, "(J[J)V"),
|
||||
MAKE_NATIVE_METHOD(nativeGetAnnotationsInBounds, "(JLcom/mapbox/mapboxsdk/geometry/LatLngBounds;)[J"),
|
||||
|
||||
Reference in New Issue
Block a user