Created
May 12, 2017 12:13
-
-
Save jsjain/c21c4a221602baa5a7b6f38dbb884062 to your computer and use it in GitHub Desktop.
sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2011-2017 HERE Global B.V. and its affiliate(s). | |
* All rights reserved. | |
* The use of this software is conditional upon having a separate agreement | |
* with a HERE company for the use or utilization of this software. In the | |
* absence of such agreement, the use of the software is not allowed. | |
*/ | |
package com.mapstutorial.simplerouting; | |
import android.Manifest; | |
import android.app.Activity; | |
import android.content.pm.PackageManager; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v4.content.ContextCompat; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.View; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.here.android.mpa.common.GeoBoundingBox; | |
import com.here.android.mpa.common.GeoCoordinate; | |
import com.here.android.mpa.common.OnEngineInitListener; | |
import com.here.android.mpa.common.PositioningManager; | |
import com.here.android.mpa.mapping.Map; | |
import com.here.android.mpa.mapping.MapFragment; | |
import com.here.android.mpa.mapping.MapMarker; | |
import com.here.android.mpa.mapping.MapRoute; | |
import com.here.android.mpa.routing.RouteManager; | |
import com.here.android.mpa.routing.RouteOptions; | |
import com.here.android.mpa.routing.RoutePlan; | |
import com.here.android.mpa.routing.RouteResult; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class RoutingActivity extends Activity { | |
private static final String LOG_TAG = RoutingActivity.class.getSimpleName(); | |
// permissions request code | |
private final static int REQUEST_CODE_ASK_PERMISSIONS = 1; | |
/** | |
* Permissions that need to be explicitly requested from end user. | |
*/ | |
private static final String[] REQUIRED_SDK_PERMISSIONS = new String[] { | |
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE }; | |
// map embedded in the map fragment | |
private Map map = null; | |
// map fragment embedded in this activity | |
private MapFragment mapFragment = null; | |
// TextView for displaying the current map scheme | |
private TextView textViewResult = null; | |
// MapRoute for this activity | |
private static MapRoute mapRoute = null; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
checkPermissions(); | |
} | |
private void initialize() { | |
setContentView(R.layout.activity_main); | |
// Search for the map fragment to finish setup by calling init(). | |
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment); | |
mapFragment.init(new OnEngineInitListener() { | |
@Override | |
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) { | |
if (error == OnEngineInitListener.Error.NONE) { | |
// retrieve a reference of the map from the map fragment | |
map = mapFragment.getMap(); | |
// Set the map center coordinate to the Vancouver region (no animation) | |
map.setCenter(new GeoCoordinate(19.174598, 72.860722), | |
Map.Animation.NONE); | |
// Set the map zoom level to the average between min and max (no animation) | |
map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2); | |
MapMarker marker = new MapMarker(); | |
PositioningManager posManager = PositioningManager.getInstance(); | |
posManager.start(PositioningManager.LocationMethod.GPS); | |
TextView info = (TextView) findViewById(R.id.info); | |
boolean temp = posManager.hasValidPosition(PositioningManager.LocationMethod.GPS); | |
info.setText(Boolean.toString(temp)); | |
marker.setCoordinate(new GeoCoordinate(19.1745, 72.8)); | |
map.addMapObject(marker); | |
} else { | |
Log.e(LOG_TAG, "Cannot initialize MapFragment (" + error + ")"); | |
} | |
} | |
}); | |
textViewResult = (TextView) findViewById(R.id.title); | |
textViewResult.setText(R.string.textview_routecoordinates_2waypoints); | |
} | |
/** | |
* Checks the dynamically controlled permissions and requests missing permissions from end user. | |
*/ | |
protected void checkPermissions() { | |
final List<String> missingPermissions = new ArrayList<>(); | |
// check all required dynamic permissions | |
for (final String permission : REQUIRED_SDK_PERMISSIONS) { | |
final int result = ContextCompat.checkSelfPermission(this, permission); | |
if (result != PackageManager.PERMISSION_GRANTED) { | |
missingPermissions.add(permission); | |
} | |
} | |
if (!missingPermissions.isEmpty()) { | |
// request all missing permissions | |
final String[] permissions = missingPermissions | |
.toArray(new String[missingPermissions.size()]); | |
ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE_ASK_PERMISSIONS); | |
} else { | |
final int[] grantResults = new int[REQUIRED_SDK_PERMISSIONS.length]; | |
Arrays.fill(grantResults, PackageManager.PERMISSION_GRANTED); | |
onRequestPermissionsResult(REQUEST_CODE_ASK_PERMISSIONS, REQUIRED_SDK_PERMISSIONS, | |
grantResults); | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], | |
@NonNull int[] grantResults) { | |
switch (requestCode) { | |
case REQUEST_CODE_ASK_PERMISSIONS: | |
for (int index = permissions.length - 1; index >= 0; --index) { | |
if (grantResults[index] != PackageManager.PERMISSION_GRANTED) { | |
// exit the app if one permission is not granted | |
Toast.makeText(this, "Required permission '" + permissions[index] | |
+ "' not granted, exiting", Toast.LENGTH_LONG).show(); | |
finish(); | |
return; | |
} | |
} | |
// all permissions were granted | |
initialize(); | |
break; | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu_routing, menu); | |
return true; | |
} | |
// Functionality for taps of the "Get Directions" button | |
public void getDirections(View view) { | |
// 1. clear previous results | |
textViewResult.setText(""); | |
if (map != null && mapRoute != null) { | |
map.removeMapObject(mapRoute); | |
mapRoute = null; | |
} | |
// 2. Initialize RouteManager | |
RouteManager routeManager = new RouteManager(); | |
// 3. Select routing options | |
RoutePlan routePlan = new RoutePlan(); | |
RouteOptions routeOptions = new RouteOptions(); | |
routeOptions.setTransportMode(RouteOptions.TransportMode.CAR); | |
routeOptions.setRouteType(RouteOptions.Type.FASTEST); | |
routePlan.setRouteOptions(routeOptions); | |
// 4. Select Waypoints for your routes | |
routePlan.addWaypoint(new GeoCoordinate(19.174598, 72.860722)); | |
routePlan.addWaypoint(new GeoCoordinate(19.1747289, 72.7762924)); | |
// 5. Retrieve Routing information via RouteManagerEventListener | |
RouteManager.Error error = routeManager.calculateRoute(routePlan, routeManagerListener); | |
if (error != RouteManager.Error.NONE) { | |
Toast.makeText(getApplicationContext(), | |
"Route calculation failed with: " + error.toString(), Toast.LENGTH_SHORT) | |
.show(); | |
} | |
} | |
private RouteManager.Listener routeManagerListener = new RouteManager.Listener() { | |
public void onCalculateRouteFinished(RouteManager.Error errorCode, | |
List<RouteResult> result) { | |
if (errorCode == RouteManager.Error.NONE && result.get(0).getRoute() != null) { | |
// create a map route object and place it on the map | |
mapRoute = new MapRoute(result.get(0).getRoute()); | |
map.addMapObject(mapRoute); | |
// Get the bounding box containing the route and zoom in (no animation) | |
GeoBoundingBox gbb = result.get(0).getRoute().getBoundingBox(); | |
map.zoomTo(gbb, Map.Animation.NONE, Map.MOVE_PRESERVE_ORIENTATION); | |
textViewResult.setText(String.format(getString(R.string.route_maneuvers), result.get(0).getRoute().getManeuvers().size())); | |
} else { | |
textViewResult.setText( | |
String.format("Route calculation failed: %s", errorCode.toString())); | |
} | |
} | |
public void onProgress(int percentage) { | |
textViewResult.setText(String.format(getString(R.string.precent_done), percentage)); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment