javaandroidtomtomtomtom-android-sdk

Returned location is null in TomTom Android SDK


I'm trying to use TomTom's SDK for my Android app so I was practicing this tutorial https://developer.tomtom.com/maps-sdk-android/android-tutorials-use-cases/search-along-route but whenever I'm trying to invoke tomtomMap.centerOnMyLocation() I get an error. It used to work but now my returned location is always null.

Thanks in advance


Solution

  • Register map ready callback

    mapFragment = (MapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
    mapFragment.getAsyncMap(onMapReadyCallback);
    

    then enable location:

    private OnMapReadyCallback onMapReadyCallback =
            new OnMapReadyCallback() {
       @Override
       public void onMapReady(TomtomMap map) {
          //Map is ready here
          tomtomMap = map;
          tomtomMap.setMyLocationEnabled(true);
       }
    };
    

    Forward permissions callbacks from Activity to TomtomMap to support runtime permission:

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        tomtomMap.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }