Skip to content Skip to sidebar Skip to footer

Google Maps Android Api V2 Creating A New Locationsource

A LocationSource is defined in Google Maps Android API v2. It is used for googlemap as the location provider. By default, the location source is provided by the gps module on the p

Solution 1:

Here is a simple implementation of LocationSource interface. In my case I'm registering both GPS and Network location providers. As mentioned by @CommonsWare, implementation may very depending on your needs. I would suggest reading official documentation about Location service in order to better understand how to utilize your needs and save some battery power

publicclassCurrentLocationProviderimplementsLocationSource, LocationListener
{
    privateOnLocationChangedListener listener;
    privateLocationManager locationManager;

    publicCurrentLocationProvider(Context context)
    {
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

    @Overridepublicvoidactivate(OnLocationChangedListener listener)
    {
        this.listener = listener;
        LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
        if(gpsProvider != null)
        {
            locationManager.requestLocationUpdates(gpsProvider.getName(), 0, 10, this);
        }

        LocationProvider networkProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);;
        if(networkProvider != null) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60 * 5, 0, this);
        }
    }

    @Overridepublicvoiddeactivate()
    {
        locationManager.removeUpdates(this);
    }

    @OverridepublicvoidonLocationChanged(Location location)
    {
        if(listener != null)
        {
            listener.onLocationChanged(location);
        }
    }

    @OverridepublicvoidonProviderDisabled(String provider)
    {
        // TODO Auto-generated method stub

    }

    @OverridepublicvoidonProviderEnabled(String provider)
    {
        // TODO Auto-generated method stub

    }

    @OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras)
    {
        // TODO Auto-generated method stub

    }
}

And here is how I would use this class:

protectedvoidsetUpMap() {
    //init routine
    .......

    this.map.setLocationSource(new CurrentLocationProvider(this));
    .......       
}

EDIT Please not that this solution is obsolete! You need to use FusedLocationProviderApi in conjunction with GoogleApiClient for tracking current location

Solution 2:

Are there any example out there?

There is not much to the interface, and its implementation is very dependent upon your app.

This sample project implements the LocationSource interface on the main activity:

@Overridepublicvoidactivate(OnLocationChangedListener listener) {
    this.mapLocationListener=listener;
  }

  @Overridepublicvoiddeactivate() {
    this.mapLocationListener=null;
  }

All I do is hold onto the OnLocationChangedListener that we are handed in activate(). Then, when you have a location fix that you wish to feed to the map, call onLocationChanged() on that listener, supplying a Location object (the same Location object you might get back from LocationManager).

Solution 3:

Here is the solution using the FusedLocationProviderApi:

Android: Google Maps location with low battery usage

Post a Comment for "Google Maps Android Api V2 Creating A New Locationsource"