Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,987 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
In my tour-finding app, I'm now able to display a list of available tours, and the user can select a tour and see the details including a live interactive map. I'd love for my application to do a lot more, but let's imagine that I have limited resources, and I'd recognize that there are other apps that are on the user's device that do a lot for them already. Such as the full version of Google Maps. You can make a decision to simply pass the location to the full version of Google Maps and let the user get point to point directions, find related attractions, and do all the other things that Google Maps does so well.
And not try to implement everything in your app. And I'll show you how to pass that information over in this project TourFinderPassLocation. In the app's current state there's a menu item that appears on the action bar. It's labelled "More Details". When the user touches it, right now nothing happens. But there is code in the app right now, that's handling that event. It's in the method onOptionsItemSelected in the class tour detail activity.
In this class, I'm looking at the item ids of any selected items. And I have two. The home item is the items that's pressed when the user touches the launcher icon. That would take them back to the main activity. This is the ID for that action bar item I just showed you. It's called menu_goto_action. And all it's doing is calling this empty method, sendToActionIntent. Here's what we're going to do, I want to take the location that the user has selected.
And that will be described in the Tour object's latitude and longitude. And I want to pass it over to Google Maps, or any other mapping application that's on the device. I'll do this with something called an action intent. The code looks like this. First, I'm going to create a StringBuilder object. I'm going to put together a string that represents a URI, or a uniform resource identifier. I'll instantiate it with new, string builder, and I'll pass in, geo colon.
The URI /g, is going to start with this prefix, and that will be a trigger to the Android operating system, that says, go find another app, that knows how to handle this URI /g. Here's how you structure the URI. I'm going to call the string builder object a pen method a few times. First, I'll pass in the latitude of the current tool. That's to clear it as a field of this class. So I'll call mTour.getLatitutude.
Next, I'll append a simple comma, then i'll append the longitude. So at this point, the whole uri starts with the geo prefix then has the latitude, a comma, and the longitude. And finally, I'll also append a zoom level. I'll call uri.append, and I'll pass in a ?z equals ten. That means, pass in a parameter in the URI, where the parameter name is z, and the value is ten, and that sets a zoom level of ten.
Now I'm ready to send the location to another mapping application. I'll create a new intent. I'll name it Intent, and I'll construct it by passing in the following parameters. First I'll pass in a constant of the intent class. Intent.ACTION_VIEW. When you wrap an action view in an intent, you're saying, I don't know what app I want to handle this but I want it to be an app that's registered for this particular u r i, in this case, starting with geo, a mapping app.
Then, I'll call the uri class from android.net and I'll call a method called "pars", and I'll pass in the string that I just built using the expression uri.toString. So now I have my intent, and I'm ready to start it. And that will be a very conventional call to the start activity method of the current activity and I'll pass in my intent object. So let's see what happens. I'll run the application, I'll select an item from the list, I'll see the map appear down here.
But then to jump to that location in Google maps, I'll touch the more details action bar item. Because I have both Google Earth, and Maps installed on this device, I'm prompted to choose one. I'll choose maps, and select, just once. And that takes me to Google Maps, and shows one country. When I touch the back button, I return to my application, and I'll choose another item, and select more details, and this time, I'll choose Google Earth.
And I'm always choosing just once so I can easily switch back and forth between the apps. And there's the location I selected. Here's one more thing that you can do with these URIs, you could also pass in a query value and when you go into the maps app the query will be executed. I'll add a little bit of conditional code here, and I will ask the question. "Does my tool object have a valid marked text value"? The expression will look like this, if not m tour.get marker text.equals, and i'll pass in a blank string.
If that expression is true, that means I have a none empty string for the marker text, and I'll pass it to Google maps. I'll start with my URI string and I'll append some more text. I'll start with and q equals. That means I'm adding another parameter to the URI. And I'll append to that, the string of the marker text, but I'll encode it so that it works in a URI. I'll use the Java class, URL encoder.
And I'll call it encode method. And pass in .getMarkerText. I get a deprecation warning. But I know that this will work. So I'm going to suppress it. And now I'm ready to test again. I'll run the app. And when it relaunches, I'll go to my Week Of Wine Tour again. And I'll touch on More Details. I'll select Maps.
That takes me to Google Maps just as before, but now because I passed in a query, I see a marker appear, and I can now use point to point directions, or any of the other powerful capabilities of the full Google Maps app. When you pass in a query, the user will need to press their back button twice to return to your application. From that point, you can go back to the list and choose another item. Go to more details, select an app and once again you're passing the location to the full version of Google Maps.
Note: An Android device with Android 3.0 (Honeycomb) or higher, and that has Google Play Store installed, is required to use the course exercises. (Kindle Fire and Nook devices do not qualify.) Finished mapping apps can be deployed on Android 2.2 (Froyo) or later.
- Setting up the developer tools
- Adding required permissions
- Getting a Google Maps API key
- Importing and linking the Google Play services
- Setting a map's initial state
- Geocoding an address
- Getting the current location programmatically
- Adding map markers
- Handling marker events
- Drawing lines, polygons, and circles
- Adding maps to existing apps