Skip to content

Commit

Permalink
Merge pull request #88 from mapzen/add-routing-overlay-methods
Browse files Browse the repository at this point in the history
Add routing overlay methods
  • Loading branch information
ecgreb committed May 13, 2016
2 parents d4651f9 + 7747dc7 commit bea835f
Show file tree
Hide file tree
Showing 15 changed files with 941 additions and 4 deletions.
121 changes: 120 additions & 1 deletion library/src/main/java/com/mapzen/android/MapzenMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.mapzen.tangram.MapData;
import com.mapzen.tangram.TouchInput;


import java.util.HashMap;
import java.util.List;

/**
* This is the main class of the Mapzen Android API and is the entry point for all methods related
Expand All @@ -20,6 +20,7 @@
* {@link MapView#getMapAsync(OnMapReadyCallback)}.
*/
public class MapzenMap {

private final MapController mapController;
private final OverlayManager overlayManager;

Expand Down Expand Up @@ -394,4 +395,122 @@ public boolean isSimultaneousGestureAllowed(TouchInput.Gestures first,
TouchInput.Gestures second) {
return mapController.isSimultaneousGestureAllowed(first, second);
}

/**
* Draws two pins on the map. The start pin is active and the end pin is inactive.
* @param start
* @param end
*/
public void drawRoutePins(LngLat start, LngLat end) {
overlayManager.drawRoutePins(start, end);
}

/**
* Clears the start and end pins from the map.
*/
public void clearRoutePins() {
overlayManager.clearRoutePins();
}

/**
* Draws a dropped pin on the map at the point supplied.
* @param point
*/
public void drawDroppedPin(LngLat point) {
overlayManager.drawDroppedPin(point);
}

/**
* Clears the dropped pin from the map.
*/
public void clearDroppedPins() {
overlayManager.clearDroppedPin();
}

/**
* Draws a search result on the map at the point supplied. The pin will be active.
* @param point
*/
public void drawSearchResult(LngLat point) {
drawSearchResult(point, true);
}

/**
* Draws a search result on the map at the point supplied.
* @param point
* @param active
*/
public void drawSearchResult(LngLat point, boolean active) {
overlayManager.drawSearchResult(point, active);
}

/**
* Draws search results on the map. All pins are displayed as active.
* @param points
*/
public void drawSearchResults(List<LngLat> points) {
int index = 0;
for (LngLat point : points) {
overlayManager.drawSearchResult(point, true, index);
index++;
}
}

/**
* Draws search results on the map. All pins will be inactive except for the one at the active
* index supplied.
* @param points
* @param activeIndexes
*/
public void drawSearchResults(List<LngLat> points, int... activeIndexes) {
int index = 0;
for (LngLat point : points) {
boolean status = false;
for (int i = 0; i < activeIndexes.length; i++) {
if (activeIndexes[i] == index) {
status = true;
break;
}
}
overlayManager.drawSearchResult(point, status, index);
index++;
}
}

/**
* Clears search results from the map.
*/
public void clearSearchResults() {
overlayManager.clearSearchResult();
}

/**
* Draws route pin at the point supplied.
* @param point
*/
public void drawRouteLocationMarker(LngLat point) {
overlayManager.drawRouteLocationMarker(point);
}

/**
* Clears route pin from the map.
*/
public void clearRouteLocationMarker() {
overlayManager.clearRouteLocationMarker();
}

/**
* Draws route line on the map for the points supplied.
* @param points
*/
public void drawRouteLine(List<LngLat> points) {
overlayManager.drawRouteLine(points);
}

/**
* Clears route line from the map.
*/
public void clearRouteLine() {
overlayManager.clearRouteLine();
}
}
168 changes: 166 additions & 2 deletions library/src/main/java/com/mapzen/android/OverlayManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.widget.ImageButton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
Expand All @@ -26,13 +27,31 @@ public class OverlayManager {
private static final int LOCATION_REQUEST_INTERVAL_MILLIS = 5000;
private static final int LOCATION_REQUEST_DISPLACEMENT_MILLIS = 5000;
private static final int ANIMATION_DURATION_MILLIS = 300;

private static final String NAME_CURRENT_LOCATION = "mz_current_location";
private static final String NAME_POLYLINE = "mz_default_line";
private static final String NAME_POLYGON = "mz_default_polygon";
private static final String NAME_MARKER = "mz_default_point";
private static final String NAME_START_PIN = "mz_route_start";
private static final String NAME_END_PIN = "mz_route_stop";
private static final String NAME_DROPPED_PIN = "mz_dropped_pin";
private static final String NAME_SEARCH_RESULT_PIN = "mz_search_result";
private static final String NAME_ROUTE_PIN = "mz_route_location";
private static final String NAME_ROUTE_LINE = "mz_route_line";

private static final String PROP_STATE = "state";
private static final String PROP_STATE_ACTIVE = "active";
private static final String PROP_STATE_INACTIVE = "inactive";
private static final String PROP_SEARCH_INDEX = "searchIndex";
private static final String PROP_TYPE = "type";
private static final String PROP_POINT = "point";
private static final String PROP_LINE = "line";

private static final int MIN_COORDINATES_POLYGON = 2;
private static final int MIN_COORDINATES_POLYLINE = 2;

private static final int INDEX_NONE = -1;

/**
* For interaction with the map.
*/
Expand Down Expand Up @@ -69,10 +88,15 @@ public class OverlayManager {
};

private MapData polylineMapData;

private MapData polygonMapData;

private MapData markerMapData;
private MapData startPinData;
private MapData endPinData;
private MapData droppedPinData;
private MapData searchResultPinData;
private MapData routePinData;
private MapData routeLineData;

/**
* Create a new {@link OverlayManager} object for handling functionality between map and
* location services using the {@link LocationFactory}'s shared {@link LostApiClient}.
Expand Down Expand Up @@ -183,6 +207,146 @@ public MapData addMarker(Marker marker) {
return addPointToMarkerMapData(marker);
}

/**
* Draws two pins on the map. The start pin is active and the end pin is inactive.
* @param start
* @param end
*/
public void drawRoutePins(LngLat start, LngLat end) {
if (startPinData == null) {
startPinData = mapController.addDataLayer(NAME_START_PIN);
}
if (endPinData == null) {
endPinData = mapController.addDataLayer(NAME_END_PIN);
}
startPinData.addPoint(start, null);
endPinData.addPoint(end, null);
mapController.requestRender();
}

/**
* Clears the start and end pins from the map.
*/
public void clearRoutePins() {
if (startPinData != null) {
startPinData.clear();
}
if (endPinData != null) {
endPinData.clear();
}
}

/**
* Draws a dropped pin on the map at the point supplied.
* @param point
*/
public void drawDroppedPin(LngLat point) {
if (droppedPinData == null) {
droppedPinData = mapController.addDataLayer(NAME_DROPPED_PIN);
}
HashMap<String, String> properties = new HashMap<>();
properties.put(PROP_STATE, PROP_STATE_ACTIVE);
droppedPinData.addPoint(point, properties);
mapController.requestRender();
}

/**
* Clears the dropped pin from the map.
*/
public void clearDroppedPin() {
if (droppedPinData != null) {
droppedPinData.clear();
}
}

/**
* Draws a search result on the map at the point supplied.
* @param point
* @param active
*/
public void drawSearchResult(LngLat point, boolean active) {
drawSearchResult(point, active, INDEX_NONE);
}

/**
* Draws a search result at the point supplied and displays it as active/inactive. If an index
* is supplied, it adds property {@code PROP_SEARCH_INDEX} when adding it to the map.
* @param point
* @param active
* @param index
*/
public void drawSearchResult(LngLat point, boolean active, int index) {
if (searchResultPinData == null) {
searchResultPinData = mapController.addDataLayer(NAME_SEARCH_RESULT_PIN);
}
HashMap<String, String> properties = new HashMap<>();
if (index != INDEX_NONE) {
properties.put(PROP_SEARCH_INDEX, String.valueOf(index));
}
if (active) {
properties.put(PROP_STATE, PROP_STATE_ACTIVE);
} else {
properties.put(PROP_STATE, PROP_STATE_INACTIVE);
}
searchResultPinData.addPoint(point, properties);
mapController.requestRender();
}

/**
* Clears search result from the map.
*/
public void clearSearchResult() {
if (searchResultPinData != null) {
searchResultPinData.clear();
}
}

/**
* Draws route pin at the point supplied.
* @param point
*/
public void drawRouteLocationMarker(LngLat point) {
if (routePinData == null) {
routePinData = mapController.addDataLayer(NAME_ROUTE_PIN);
}
HashMap<String, String> properties = new HashMap<>();
properties.put(PROP_TYPE, PROP_POINT);
routePinData.addPoint(point, properties);
mapController.requestRender();
}

/**
* Clears route pin from the map.
*/
public void clearRouteLocationMarker() {
if (routePinData != null) {
routePinData.clear();
}
}

/**
* Draws route line on the map for the points supplied.
* @param points
*/
public void drawRouteLine(List<LngLat> points) {
if (routeLineData == null) {
routeLineData = mapController.addDataLayer(NAME_ROUTE_LINE);
}
HashMap<String, String> properties = new HashMap<>();
properties.put(PROP_TYPE, PROP_LINE);
routeLineData.addPolyline(points, properties);
mapController.requestRender();
}

/**
* Clears route line from the map.
*/
public void clearRouteLine() {
if (routeLineData != null) {
routeLineData.clear();
}
}

private void initCurrentLocationMapData() {
currentLocationMapData = mapController.addDataLayer(NAME_CURRENT_LOCATION);
}
Expand Down
Loading

0 comments on commit bea835f

Please sign in to comment.