Skip to content

Commit 110c7ae

Browse files
sarahsnow1tallytalwar
authored andcommitted
Expand BitmapMarker API (#427)
* expand BitmapMarker api * StyleStringGenerator shouldnt be singleton & add ability to set MarkerOptions size * Rm unused code * Add support for drawable in marker options * bgcolor to color rename * update documentation
1 parent 0a40f84 commit 110c7ae

File tree

11 files changed

+408
-36
lines changed

11 files changed

+408
-36
lines changed

core/src/main/java/com/mapzen/android/graphics/MapzenMap.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mapzen.android.graphics;
22

33
import com.mapzen.android.core.MapzenManager;
4+
import com.mapzen.android.graphics.internal.StyleStringGenerator;
45
import com.mapzen.android.graphics.model.BitmapMarker;
56
import com.mapzen.android.graphics.model.CameraType;
67
import com.mapzen.android.graphics.model.EaseType;
@@ -30,6 +31,9 @@
3031
import java.util.Locale;
3132
import java.util.Map;
3233

34+
import static com.mapzen.android.graphics.internal.EaseTypeConverter.
35+
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE;
36+
3337
/**
3438
* This is the main class of the Mapzen Android API and is the entry point for all methods related
3539
* to the map. You cannot instantiate a {@link MapzenMap} object directly. Rather you must obtain
@@ -85,16 +89,6 @@ public class MapzenMap {
8589
private TouchInput.ScaleResponder scaleResponder;
8690
private TouchInput.ShoveResponder shoveResponder;
8791

88-
private static final HashMap<EaseType, MapController.EaseType>
89-
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE = new HashMap();
90-
91-
static {
92-
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.LINEAR, MapController.EaseType.LINEAR);
93-
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.CUBIC, MapController.EaseType.CUBIC);
94-
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.QUINT, MapController.EaseType.QUINT);
95-
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.SINE, MapController.EaseType.SINE);
96-
}
97-
9892
private static final HashMap<CameraType, MapController.CameraType>
9993
CAMERA_TYPE_TO_MAP_CONTROLLER_CAMERA_TYPE = new HashMap<>();
10094
private static final HashMap<MapController.CameraType, CameraType>
@@ -611,7 +605,7 @@ public boolean isSimultaneousGestureAllowed(TouchInput.Gestures first,
611605
/**
612606
* Set a listener for feature pick events.
613607
*
614-
* @param listener Listener to call
608+
* @param listener Listener to call when {@link Marker}s are selected.
615609
*/
616610
public void setFeaturePickListener(final FeaturePickListener listener) {
617611
mapController.setFeaturePickListener(new MapController.FeaturePickListener() {
@@ -639,7 +633,7 @@ public void setLabelPickListener(final LabelPickListener listener) {
639633
/**
640634
* Set a listener for marker pick events.
641635
*
642-
* @param listener Listener to receive callback when markers are selected.
636+
* @param listener Listener to receive callback when {@link BitmapMarker}s are selected.
643637
*/
644638
public void setMarkerPickListener(final MarkerPickListener listener) {
645639
mapController.setMarkerPickListener(new MapController.MarkerPickListener() {
@@ -649,7 +643,8 @@ public void onMarkerPick(final MarkerPickResult markerPickResult, final float po
649643
mapView.post(new Runnable() {
650644
@Override public void run() {
651645
if (markerPickResult != null) {
652-
listener.onMarkerPick(new BitmapMarker(markerManager, markerPickResult.getMarker()));
646+
listener.onMarkerPick(new BitmapMarker(markerManager, markerPickResult.getMarker(),
647+
new StyleStringGenerator()));
653648
}
654649
}
655650
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.mapzen.android.graphics.internal;
2+
3+
import com.mapzen.android.graphics.model.EaseType;
4+
import com.mapzen.tangram.MapController;
5+
6+
import java.util.HashMap;
7+
8+
/**
9+
* Converts between SDK {@link EaseType} and internal {@link MapController.EaseType}.
10+
*/
11+
public class EaseTypeConverter {
12+
13+
public static final HashMap<EaseType, MapController.EaseType>
14+
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE = new HashMap();
15+
16+
static {
17+
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.LINEAR, MapController.EaseType.LINEAR);
18+
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.CUBIC, MapController.EaseType.CUBIC);
19+
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.QUINT, MapController.EaseType.QUINT);
20+
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.SINE, MapController.EaseType.SINE);
21+
}
22+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.mapzen.android.graphics.internal;
2+
3+
/**
4+
* Handles updating properties used to generate a style string for a Tangram
5+
* {@link com.mapzen.tangram.Marker}. Used directly by
6+
* {@link com.mapzen.android.graphics.model.BitmapMarker}.
7+
*/
8+
public class StyleStringGenerator {
9+
10+
private int width = 50;
11+
private int height = 50;
12+
private boolean interactive = true;
13+
private String colorHex = "#FFFFFF";
14+
15+
/**
16+
* Set the width and height in pixels.
17+
* @param width
18+
* @param height
19+
*/
20+
public void setSize(int width, int height) {
21+
this.width = width;
22+
this.height = height;
23+
}
24+
25+
/**
26+
* Set whether or not the marker can be selected.
27+
* @param interactive
28+
*/
29+
public void setInteractive(boolean interactive) {
30+
this.interactive = interactive;
31+
}
32+
33+
/**
34+
* Sets the hex value for color to be used.
35+
* @param hex
36+
*/
37+
public void setColor(String hex) {
38+
this.colorHex = hex;
39+
}
40+
41+
/**
42+
* Return the style string given the current property configurations.
43+
* @return
44+
*/
45+
public String getStyleString() {
46+
return new StringBuilder()
47+
.append("{ style: 'points', color: '")
48+
.append(colorHex)
49+
.append("', size: [")
50+
.append(width)
51+
.append("px, ")
52+
.append(height)
53+
.append("px], ")
54+
.append("collide: false, interactive: ")
55+
.append(interactive)
56+
.append(" }")
57+
.toString();
58+
}
59+
}

core/src/main/java/com/mapzen/android/graphics/model/BitmapMarker.java

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
package com.mapzen.android.graphics.model;
22

3+
import com.mapzen.android.graphics.internal.EaseTypeConverter;
4+
import com.mapzen.android.graphics.internal.StyleStringGenerator;
5+
import com.mapzen.tangram.LngLat;
36
import com.mapzen.tangram.Marker;
47

8+
import android.graphics.drawable.Drawable;
9+
510
/**
611
* Dynamic marker overlay constructed using a local bitmap.
712
*/
813
public class BitmapMarker {
14+
915
private final MarkerManager markerManager;
1016
private final Marker tangramMarker;
17+
private final StyleStringGenerator styleStringGenerator;
1118

1219
/**
1320
* Constructor that wraps a Tangram marker.
1421
*
1522
* @param tangramMarker the underlying Tangram marker object.
1623
*/
17-
public BitmapMarker(MarkerManager markerManager, Marker tangramMarker) {
24+
public BitmapMarker(MarkerManager markerManager, Marker tangramMarker,
25+
StyleStringGenerator styleStringGenerator) {
1826
this.markerManager = markerManager;
1927
this.tangramMarker = tangramMarker;
28+
this.styleStringGenerator = styleStringGenerator;
2029
}
2130

2231
/**
@@ -26,4 +35,114 @@ public BitmapMarker(MarkerManager markerManager, Marker tangramMarker) {
2635
public void remove() {
2736
markerManager.removeMarker(tangramMarker);
2837
}
38+
39+
/**
40+
* Sets the marker's coordinate position.
41+
* @param position
42+
*/
43+
public void setPosition(LngLat position) {
44+
this.tangramMarker.setPoint(position);
45+
}
46+
47+
/**
48+
* Sets the marker's coordinate position with animation.
49+
* @param position
50+
* @param duration
51+
* @param easeType
52+
*/
53+
public void setPosition(LngLat position, int duration, EaseType easeType) {
54+
this.tangramMarker.setPointEased(position, duration,
55+
EaseTypeConverter.EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.get(easeType));
56+
}
57+
58+
/**
59+
* Sets the drawable resource id displayed as the marker's icon.
60+
* @param resourceId
61+
*/
62+
public void setIcon(int resourceId) {
63+
this.tangramMarker.setDrawable(resourceId);
64+
}
65+
66+
/**
67+
* Sets the drawable displayed as the marker's icon.
68+
* @param drawable
69+
*/
70+
public void setIcon(Drawable drawable) {
71+
this.tangramMarker.setDrawable(drawable);
72+
}
73+
74+
/**
75+
* Sets the width and height in pixels for the marker's size.
76+
* @param width
77+
* @param height
78+
*/
79+
public void setSize(int width, int height) {
80+
styleStringGenerator.setSize(width, height);
81+
updateStyleString();
82+
}
83+
84+
/**
85+
* Sets the marker's visibility.
86+
* @param visible
87+
*/
88+
public void setVisible(boolean visible) {
89+
tangramMarker.setVisible(visible);
90+
}
91+
92+
/**
93+
* Sets marker z-axis draw order.
94+
* @param drawOrder
95+
*/
96+
public void setDrawOrder(int drawOrder) {
97+
this.tangramMarker.setDrawOrder(drawOrder);
98+
}
99+
100+
/**
101+
* Sets extra data to be associated with this marker.
102+
* @param userData
103+
*/
104+
public void setUserData(Object userData) {
105+
this.tangramMarker.setUserData(userData);
106+
}
107+
108+
/**
109+
* Gets extra data associated with this marker.
110+
* @return
111+
*/
112+
public Object getUserData() {
113+
return this.tangramMarker.getUserData();
114+
}
115+
116+
/**
117+
* Sets color of marker given a color int ie {@code android.graphics.Color.BLUE}.
118+
* @param colorInt
119+
*/
120+
public void setColor(int colorInt) {
121+
String hex = "#" + Integer.toHexString(colorInt);
122+
styleStringGenerator.setColor(hex);
123+
updateStyleString();
124+
}
125+
126+
/**
127+
* Sets color of marker given a color hex string.
128+
* @param hex
129+
*/
130+
public void setColor(String hex) {
131+
styleStringGenerator.setColor(hex);
132+
updateStyleString();
133+
}
134+
135+
/**
136+
* Sets whether or not marker can be selected.
137+
* @param interactive
138+
*/
139+
public void setInteractive(boolean interactive) {
140+
styleStringGenerator.setInteractive(interactive);
141+
updateStyleString();
142+
}
143+
144+
private void updateStyleString() {
145+
tangramMarker.setStylingFromString(styleStringGenerator.getStyleString());
146+
}
147+
29148
}

core/src/main/java/com/mapzen/android/graphics/model/Marker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.mapzen.tangram.LngLat;
44

55
/**
6-
* Represents a pin marker on a map.
6+
* Represents a pin marker on a map backed by {@link com.mapzen.tangram.MapData} objects.
77
*/
88
public class Marker {
99

core/src/main/java/com/mapzen/android/graphics/model/MarkerManager.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mapzen.android.graphics.model;
22

3+
import com.mapzen.android.graphics.internal.StyleStringGenerator;
34
import com.mapzen.tangram.MapController;
45
import com.mapzen.tangram.Marker;
56

@@ -27,9 +28,15 @@ public MarkerManager(MapController mapController) {
2728
public BitmapMarker addMarker(MarkerOptions markerOptions) {
2829
final Marker marker = mapController.addMarker();
2930
marker.setPoint(markerOptions.getPosition());
30-
marker.setDrawable(markerOptions.getIcon());
31-
marker.setStylingFromString(markerOptions.getStyle());
32-
return new BitmapMarker(this, marker);
31+
if (markerOptions.getIconDrawable() != null) {
32+
marker.setDrawable(markerOptions.getIconDrawable());
33+
} else {
34+
marker.setDrawable(markerOptions.getIcon());
35+
}
36+
StyleStringGenerator styleStringGenerator = new StyleStringGenerator();
37+
styleStringGenerator.setSize(markerOptions.getWidth(), markerOptions.getHeight());
38+
marker.setStylingFromString(styleStringGenerator.getStyleString());
39+
return new BitmapMarker(this, marker, styleStringGenerator);
3340
}
3441

3542
/**

0 commit comments

Comments
 (0)