- Opening existing native app
- Native implementation
- WebView implementation
Opening existing native app:
Android Intent class can be used to open existing application by class name. Using Intent.ACTION_VIEW we can open map or browser to show location/directions. Native Google map is faster than web or webview map on Mobile. Problem here may be, map will not be embedded into our custom apps. It will open external an app.
Here is the sample code:
[source language=”java”]
void drawRoute(Location src_location,Location dest_location) {
String geoUriString = “http://maps.google.com/maps?saddr=”
+ src_location.getLatitude() + “,” + src_location.getLongitude()
+ “&daddr=” +dest_location.getLatitude() + “,” + dest_location.getLongitude();
Intent navigation = new Intent(Intent.ACTION_VIEW,
Uri.parse(geoUriString));
navigation.setClassName(“com.google.android.apps.maps”,
“com.google.android.maps.MapsActivity”);
startActivity(navigation);
}
[/source]
Complete snippet is available at https://gist.github.com/sivakumarbdu/4680530#file-googlemap_webview
Native implementation:
When application needs embedded map module, this will be useful. Google provide API to access directions/routes and several other functions.
Also Google provide nice documentation for map implementation at https://developers.google.com/maps/documentation/android/
If you want to try the direct source, please check the snippet available at https://gist.github.com/4680612
WebView implementation
Its very easy way to embedded map into custom apps but performance will be very slow compared to native implementation. We can render maps.google.com to show locations with proper parameter.
Source snipped available https://gist.github.com/4680628
[source language=”java”]
void setupMapView() {
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
void drawRoute(Location location) {
selected_lat = Double.parseDouble(intent.getStringExtra(“lat”));
selected_lon = Double.parseDouble(intent.getStringExtra(“lon”));
ArrayList> all_geo_points = Directions.getDirections(
location.getLatitude(), location.getLongitude(), selected_lat,
selected_lon);
mapOverlays.add(new MyOverlay1(all_geo_points));
controller.setZoom(12);
mapView.invalidate();
}
[/source]
MapQust Map
Other good alternate for Google map is MapQuest. MapQuest can also be implemented as native app or as Webview.
MapQuest Javascript API can be found at http://developer.mapquest.com/web/documentation/sdk/javascript/v7.0 . Implementation of MapQuest is similar to Google map with few differences. Libraries and class names will be from MapQuest jar rather than Google map.
[source language=”java”]
import com.google.android.maps.Overlay;
import com.mapquest.android.maps.DefaultItemizedOverlay;
import com.mapquest.android.maps.GeoPoint;
import com.mapquest.android.maps.ItemizedOverlay;
import com.mapquest.android.maps.MapActivity;
import com.mapquest.android.maps.MapView;
import com.mapquest.android.maps.OverlayItem;
import com.mapquest.android.maps.RouteManager;
import com.mapquest.android.maps.RouteResponse;
import com.mapquest.android.maps.ServiceResponse.Info;
import com.mapquest.android.Geocoder;
[/source]
[source language=”java”]
void setupMapView() {
setContentView(R.layout.map);
map = (MapView) findViewById(R.id.map);
map.setBuiltInZoomControls(true);
}
[/source]
Source snippet available at https://gist.github.com/4680672
Images credits goes to :
http://www.thebiblescholar.com/android_awesome.jpg
http://ecx.images-amazon.com/images/I/31aWEVpfCiL._SL500_AA300_.png
http://corrupteddevelopment.com/wp-content/uploads/2012/02/google-maps-icon.jpg
good comparison. Comparison with one indoor map will also be good.