getUserLocation() return null / centerOnMyLocation() not center in onMapReady

Hi all,

I did a google search and some other people have similar/same issue in Github.

Anyway the issue is when the activity is launched, I want the map shows the user current location (assume their GPS is on).

However it crashes and I found that it is because the user current location is null.

If I don’t use centerOnMylocation() / use the result from getUserLocation(), but click the center button / click a custom made button to center it, it works.

So here is my code (simplified one, which won’t center):

@Override
    public void onMapReady(@NonNull TomtomMap tomtomMap) {
        this.tomtomMap = tomtomMap;
        this.tomtomMap.setMyLocationEnabled(true);
        this.tomtomMap.centerOnMyLocation();
    }

If I use this way, it will crash

@Override
    public void onMapReady(@NonNull TomtomMap tomtomMap) {
        this.tomtomMap = tomtomMap;
        this.tomtomMap.setMyLocationEnabled(true);


        Location location = tomtomMap.getUserLocation();

        LatLng centre = new LatLng(location.getLatitude(), location.getLongitude());
        
        CameraPosition cameraPosition = CameraPosition.builder().focusPosition(centre).zoom(1).build();

        this.tomtomMap.centerOn(cameraPosition);

     
    }

Now I think if it is because after setMyLocationEnabled it takes sometimes for the app get the GPS signal/location detail which eventually it gets the detail after the centerOnMyLocation or getUserLocation is called.

Thanks.

Hi.
Most probably you are trying to get user location too early when GPS position is not yet available.

To make sure that you are getting the user location as quick as possible you can override the onLocationChanged callback.

Please find an example activity code below:

public class MainActivity extends AppCompatActivity implements LocationUpdateListener, OnMapReadyCallback {

private TomtomMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Register onMapReady callback
    MapFragment mapFragment = (MapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
    mapFragment.getAsyncMap(this);
}

@Override
public void onMapReady(@NonNull TomtomMap tomtomMap) {
    this.map = tomtomMap;

    // Enable location and register location listener callback
    this.map.setMyLocationEnabled(true);
    this.map.addLocationUpdateListener(this);
}

// Forward permissions callbacks
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    this.map.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
public void onLocationChanged(Location location) {
    // Use map.getUserLocation() without getting NULL
    Toast.makeText(this, this.map.getUserLocation().toString(), Toast.LENGTH_SHORT).show();
    // Remove location listener if needed
    this.map.removeLocationUpdateListener(this);
}
}

Hope this helps a little bit.

Regards,
Mateusz

3 Likes

Hi Mateusz,

It works :slight_smile: Thank you for your help once again. Always helpful.

Regards,

David

1 Like