【问题标题】:google Maps v2 API for android location return null [duplicate]用于 android 位置的 Google Maps v2 API 返回 null [重复]
【发布时间】:2015-03-02 14:38:40
【问题描述】:

您好,我需要一些帮助,我已经查看并尝试了许多不同的东西。我正在尝试在 android 上获取我当前的位置,但由于该位置返回 null,我的应用程序崩溃了。我真的需要帮助。

我不知道我在这里是否清楚,但每次我调用 getLastKnownlocation 它都会返回 null,所以当我尝试让双 lat= location.getLatitude() 与经度相同时,它不会返回任何东西都会在我的应用崩溃的地方。

帮助... 这里有一段代码

mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();

String provider = locationManager.getBestProvider(criteria, true);

Location location = locationManager.getLastKnownLocation(provider);

mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng latLng = new LatLng(lat, lng);

在纬度是它停止的地方。

【问题讨论】:

  • 你在 String provider = locationManager.getBestProvider(criteria, true); 得到什么提供者?
  • 永远不必将位置返回给您,因此您必须始终检查空位置
  • @CAA 我得到的提供者是这条线上的GPS
  • 尝试在你的Mainactivity中添加findLocation方法,请参考here

标签: android google-maps google-maps-android-api-2 google-maps-api-2


【解决方案1】:

在调用这些函数之前,您是否从片段中检索 GoogleMap 对象?

类似:

//get map fragment from the static layout (in this case it has id = "map")
    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map);
//get GoogleMap object, then you can start using it, for example enabling the location
    map = mapFragment.getMap();
    map.setMyLocationEnabled(true);

【讨论】:

  • 是的@Andrea Montanari 它正在被调用,事实上我的地图打开得很好,没有问题,但这只是在我编码它以获得当前位置之前。如果我这样做会更清楚: MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.map); map.setMyLocationEnabled(true);它会打开并指向我所在的位置,但如果我尝试获取当前位置以便相机可以移动到我所在的位置,这将返回 null 并使我的应用程序崩溃。
  • 您是否添加了检索职位所需的权限?
  • 是的,我已经添加了所有这些:
  • 好的,那么我建议您执行以下操作:在我的工作代码中,我执行以下步骤:gps = new GPSTracker(this); if(gps.canGetLocation()){ //检索坐标双纬度 = gps.getLatitude();双经度 = gps.getLongitude(); latlng = new LatLng(纬度,经度); //将地图放大 16 级 map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 16));你可以从github.com/andreamontanari/android看到GPSTracker类
【解决方案2】:

谢谢大家的帮助,我很感激,我已经解决了我的问题。 这里有一段代码帮助我完成了工作:

//I needed to have this line before everything else for me to get my current 
//location from getLastKnownLocation method.

mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet"));

//And the all the codes I had before
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

String provider = locationManager.getBestProvider(criteria, true);

Location location = locationManager.getLastKnownLocation(provider);

mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng latLng = new LatLng(lat, lng);

mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions()
             .position(new LatLng(lat, lng))
             .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
             .title("Here"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多