Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Passing the current location to another mapping app

From: Building Mobile Apps with Google Maps Android API v2

Video: Passing the current location to another mapping app

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.

Passing the current location to another mapping app

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.

Show transcript

This video is part of

Image for Building Mobile Apps with Google Maps Android API v2
 
Expand all | Collapse all
  1. 9m 52s
    1. Welcome
      52s
    2. What you should know before watching this course
      3m 50s
    3. Using the exercise files
      5m 10s
  2. 18m 55s
    1. Understanding the Maps API terms of service
      3m 51s
    2. Setting up the developer tools
      3m 33s
    3. Connecting and testing an Android device
      5m 45s
    4. Using the SDK documentation
      2m 22s
    5. Demonstrating the finished apps
      3m 24s
  3. 33m 49s
    1. Adding required permissions to a project
      6m 53s
    2. Getting a Google Maps API key
      8m 9s
    3. Importing and linking the Google Play services library
      3m 46s
    4. Checking the device for the Google Play services APK
      9m 4s
    5. Displaying your first map
      5m 57s
  4. 51m 13s
    1. Displaying a map with MapView
      6m 36s
    2. Getting a reference to the map object
      5m 35s
    3. Setting a map's initial state in the layout
      6m 26s
    4. Setting a map's initial state programmatically
      6m 42s
    5. Geocoding an address to get its coordinates
      8m 10s
    6. Changing between different types of map objects
      4m 23s
    7. Maintaining map state between activity restarts
      13m 21s
  5. 21m 3s
    1. Enabling the My Location button
      6m 26s
    2. Getting the current location programatically
      8m 16s
    3. Listening for current location updates
      6m 21s
  6. 38m 47s
    1. Adding map markers
      4m 52s
    2. Removing map markers
      4m 5s
    3. Customizing marker icon graphics
      4m 55s
    4. Displaying default info windows
      1m 53s
    5. Customizing marker info windows
      8m 49s
    6. Handling marker events
      7m 59s
    7. Dragging and dropping markers
      6m 14s
  7. 24m 54s
    1. Drawing lines
      8m 48s
    2. Drawing polygons
      8m 3s
    3. Drawing circles
      8m 3s
  8. 31m 21s
    1. Reviewing the tour finder application
      4m 19s
    2. Adding a map fragment to an activity
      11m 54s
    3. Passing the current location to another mapping app
      7m 31s
    4. Displaying Google Play services license terms
      5m 2s
    5. Preparing the app for distribution
      2m 35s
  9. 28s
    1. Next steps
      28s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Building Mobile Apps with Google Maps Android API v2.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.