0

这是我在谷歌地图上显示登录用户并获取用户位置详细信息(即纬度和经度)的代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start);
    googleMap = ((MapFragment) getFragmentManager().findFragmentById(
            R.id.map)).getMap();
    googleMap.setMyLocationEnabled(true);
    // Creating location manager and location classes instances
    locationManager = (LocationManager) this
            .getSystemService(LOCATION_SERVICE);
     location = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
     CameraPosition cameraPosition = new CameraPosition.Builder().target(
                new LatLng(location.getLatitude(), location.getLongitude())).zoom(9).build();

googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

.通过调试在这里尝试能够在地图上显示用户成功完成但是为了获取位置详细信息得到空指针异常,我正在使用android移动(设置已配置)但我不明白为什么空指针异常。建议我合适的解决方案。(这里使用wifi连接。)

4

2 回答 2

1

您打电话时可能没有位置,getLastKnownLocation这就是您获得空值的原因。因此,在使用位置对象之前,您应该检查以确保它不为空。例如if(location != null)

您可能应该考虑使用新的Location API甚至是内置的谷歌地图位置管理器,因为它们可以获得更好的结果

于 2013-09-27T20:18:57.080 回答
1

一个不错的 gps 跟踪器指南:http ://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

只需在 onLocationChanged 中添加 this.mLocation = location


googleMap 可以为空,视图尚未完全创建。

如果手机上未启用 GPS,则 location 可以为空。

搜索并遵循有关该主题的一些指南。 

于 2013-09-27T20:24:19.207 回答