Get Coordinates from the Map

The final way to get the GPS coordinates of your device’s location is through the map object. The map has built-in methods that access the sensors without writing any code to access the sensors. This makes things easier because all sensor management is handled by the map. The drawback is that you must display a map in the layout to use these features.

Return to activity_contact_maps.xml and delete all the widgets you put in the layout except the navbar. Be sure to leave the root RelativeLayout. Next, add the MapFragment. The MapFragment is not available in the Palette, so it must be added through XML. Refer to Listing 7.6 to do this.

Listing 7.6 MapFragment XML

<fragment    android:id= ...

Get Learning Mobile App Development: A Hands-on Guide to Building Apps with iOS and Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.