How to Obtain Current Location Using Android Location Manager [Part: 1]

0
Shares
How to Obtain Current Location Using Android Location Manager [Part: 1]
3 (60%) 2 votes

Integrating location feature in your app adds great potential in making your app seem smart to end-users.

And with your end-users location data, you can predict their potential actions, regular places they visit and much more.

This basically helps in a way to understanding your users.

So if you’re developing an app that includes location-based features, then why not make it smarter by automatically finding user’s current location with Android location manager?

This class basically provides access to Google location services. And these services allows Android app to obtain the latest/current device’s location.

In the first part of this tutorial series, we’ll show how to integrate Google maps in your Android location. Then in the next part, we’ll show how to automate the process of obtaining the current location of user’s device.

Let’s Get Started

First of all, you need to make settings in the developer console.

Go to https://console.developers.google.com/ and create a new project.

After creating a new project, create Key by going to Credential-> Create credentials-> APIKEY.

Step 1 Android Location Manager

Step 2 Android Location Manager

Now you can add restrictions if you needed.

Step 3 Android Location Manager
Note: If you want to make it project independent, then no need to add package name and SHA1 key.

Now enable Google Map Android API Service (If not enabled)

Go to Dashboard -> Enable API -> Google Map Android API.

Once you’ve done all the settings in the developer console, open your Android Studio to develop the demo.

Firstly, create a new project.

Now to automatically add the map in the new activity, right click on the package -> New -> Google map activity.

Step 4 Android Location Manager

Next, Setup your key through main -> res -> googlemapapi.xml

Step 5 Android Location Manager

After setting up the above key, go to Android manifest file and add the shown metadata if not added.

Step 6 Android Location Manager

Also, don’t forget to give the necessary permissions.

Step 7 Android Location Manager
Next, open Gradle and check whether the following dependency is added or not in build.gradle of project’s main module.

Step 8 Android Location Manager
Start Code Integration

    • MapsActivity.java
      public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
      private GoogleMap mMap;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_maps);
      // Obtain the SupportMapFragment and get notified when the map is ready to be used.
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
      .findFragmentById(R.id.map);
      mapFragment.getMapAsync(this);
      }
      /**
      * Manipulates the map once available.
      * This callback is triggered when the map is ready to be used.
      * This is where we can add markers or lines, add listeners or move the camera. In this case,
      * we just add a marker near Sydney, Australia.
      * If Google Play services is not installed on the device, the user will be prompted to install
      * it inside the SupportMapFragment. This method will only be triggered once the user has
      * installed Google Play services and returned to the app.
      */
      @Override
      public void onMapReady(GoogleMap googleMap) {
      mMap = googleMap;
      // Add a marker in Sydney and move the camera
      LatLng sydney = new LatLng(-34, 151);
      mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
      mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
      }
      }

 

    • activity_maps
      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/activity_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="com.firebase.pushnotification.MainActivity">
      <TextView
      android:id="@+id/tvNotificationDetails"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Here notification details will be display" />
      </RelativeLayout>

 

Run this demo. It will look like:

Android tutorial google map integration in Android

Congratulations!

You have successfully integrated Map in your Android app.

Now what?

Now, in the second part of this tutorial, we’ll move forward on automating obtaining current location process.

However, if you face any problem implementing, contact our developers for help.

While adding location-based features in your Android apps, do remember that users might have location turned off. This means you have to plan for when any user has location turned off. Still, if you need any expert to help you with building your Android app, you can hire Android app developer from us.

You can directly grab a free copy of Google map v2 demo From Github.

 
0
Shares
 

LET'S TALK VALIDATE YOUR IDEA!