Google’s fused location API for Android

The Fused Location Provider intelligently manages the underlying location technology and gives us the best location according to our needs.

WHY USE?

We could choose one of the location providers (network or GPS) and request location updates or set up proximity alert.But there were two main issues with this approach:

1. In case we need to define precise location, we had to switch between network and GPS location providers (as GPS doesn’t work indoors).

2. Proximity alerts were used to notify a user about proximity to a location, and this took its toll on the battery life.

ADVANTAGE OF USING THIS API

1. Simple APIs: Lets us specify high-level needs like “high accuracy” or “low power”, instead of having to worry about location providers.

2. Immediately available: Gives our apps immediate access to the best, most recent location.

3. Power-efficiency: Minimizes out app’s use of power. Based on all incoming location requests and available sensors, fused location provider chooses the most efficient way to meet those needs.

4. Versatility: Meets a wide range of needs, from foreground uses that need highly accurate location to background uses that need periodic location updates with negligible power impact.

Note : Make sure Google Play services is properly installed and working in our device. Please don’t test this location api in emulator because this api is not working in the emulator.

WAY TO USE?

1. add Google Play services (android sdk -> extras -> google – > google_play_service -> libproject) Library Project as a library in our application.

2. Declare the permission in the manifest file.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Android has two location permissions, ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION. The permission we choose affects the accuracy of the location updates we receive. For example, If we request only coarse location permission, Location Services obfuscates the updated location to an accuracy that’s roughly equivalent to a city block.

Requesting ACCESS_FINE_LOCATION implies a request for ACCESS_COARSE_LOCATION.

3. There is two main class which are most important in this server

3.1 com.google.android.gms.location.LocationClient : Stores the current instantiation of the location client.

3.2 com.google.android.gms.location.LocationRequest : A data object that contains quality of service parameters for requests to the LocationClient.

4.Location service callbacks : Before we request location updates, we must first implement the interfaces that Location Services uses to communicate connection status to our app:

4.1 com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks : Specifies methods that Location Services calls when a location client is connected or disconnected.

new  GooglePlayServicesClient.ConnectionCallbacks() {

      @Override

      public void onDisconnected() {

        // TODO Auto-generated method stub

      }

      @Override

      public void onConnected(Bundle arg0) {

        // TODO Auto-generated method stub

      }

    };

4.2com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener : Specifies a method that Location Services calls if an error occurs while attempting to connect the location client.

new GooglePlayServicesClient.OnConnectionFailedListener() {

      @Override

      public void onConnectionFailed(ConnectionResult arg0) {

        // TODO Auto-generated method stub

      }

    };

HOW TO USE

1. Connecting LocationClient to Google api. After connecting locationClient, we are able to get location.

2. Retrieving the current location

3. Retrieving the location update at particular time interval or particular distance or both.

1. Connecting LocationClient to Google api :

// Create a new global location parameters object
LocationRequest mLocationRequest = LocationRequest.create();
//Set the update interval
mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);
 // Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

/*
* LocationClient(arg1, arg2 , arg3)
* arg1 :Context
* arg2 : ConnectionCallbacks
* arg3 :OnConnectionFailedListener
*/
LocationClient  mLocationClient = new LocationClient(arg1, arg2 , arg3);
mLocationClient.connect();

// Note: connect() method will take some time. So for getting the current location or getting
//location update at particular inteval , we have to wait for the connectin established.

During the Google IO presentation, a chart was presented showing effect of different priorities of the recognition algorithm as tested multiple times on Galaxy Nexus.

Priority Typical location update interval Battery drain per hour (%) Accuracy
HIGH_ACCURACY 5 seconds 7.25% ~10 meters
BALANCED_POWER 20 seconds 0.6% ~40 meters
NO_POWER N/A small ~1 mile

2. Retrieving the current location :

Location myLocation = mLocationClient.getLastLocation();
/*
* Now MyLocation object has all infromation like  latitude,longitude etc.
*/

The return value is the best, most recent location, based on the permissions our app requested and the currently-enabled location sensors.

Before we create the location client, implement the interfaces that Location Services uses to communicate with our app:

Reference : https://developer.android.com/training/location/retrieve-current.html

3. Retrieving Location Updates : To get periodic location updates from Location Services, we send a request using a location client. Depending on the form of the request, Location Services either invokes a callback method and passes in a Location object, or issues an Intent that contains the location in its extended data. The accuracy and frequency of the updates are affected by the location permissions we’ve requested and the parameters we pass to Location Services with the request.

LocationListener listener =  new LocationListener() {

      @Override
      public void onLocationChanged(Location location) {
    	// Report to the UI that the location was updated
  String msg = "Updated Location: " +
    Double.toString(location.getLatitude()) + "," +
    Double.toString(location.getLongitude());
  Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();

      }
    };

mLocationClient.requestLocatonUpdates(mLocationRequest,listener);

Reference : https://developer.android.com/training/location/receive-location-updates.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值