【问题标题】:Google Maps Android API v2 and current locationGoogle Maps Android API v2 和当前位置
【发布时间】:2013-02-22 11:12:13
【问题描述】:

我正在使用 JellyBean 4.2 并在我的设备上进行测试。

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

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

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

在此之后点击 MyLocatiionButton 并没有带来任何变化

【问题讨论】:

  • 是在设备还是模拟器上使用?

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


【解决方案1】:

您需要将 LocationSource 添加到您的地图片段。请参阅this answer 了解离散示例。

【讨论】:

    【解决方案2】:

    基本上你应该做的是调用 Google Directions API 接收道路方向坐标(许多 Latlng 点),然后在每个坐标点之间绘制一条折线。

    你可以使用overview_polyline 或legs and steps。

    【讨论】:

      【解决方案3】:

      您是否在清单中设置了所有权限? 此外,您必须启用定位服务(状态栏中的 gps 图标是否显示?)。 设备获取位置需要一些时间,因此请查看是否调用了 onLocationChanged。

      【讨论】:

      • 我已按照文档中的说明设置了所有权限,并且 gps 图标也出现在状态栏
      【解决方案4】:

      使用此代码 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();
      }
      

      【讨论】:

      • 稍后会进行更改并通知您
      猜你喜欢
      • 2012-11-30
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 2015-03-13
      相关资源
      最近更新 更多