Android Tutorial Google Maps with FusedLocationProvider

Discussion in 'Tutorials & Examples' started by Erel, Sep 13, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Google Maps uses the GPS as its location provider. It will not show the current location if GPS is disabled.
    It is possible to switch to a custom location provider.

    For example, here is me traveling in Antarctica:


    The attached example uses FusedLocationProvider to inject the location.

    The steps to create a custom location source are:

    1. Create the LocationSource object with JavaObject.
    2. Handle the LocationSource_Event. It will raise when the source is activated.
    3. Set the map source by calling setLocationSource.
    4. Call LocationChangedListener.onLocationChanged method to inject locations.

    These steps are done in the starter service.

    You need to open the manifest editor and set your api key.

    Attached Files:

  2. BillMeyer

    BillMeyer Active Member Licensed User

    @Erel - and you are doing all this from Antarctica - impressive - Thank You !! - Just don't freeze - we need you !!
  3. peacemaker

    peacemaker Well-Known Member Licensed User

    Thanks, Erel !
    So, if GPS is disabled - it's needed to set another LocationSource with PRIORITY_BALANCED_POWER_ACCURACY ?
    Last edited: Sep 13, 2017
  4. Erel

    Erel Administrator Staff Member Licensed User

    I expect that FusedLocationProvider will do it automatically. Try it.
  5. peacemaker

    peacemaker Well-Known Member Licensed User

    Tried. And do not understand now what we can get from Google Map - gmap.MyLocation.IsInitialized is anyway false.
    FLP_LocationChanged occurs only if GPS is allowed.
    Map is visible, but no current position anyway :(

    How to setup the code sample to be in Antarctica ? :)
    Last edited: Sep 13, 2017
  6. Erel

    Erel Administrator Staff Member Licensed User

    Works properly here with the location mode set to battery saving.

    Maybe your device doesn't know its location.

    Go over the starter service code. See how it updates the location.
    Last edited: Sep 13, 2017
  7. eelias

    eelias Active Member Licensed User

    based on this, can i avoid to have the COARSE_LOCATION and FINE_LOCATION in manifest?

    today I have

    on manifest, the reason is that my app will be incompatible with some tablet if I declare this permission.

    However I need to use google maps, i was using it by browser and was somewhat working (some old tablets does not work properly)

    I wanted to switch to the google maps views. I have the coordinates based on ZIPCODE what is all that I need from start and target and what to use this on the map, eventually even make the route.

    Is this FusedLocation the way to go?
  8. Erel

    Erel Administrator Staff Member Licensed User

    FusedLocationProvider requires these permissions (at least COARSE_LOCATION).

    The permission itself will never cause your app not to be compatible if you do it correctly. Start a new thread in the questions forum and I'll explain.
  9. Peter Simpson

    Peter Simpson Well-Known Member Licensed User

    Last edited: Dec 9, 2017 at 4:01 PM