Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #5076 - fixed update marker for MarkerView, rewrite update …
Browse files Browse the repository at this point in the history
…marker jni, cleanup animation api
  • Loading branch information
tobrun committed Jun 6, 2016
1 parent 436c23c commit 0a646b4
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -119,28 +116,6 @@ public T infoWindowAnchor(float u, float v) {
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.
*
Expand Down Expand Up @@ -244,24 +219,6 @@ public float getInfoWindowAnchorV() {
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ protected MarkerViewOptions(Parcel in) {
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) {
Expand Down Expand Up @@ -61,8 +59,6 @@ public void writeToParcel(Parcel out, int flags) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ public void onMapChanged(@MapChange int change) {
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();
Expand Down Expand Up @@ -672,11 +673,11 @@ int getContentPaddingBottom() {
return mContentPaddingBottom;
}

int getContentWidth(){
int getContentWidth() {
return getWidth() - mContentPaddingLeft - mContentPaddingRight;
}

int getContentHeight(){
int getContentHeight() {
return getHeight() - mContentPaddingBottom - mContentPaddingTop;
}

Expand Down Expand Up @@ -993,7 +994,10 @@ void updateMarker(@NonNull Marker updatedMarker) {
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);
}

Expand Down Expand Up @@ -1021,7 +1025,7 @@ private void ensureIconLoaded(Marker marker) {
}

long addMarker(@NonNull Marker marker) {
if(mDestroyed){
if (mDestroyed) {
return 0l;
}
return mNativeMapView.addMarker(marker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand Down Expand Up @@ -371,7 +372,9 @@ public long[] addPolygons(List<Polygon> polygon) {
}

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) {
Expand Down Expand Up @@ -462,7 +465,7 @@ public void removeCustomLayer(String id) {
nativeRemoveCustomLayer(mNativeMapViewPtr, id);
}

public double[] getCameraValues(){
public double[] getCameraValues() {
return nativeGetCameraValues(mNativeMapViewPtr);
}

Expand Down Expand Up @@ -591,7 +594,7 @@ private native void nativeSetBearingXY(long nativeMapViewPtr, double degrees,

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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand All @@ -31,17 +32,24 @@
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),
Expand Down Expand Up @@ -83,8 +91,6 @@ public void onMapReady(@NonNull MapboxMap mapboxMap) {
.position(LAT_LNGS[i])
.title(String.valueOf(i))
.icon(usFlag)
.selectAnimatorResource(R.animator.scale_up)
.deselectAnimatorResource(R.animator.scale_down)
);
}

Expand All @@ -94,8 +100,6 @@ public void onMapReady(@NonNull MapboxMap mapboxMap) {
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);

Expand All @@ -119,6 +123,7 @@ public void onMapReady(@NonNull MapboxMap mapboxMap) {
.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));
Expand All @@ -143,10 +148,53 @@ public boolean onMarkerClick(@NonNull Marker marker, @NonNull View view, @NonNul
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;
Expand Down Expand Up @@ -353,4 +401,32 @@ public boolean onOptionsItemSelected(MenuItem item) {
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)
};

}
}
Original file line number Diff line number Diff line change
@@ -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> {
Expand All @@ -27,8 +24,6 @@ protected CountryMarkerViewOptions(Parcel in) {
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
Expand Down Expand Up @@ -61,10 +56,8 @@ public void writeToParcel(Parcel out, int flags) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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;

Expand All @@ -23,8 +23,6 @@ protected TextMarkerViewOptions(Parcel in) {
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
Expand Down Expand Up @@ -56,8 +54,6 @@ public void writeToParcel(Parcel out, int flags) {
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));
Expand Down
Loading

0 comments on commit 0a646b4

Please sign in to comment.