Before going through this project ready tutorial , go through the previous tutorial for a compass app , Done ?? Now we can continue with this app tutorial Which is To create Geolocation and navigation app .

Geolocation and navigation apps are popular in all mobile platforms. Considering this, most mobile devices especially smartphones include components called GPS receivers. These receivers take microwave band radio signals from global positioning satellites that move in specified orbits around the earth. These GPS signals are extremely weak but thanks to the electronics tech, amplifier and processing circuits in smartphones can utilize these signals for location services.

Anyway, let‟s start developing our 5th app: Show My Location.you‟ll learn to use maps and geolocation data from GPS in your apps. It sounds easy but there are some confusing tricks to use the GPS receiver; don‟t worry I‟ll show all of them in a while.

Let‟s start with creating a new Android project and select Google Maps Activity as the activity type a shown in Figure. When we select the Google Maps Activity, the main Java file and the xml layout file of the project are named as MapsActivity.java and activity_maps.xml automatically as in Figure.

When a Google Maps App project is created, a file named google_maps_api.xml is generated and placed under the res –> values folder as shown in Figure

Creating and Adding an Api Key

We have to enter the api key to the place indicated by YOUR_KEY_HERE in the file above. So, where should we obtain this key? We just need to go to the website indicated in our google_maps_api.xml file which starts with https://console.developers . When we navigate to this site, we need to select Create a new project and hit Continue as follows:

In the next dialog, please hit the Create API Key:

Google console will then display the generated api key as shown below:

Please copy the generated api key and paste it to the place indicated in the google_maps_api.xml file:

Please note that you need to generate and paste your own key otherwise your app won‟t work.

The Default MapsActivity.java File

Let‟s now view the automatically generated MapsActivity.java file where we‟ll write the main code:

In this code, mapFragment.getMapAsync(this) line adds the Map component to the app. Then a LatLng object, which holds the latitude and longitude data is created inside the onMapReady() method. The code line LatLng sydney = new LatLng(-34, 151) declares a LatLng object at the latitude and longitude of -34 and 151, which is the coordinates of Sydney, Australia (please note that this point is automatically chosen by Android Studio). Then, a marker on Sydney is placed by the addMarker() method which is applied on the map object. And in the last line, the camera is moved to this point by the moveCamera() method.

Running the Maps App for the First Time

Please hit Run in Android Studio and then you should see our app in the emulator as shown in Figure.If you see the map with the marker, congratulations. If you cannot see the map, please check the api key section above

Implementing the Required Callbacks

We now need to take data from the GPS receiver and then show our current location on the map rather than the default marker.

Populating the Implemented Methods

The complete MapsActivity.java file is shown in Code

Adding the Required Permissions to the Manifest File

The AndroidManifest.xml file with the required permissions is also given in Code

Running Our App and Sending Custom Coordinates to the Emulator

Please hit the Run button in Android Studio to run our Show My Location app. You can run the app in an emulator or on an actual device.

It is worth noting that I‟ve tried the app on a real device and it works as expected.

Leave a Reply

Your email address will not be published.