Basic Android App using Google Maps and Current Location

Applies to:
  • Android Studio (Beta) 0.8.6
  • Google Android Maps API

I am writing an app for a tablet running Android and wanted to include a Google Map. The following exercise focuses solely on creating an app which opens Google Maps at your current location.

I couldn't find any instructions using the tools I had (Android Studio) which is why I've written this article.
  1. Start "Android Studio"

  2. Go to File and create a New Project...

  3. I'm developing on a Tablet so I select Phone and Tablet then a relatively old version of the Android OS:

  4. Add an activity to Mobile: select Google Maps Activity:

  5. Change the Title to what you want (don't change the activity or layout) and click on Finish:

  6. Get yourself a developers' Google Android Maps API Key (not necessarily the same as Google API Key) and enter it as instructed:

  7. Change the Java file to open on your current location, find the file and open it:

  8. Change the function setUpMap() to the following code
        private void setUpMap() {
            mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet"));
            // Enable MyLocation Layer of Google Map
            // Get LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            // Create a criteria object to retrieve provider
            Criteria criteria = new Criteria();
            // Get the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);
            // Get Current Location
            Location myLocation = locationManager.getLastKnownLocation(provider);
            // set map type
            // Get latitude of the current location
            double latitude = myLocation.getLatitude();
            // Get longitude of the current location
            double longitude = myLocation.getLongitude();
            // Create a LatLng object for the current location
            LatLng latLng = new LatLng(latitude, longitude);
            // Show the current location in Google Map
            // Zoom in the Google Map
            mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located"));
    1. If you are prompted to import the classes then agree by clicking OK:

    2. If you are not prompted,
      1. you should see some of your code go red like in the following screenshot:

      2. Focus the cursor on the word and hold down the key and press Enter, then import each class:

  9. Your code will now include the imported classes near the beginning, so TEST it!

Problems animating the camera to your current location? I had to add the following:
LatLng myCoordinates = new LatLng(latitude, longitude);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(myCoordinates, 12);

Additional Additional:
A little more control on the animation?
CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(myCoordinates)      // Sets the center of the map to LatLng (refer to previous snippet)
    .zoom(17)                   // Sets the zoom
    .bearing(90)                // Sets the orientation of the camera to east
    .tilt(30)                   // Sets the tilt of the camera to 30 degrees
    .build();                   // Creates a CameraPosition from the builder

Just in case: AndroidManifest.xml
The example above should have populated this correctly but if you are still getting problems, I have the following fields that I also use related to Google Maps v2:
        android:value="@integer/google_play_services_version" />
        android:value="@string/google_maps_key" />
        android:protectionLevel="signature" />
        android:required="true" />
        android:name="" />
        android:name="" />
        android:name="android.permission.INTERNET" />
        android:name="" />
        android:name="android.permission.ACCESS_COARSE_LOCATION" />
        android:name="android.permission.ACCESS_FINE_LOCATION" />

Last Additional
Ensure that when Google asks for your Android API key, you enter the production key (which may be the same as your testing key); the one from your app and not from your computer (as other people don't use your computer to access the service). If your fellow testers only see a grey map, just double-check the key is the right one given to Google Maps.

Follow On Article/Sequel
Next: Article on separating the map into a sub-activity rather than it being the main activity - Android OS: Add GoogleMap as fragment.

Related Articles

Joes Revolver Map


Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Donate to Joel Lipman via PayPal

Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.