本文介绍了TomtomMap.getUserLocation()始终返回null吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取设备位置,但是TomtomMap.getUsersLocation()始终返回null.

I trying to get the devices location, but the TomtomMap.getUsersLocation() is always returning null.

接线问题是,由于我设置了TomtomMap.setMyLocationEnabled(true),所以地图正确标记了用户位置,但是当我尝试自行获取时,它会返回null.

The wired thing is that since i set TomtomMap.setMyLocationEnabled(true) the map marks the user location correctly, but when i try to get it for my self it returns null.

我遵循了有关如何执行此操作的文档,它非常简单,无法使用.我还检查了下面的答案,但这只是文档的副本.

I followed the documentation on how to do it, it was very simple to follow, it just does not work.I also checked the answer below, but it is just a copy of the docs.

在TomTom Android SDK中返回的位置为空

推荐答案

不看代码就很难猜测,但很可能是在GPS位置尚不可用时,您试图过早获取用户位置(也许在onMapReady回调).

It's hard to guess without looking at the code, but most probably you are trying to get user location too early when GPS position is not yet available (perhaps inside onMapReady callback).

要确保尽快获得用户位置,可以覆盖onLocationChanged回调.

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);
}
}

这篇关于TomtomMap.getUserLocation()始终返回null吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:00