Sometimes it makes perfect sense for an app to have map based functionality, and on Android the usual way to proceed is to use the Google Maps api library and embed a MapView directly into your app. This is fine If the mapping functionality the app requires is relatively simple, but what if you require more complex functionality like directions and route plotting? While these requirements are possible with the library there is quite a bit plumbing that needs to be undertaken.
So what else can you do? Well what better way to handle mapping functionality than to offload the work to an app designed specifically for the task a.k.a Google Maps. With a few lines of code you can have Google Maps load with a marker on a specific location, with the added benefits of giving users all the functionality that Google Maps contains.
So how do we do it?
String uri = "http://maps.google.com/maps?q=%s"; Intent mapIntent = new Intent( Intent.ACTION_VIEW, Uri.parse(String.format(Uri, address)); startActivity(mapIntent);
Want to load google maps with a path between a start address and a destination? The following code gets this done quickly and easily.
String uri = "http://maps.google.com/maps?saddr=%s&daddr=%s" Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse(String.format(uri, startAddr, destinationAddr)); intent.setData(Uri.parse(directions));
You can even omit the starting address to use the current location as the starting point
String uri = "http://maps.google.com/maps?daddr=%s" Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse(String.format(uri, destinationAddr)); intent.setData(Uri.parse(directions));
Like everything there are a couple of drawbacks with this method that need to be weight up. If there are multiple apps on the users device that can handle the intent they will be displayed in a list possible options. This means that Google Maps may not be the application the user ultimately chooses and assumptions may be broken. The second issue is that the user will be taken away from your app and pushed to an app developed by somebody else. While this may not be a major issue, as they will be brought back to the original app when they hit the back button, it’s something to keep in mind.