Google Maps Android API v2和当前位置

问题描述:

我正在使用JellyBean 4.2并在我的设备上进行测试。Google Maps Android API v2和当前位置

如何在点击myLocationButton时获取当前位置。

getMYLocation在使用网络提供程序时返回null。

我正在使用LocationManager获取当前位置并参考Google文档 https://developers.google.com/maps/documentation/android/start

此点击上MyLocatiionButton后没有带来任何变化

+0

正在使用设备或模拟器? – Jitendra 2013-02-22 11:36:35

+0

我正在使用Nexus 4 – 2013-02-22 11:44:32

基本上你应该做的就是打电话给谷歌方向API接收道路方向坐标(许多Latlng点),然后在他们每一个之间画一条折线。

您可以使用用户overview_polyline或腿和步骤。

你都同意在清单中设置? 此外,您必须启用位置服务(状态栏中的gps图标显示?)。 设备获取位置需要一些时间,因此请参阅是否调用onLocationChanged。

+0

我已经设置了文档中指定的所有权限,并且gps图标也出现在状态栏 – 2013-02-22 11:16:27

使用此代码Id它工作。

private LocationManager locationManager; 
private Location location; 
private boolean hasGpsProvider, hasNetwrokProvider; 

    public Location getLocation(Context mContext) { 

     if (locationManager == null) { 
      locationManager = (LocationManager) mContext 
        .getSystemService(Context.LOCATION_SERVICE); 
     } 

     hasGpsProvider = locationManager 
       .isProviderEnabled(LocationManager.GPS_PROVIDER); 
     hasNetwrokProvider = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
     if (hasGpsProvider) { 
      locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 0, 100, locationListenerGps); 
      location = locationManager 
        .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      return location; 
     } 

     if (hasNetwrokProvider) { 
      locationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 0, 100, 
        locationListenerNetwork); 
      location = locationManager 
        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      return location; 
     } 

     return location; 
    } 

并调用

if (location != null) { 
    lat = location.getLatitude(); 
    lng = location.getLongitude(); 
} 
+0

您是否解决了您的问题? – Jitendra 2013-02-22 11:55:46

+0

稍后会做出更改并通知您 – 2013-02-22 12:11:32

你需要一个LocationSource添加到您的地图片段。请参阅this answer以获得一个独立的示例。

+0

感谢spotdog13它工作 – 2013-02-27 18:32:12