Finding Location : Google Play Location Service or Android platform location APIs

后端 未结 2 552
礼貌的吻别
礼貌的吻别 2020-12-25 09:36

i am trying to get the users location for my new navigation app. i want to check the users location frequently and it has to be accurate . I use the following code from samp

2条回答
  •  生来不讨喜
    2020-12-25 10:11

    When I was working with it, I came across 2 good sources:

    1. The blog post: Android Location Fused Provider. It includes a great complete tutorial on using the FusedLocationProviderApi to get location in Android.

    2. Answers on following Android LocationClient class is deprecated but used in documentation question.

    Basically both talk about the similar steps as mentioned in the Google I/O session, link to which is mentioned in one of the answers:

    • First, to get a GoogleClientApi instance, like:

      GoogleClientApi googleApiClient = new GoogleApiClient.Builder(locationActivity)
              .addApi(LocationServices.API)
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
              .build();
      
    • Then to call .connect() on it like:

      googleApiClient.connect();
      
    • Then in the callback onConnected, get the last location or even can request the location updates as per your need, eg:

      LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
      

      If you want request location updates, then can use the requestLocationUpdates call.

    Hope it helps.

提交回复
热议问题