Location on Google Maps

The Google Maps API is a great choice for a sample application because it provides an easy way to get a base map to view your location. This page will show you how to get access to the base map and provide a custom location source to display your location on the map. If you have a different map visualizer or don’t want to use a map to display location, you can ignore this page.

Register for API Key

In order to access the Google Maps API you need an API Key. If you elected to use the Google Maps Activity, you can simply follow the link provided in app/res/values/google_maps_api.xml. If not, the Maps SDK for Android documentation walks you through the process of creating an API Key.

Custom Location Source

In order to display locations from the NEON Location Service on a Google Map, we need to define a custom location source. This location source will allow us to send locations from the NEON Location Service to the map.

First, implement LocationSource in your activity. This requires an activate() and deactivate() function. Declare a OnLocationChangedListener and set it in activate and clear it in deactivate.

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback, LocationSource, INeonLocationListener, INeonEventListener 
{
    private LocationSource.OnLocationChangedListener locationListener;

    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        Log.i("NEONSampleApp", "location source - activate!");
        locationListener = onLocationChangedListener;
    }

    @Override
    public void deactivate() {
        Log.i("NEONSampleApp", "location source - deactivate!");
        locationListener = null;
    }
}

Finally, set your location source and enable my location in the onMapReady callback.

NOTE: If the targetSdkVersion is above 22, you will be required to ask for run-time location permissions from the user and handle the response. For simplicity, you can set the targetSdkVersion to 22 to avoid this issue.

@Override
public void onMapReady(GoogleMap googleMap) 
{
    mMap = googleMap;

    mMap.setLocationSource(this);
    mMap.setMyLocationEnabled(true);
}

Using a Custom Location Source

To pass NEON locations for display on Google Maps, you can use the toLocation() method provided by the NeonLocation class to automatically convert from the NeonLocation class to the standard location object used in Android. The example below shows a NeonLocation being passed to the custom location listener.

@Override
public void onLocationChanged(NeonLocation neonLocation) 
{
    Log.i("NEONSampleApp", "Got a location: "+neonLocation.toString());

    if (locationListener != null)
        locationListener.onLocationChanged(neonLocation.toLocation());

}