Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class BasicMapActivity extends Activity {
// map embedded in the map fragment
private Map map = null;
// map fragment embedded in this activity
private MapFragment mapFragment = null;
private PositioningManager posManager;
// markers and containers
private MapContainer placesContainer = null;
private MapMarker selectedMapMarker = null;
private MapRoute mapRoute = null;
private float range;
// Create a gesture listener and add it to the MapFragment
MapGesture.OnGestureListener listener =
new MapGesture.OnGestureListener.OnGestureListenerAdapter() {
@Override
public boolean onMapObjectsSelected(List<ViewObject> objects) {
// There are various types of map objects, but we only want
// to handle the MapMarkers we have added
for (ViewObject viewObj : objects) {
if (viewObj.getBaseType() == ViewObject.Type.USER_OBJECT) {
if (((MapObject) viewObj).getType() == MapObject.Type.MARKER) {
// save the selected marker to use during route calculation
selectedMapMarker = ((MapMarker) viewObj);
// Create the RoutePlan and add two waypoints
RoutePlan routePlan = new RoutePlan();
// Use our current position as the first waypoint
routePlan.addWaypoint(new GeoCoordinate(
posManager.getPosition().getCoordinate()));
// Use the marker's position as the second waypoint
routePlan.addWaypoint(new GeoCoordinate(
((MapMarker) viewObj).getCoordinate()));
// Create RouteOptions and set to fastest & pedestrian mode
RouteOptions routeOptions = new RouteOptions();
routeOptions.setTransportMode(RouteOptions.TransportMode.PEDESTRIAN);
routeOptions.setRouteType(RouteOptions.Type.SHORTEST);
routePlan.setRouteOptions(routeOptions);
// Create a RouteManager and calculate the route
RouteManager rm = new RouteManager();
rm.calculateRoute(routePlan, new RouteListener());
// Remove all other markers from the map
for (MapObject mapObject : placesContainer.getAllMapObjects()) {
if (!mapObject.equals(viewObj)) {
placesContainer.removeMapObject(mapObject);
}
}
// If user has tapped multiple markers, just display one route
break;
}
}
}
// return false to allow the map to handle this callback also
return false;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapFragment = (MapFragment) getFragmentManager().findFragmentById(
R.id.mapfragment);
mapFragment.init(new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
mapFragment.getMapGesture().addOnGestureListener(listener);
onMapFragmentInitializationCompleted();
} else {
System.out.println("ERROR: Cannot initialize Map Fragment: " + error);
}
}
});
}
private void onMapFragmentInitializationCompleted() {
// retrieve a reference of the map from the map fragment
map = mapFragment.getMap();
// start the position manager
posManager = PositioningManager.getInstance();
posManager.start(PositioningManager.LocationMethod.GPS_NETWORK);
// Set a pedestrian friendly map scheme
map.setMapScheme(Map.Scheme.PEDESTRIAN_DAY);
// Display position indicator
map.getPositionIndicator().setVisible(true);
placesContainer = new MapContainer();
map.addMapObject(placesContainer);
// Set the map center coordinate to the current position
map.setCenter(posManager.getPosition().getCoordinate(), Map.Animation.NONE);
map.setZoomLevel(14);
}
public void findPlaces(View view) {
// clear the map
if (mapRoute != null) {
map.removeMapObject(mapRoute);
mapRoute = null;
}
placesContainer.removeAllMapObjects();
// collect data to set options
GeoCoordinate myLocation = posManager.getPosition().getCoordinate();
EditText editSteps = (EditText) findViewById(R.id.editSteps);
int noOfSteps;
try {
noOfSteps = Integer.valueOf(editSteps.getText().toString());
} catch (NumberFormatException e) {
// if input is not a number set to default of 2500
noOfSteps = 2500;
}
range = ((noOfSteps * 0.762f) / 2);
// create an exploreRequest and set options
ExploreRequest request = new ExploreRequest();
request.setSearchArea(myLocation, (int) range);
request.setCollectionSize(50);
request.setCategoryFilter(new CategoryFilter().add(Category.Global.SIGHTS_MUSEUMS));
try {
ErrorCode error = request.execute(new SearchRequestListener());
if (error != ErrorCode.NONE) {
// Handle request error
}
} catch (IllegalArgumentException ex) {
// Handle invalid create search request parameters
}
}
// search request listener
class SearchRequestListener implements ResultListener<DiscoveryResultPage> {
@Override
public void onCompleted(DiscoveryResultPage data, ErrorCode error) {
if (error != ErrorCode.NONE) {
// Handle error
} else {
// results can be of different types
// we are only interested in PlaceLinks
List<PlaceLink> results = data.getPlaceLinks();
if (results.size() > 0) {
for (PlaceLink result : results) {
// get all results that are far away enough to be a good candidate
if (result.getDistance() < range && result.getDistance() > (range * 0.7f)) {
GeoCoordinate c = result.getPosition();
com.here.android.mpa.common.Image img =
new com.here.android.mpa.common.Image();
try {
img.setImageAsset("pin.png");
} catch (IOException e) {
// handle exception
}
MapMarker marker = new MapMarker(c, img);
marker.setTitle(result.getTitle());
// using a container to group the markers
placesContainer.addMapObject(marker);
}
}
} else {
// handle empty result case}
}
}
}
}
private class RouteListener implements RouteManager.Listener {
public void onProgress(int percentage) {
// You can use this to display the progress of the route calculation
}
public void onCalculateRouteFinished(RouteManager.Error error, List<RouteResult> routeResult) {
// If the route was calculated successfully
if (error == RouteManager.Error.NONE) {
// Render the route on the map
mapRoute = new MapRoute(routeResult.get(0).getRoute());
map.addMapObject(mapRoute);
int routeLength = routeResult.get(0).getRoute().getLength();
float steps = 2 * (routeLength / 0.8f);
String title = selectedMapMarker.getTitle();
title = String.format(Locale.ENGLISH, "It's %d steps to %s and back!",
((int) steps), title);
selectedMapMarker.setTitle(title);
selectedMapMarker.showInfoBubble();
} else {
// Display a message indicating route calculation failure
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.