Skip to content

Commit

Permalink
Expand BitmapMarker API (#427)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sarahsnow1 authored and tallytalwar committed Aug 15, 2017
1 parent 0a40f84 commit 110c7ae
Show file tree
Hide file tree
Showing 11 changed files with 408 additions and 36 deletions.
21 changes: 8 additions & 13 deletions core/src/main/java/com/mapzen/android/graphics/MapzenMap.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapzen.android.graphics;

import com.mapzen.android.core.MapzenManager;
import com.mapzen.android.graphics.internal.StyleStringGenerator;
import com.mapzen.android.graphics.model.BitmapMarker;
import com.mapzen.android.graphics.model.CameraType;
import com.mapzen.android.graphics.model.EaseType;
Expand Down Expand Up @@ -30,6 +31,9 @@
import java.util.Locale;
import java.util.Map;

import static com.mapzen.android.graphics.internal.EaseTypeConverter.
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE;

/**
* This is the main class of the Mapzen Android API and is the entry point for all methods related
* to the map. You cannot instantiate a {@link MapzenMap} object directly. Rather you must obtain
Expand Down Expand Up @@ -85,16 +89,6 @@ public class MapzenMap {
private TouchInput.ScaleResponder scaleResponder;
private TouchInput.ShoveResponder shoveResponder;

private static final HashMap<EaseType, MapController.EaseType>
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE = new HashMap();

static {
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.LINEAR, MapController.EaseType.LINEAR);
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.CUBIC, MapController.EaseType.CUBIC);
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.QUINT, MapController.EaseType.QUINT);
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.SINE, MapController.EaseType.SINE);
}

private static final HashMap<CameraType, MapController.CameraType>
CAMERA_TYPE_TO_MAP_CONTROLLER_CAMERA_TYPE = new HashMap<>();
private static final HashMap<MapController.CameraType, CameraType>
Expand Down Expand Up @@ -611,7 +605,7 @@ public boolean isSimultaneousGestureAllowed(TouchInput.Gestures first,
/**
* Set a listener for feature pick events.
*
* @param listener Listener to call
* @param listener Listener to call when {@link Marker}s are selected.
*/
public void setFeaturePickListener(final FeaturePickListener listener) {
mapController.setFeaturePickListener(new MapController.FeaturePickListener() {
Expand Down Expand Up @@ -639,7 +633,7 @@ public void setLabelPickListener(final LabelPickListener listener) {
/**
* Set a listener for marker pick events.
*
* @param listener Listener to receive callback when markers are selected.
* @param listener Listener to receive callback when {@link BitmapMarker}s are selected.
*/
public void setMarkerPickListener(final MarkerPickListener listener) {
mapController.setMarkerPickListener(new MapController.MarkerPickListener() {
Expand All @@ -649,7 +643,8 @@ public void onMarkerPick(final MarkerPickResult markerPickResult, final float po
mapView.post(new Runnable() {
@Override public void run() {
if (markerPickResult != null) {
listener.onMarkerPick(new BitmapMarker(markerManager, markerPickResult.getMarker()));
listener.onMarkerPick(new BitmapMarker(markerManager, markerPickResult.getMarker(),
new StyleStringGenerator()));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mapzen.android.graphics.internal;

import com.mapzen.android.graphics.model.EaseType;
import com.mapzen.tangram.MapController;

import java.util.HashMap;

/**
* Converts between SDK {@link EaseType} and internal {@link MapController.EaseType}.
*/
public class EaseTypeConverter {

public static final HashMap<EaseType, MapController.EaseType>
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE = new HashMap();

static {
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.LINEAR, MapController.EaseType.LINEAR);
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.CUBIC, MapController.EaseType.CUBIC);
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.QUINT, MapController.EaseType.QUINT);
EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.put(EaseType.SINE, MapController.EaseType.SINE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.mapzen.android.graphics.internal;

/**
* Handles updating properties used to generate a style string for a Tangram
* {@link com.mapzen.tangram.Marker}. Used directly by
* {@link com.mapzen.android.graphics.model.BitmapMarker}.
*/
public class StyleStringGenerator {

private int width = 50;
private int height = 50;
private boolean interactive = true;
private String colorHex = "#FFFFFF";

/**
* Set the width and height in pixels.
* @param width
* @param height
*/
public void setSize(int width, int height) {
this.width = width;
this.height = height;
}

/**
* Set whether or not the marker can be selected.
* @param interactive
*/
public void setInteractive(boolean interactive) {
this.interactive = interactive;
}

/**
* Sets the hex value for color to be used.
* @param hex
*/
public void setColor(String hex) {
this.colorHex = hex;
}

/**
* Return the style string given the current property configurations.
* @return
*/
public String getStyleString() {
return new StringBuilder()
.append("{ style: 'points', color: '")
.append(colorHex)
.append("', size: [")
.append(width)
.append("px, ")
.append(height)
.append("px], ")
.append("collide: false, interactive: ")
.append(interactive)
.append(" }")
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package com.mapzen.android.graphics.model;

import com.mapzen.android.graphics.internal.EaseTypeConverter;
import com.mapzen.android.graphics.internal.StyleStringGenerator;
import com.mapzen.tangram.LngLat;
import com.mapzen.tangram.Marker;

import android.graphics.drawable.Drawable;

/**
* Dynamic marker overlay constructed using a local bitmap.
*/
public class BitmapMarker {

private final MarkerManager markerManager;
private final Marker tangramMarker;
private final StyleStringGenerator styleStringGenerator;

/**
* Constructor that wraps a Tangram marker.
*
* @param tangramMarker the underlying Tangram marker object.
*/
public BitmapMarker(MarkerManager markerManager, Marker tangramMarker) {
public BitmapMarker(MarkerManager markerManager, Marker tangramMarker,
StyleStringGenerator styleStringGenerator) {
this.markerManager = markerManager;
this.tangramMarker = tangramMarker;
this.styleStringGenerator = styleStringGenerator;
}

/**
Expand All @@ -26,4 +35,114 @@ public BitmapMarker(MarkerManager markerManager, Marker tangramMarker) {
public void remove() {
markerManager.removeMarker(tangramMarker);
}

/**
* Sets the marker's coordinate position.
* @param position
*/
public void setPosition(LngLat position) {
this.tangramMarker.setPoint(position);
}

/**
* Sets the marker's coordinate position with animation.
* @param position
* @param duration
* @param easeType
*/
public void setPosition(LngLat position, int duration, EaseType easeType) {
this.tangramMarker.setPointEased(position, duration,
EaseTypeConverter.EASE_TYPE_TO_MAP_CONTROLLER_EASE_TYPE.get(easeType));
}

/**
* Sets the drawable resource id displayed as the marker's icon.
* @param resourceId
*/
public void setIcon(int resourceId) {
this.tangramMarker.setDrawable(resourceId);
}

/**
* Sets the drawable displayed as the marker's icon.
* @param drawable
*/
public void setIcon(Drawable drawable) {
this.tangramMarker.setDrawable(drawable);
}

/**
* Sets the width and height in pixels for the marker's size.
* @param width
* @param height
*/
public void setSize(int width, int height) {
styleStringGenerator.setSize(width, height);
updateStyleString();
}

/**
* Sets the marker's visibility.
* @param visible
*/
public void setVisible(boolean visible) {
tangramMarker.setVisible(visible);
}

/**
* Sets marker z-axis draw order.
* @param drawOrder
*/
public void setDrawOrder(int drawOrder) {
this.tangramMarker.setDrawOrder(drawOrder);
}

/**
* Sets extra data to be associated with this marker.
* @param userData
*/
public void setUserData(Object userData) {
this.tangramMarker.setUserData(userData);
}

/**
* Gets extra data associated with this marker.
* @return
*/
public Object getUserData() {
return this.tangramMarker.getUserData();
}

/**
* Sets color of marker given a color int ie {@code android.graphics.Color.BLUE}.
* @param colorInt
*/
public void setColor(int colorInt) {
String hex = "#" + Integer.toHexString(colorInt);
styleStringGenerator.setColor(hex);
updateStyleString();
}

/**
* Sets color of marker given a color hex string.
* @param hex
*/
public void setColor(String hex) {
styleStringGenerator.setColor(hex);
updateStyleString();
}

/**
* Sets whether or not marker can be selected.
* @param interactive
*/
public void setInteractive(boolean interactive) {
styleStringGenerator.setInteractive(interactive);
updateStyleString();
}

private void updateStyleString() {
tangramMarker.setStylingFromString(styleStringGenerator.getStyleString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.mapzen.tangram.LngLat;

/**
* Represents a pin marker on a map.
* Represents a pin marker on a map backed by {@link com.mapzen.tangram.MapData} objects.
*/
public class Marker {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapzen.android.graphics.model;

import com.mapzen.android.graphics.internal.StyleStringGenerator;
import com.mapzen.tangram.MapController;
import com.mapzen.tangram.Marker;

Expand Down Expand Up @@ -27,9 +28,15 @@ public MarkerManager(MapController mapController) {
public BitmapMarker addMarker(MarkerOptions markerOptions) {
final Marker marker = mapController.addMarker();
marker.setPoint(markerOptions.getPosition());
marker.setDrawable(markerOptions.getIcon());
marker.setStylingFromString(markerOptions.getStyle());
return new BitmapMarker(this, marker);
if (markerOptions.getIconDrawable() != null) {
marker.setDrawable(markerOptions.getIconDrawable());
} else {
marker.setDrawable(markerOptions.getIcon());
}
StyleStringGenerator styleStringGenerator = new StyleStringGenerator();
styleStringGenerator.setSize(markerOptions.getWidth(), markerOptions.getHeight());
marker.setStylingFromString(styleStringGenerator.getStyleString());
return new BitmapMarker(this, marker, styleStringGenerator);
}

/**
Expand Down
Loading

0 comments on commit 110c7ae

Please sign in to comment.