【问题标题】:Getting Longitude and Latitude using Google maps v2使用谷歌地图 v2 获取经度和纬度
【发布时间】:2014-06-02 13:08:05
【问题描述】:

如何使用 Google 地图 v2 获取当前手机位置的经度经度?

我使用这种方法来放大我的设备位置:

googleMap.getUiSettings().setMyLocationButtonEnabled(true);

现在我如何获得坐标?

我知道这种获取面积的方法:

        double left = vr.latLngBounds.southwest.longitude;
        double top = vr.latLngBounds.northeast.latitude; ...

【问题讨论】:

  • 不能用谷歌地图完成吗?保持高效
  • (-1!!!) 如果不得到-1,这里就很难问了-至少要说为什么??
  • 如果您已经看到该链接解释了有关使用 mapView 的 Google 地图。我刚刚注意到您还有其他问题!
  • 希望此链接对您有所帮助。 [如何在谷歌地图中标注经纬度?][1] [1]:stackoverflow.com/questions/22032297/…

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


【解决方案1】:

您可以通过此代码从谷歌地图获取中心 Lat 和 Lng 值

LatLng latLng = map.getCameraPosition().target;
double lat = latLng.latitude;
double lng = latLng.longitude;

【讨论】:

    【解决方案2】:

    Google Maps API 位置有监听器,例如:

    private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new 
    
    GoogleMap.OnMyLocationChangeListener() {
    @Override
    
    public void onMyLocationChange(Location location) {
        LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
        mMarker = gMap.addMarker(new MarkerOptions().position(loc));
        if(gMap != null){
            gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
        }
    }
    

    };

    然后为地图设置监听器:

    gMap.setOnMyLocationChangeListener(myLocationChangeListener);

    这将在地图首次找到位置时调用。 试试这个代码。

    【讨论】:

      【解决方案3】:

      使用这个:

      private boolean gps_enabled = false;
      private boolean network_enabled = false;
      private Location location;
      private void getMyCurrentLocation() {
          Double MyLat = null, MyLong = null;
          String CityName = "";
          String StateName = "";
          String CountryName = "";
          LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          LocationListener locListener = new MyLocationListener();
      
          try {
              gps_enabled = locManager
                      .isProviderEnabled(LocationManager.GPS_PROVIDER);
          } catch (Exception ex) {
          }
          try {
              network_enabled = locManager
                      .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
          } catch (Exception ex) {
          }
      
          // don't start listeners if no provider is enabled
          // if(!gps_enabled && !network_enabled)
          // return false;
      
          if (gps_enabled) {
              locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                      0, locListener);
      
          }
      
          if (gps_enabled) {
              location = locManager
                      .getLastKnownLocation(LocationManager.GPS_PROVIDER);
      
          }
      
          if (network_enabled && location == null) {
              locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                      0, 0, locListener);
      
          }
      
          if (network_enabled && location == null) {
              location = locManager
                      .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
      
          }
      
          if (location != null) {
      
              MyLat = location.getLatitude();
              MyLong = location.getLongitude();
              map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                      MyLat, MyLong), 15));
      
          } else {
              Location loc = getLastKnownLocation(this);
              if (loc != null) {
      
                  MyLat = loc.getLatitude();
                  MyLong = loc.getLongitude();
      
              }
          }
          locManager.removeUpdates(locListener); // removes the periodic updates
      
      
      
      
      }
      
      public class MyLocationListener implements LocationListener {
          public void onLocationChanged(Location location) {
              if (location != null) {
              }
          }
      
          public void onProviderDisabled(String provider) {
          }
      
          public void onProviderEnabled(String provider) {
          }
      
          public void onStatusChanged(String provider, int status, Bundle extras) {
          }
      }
      
      public static Location getLastKnownLocation(Context context) {
          Location location = null;
          LocationManager locationmanager = (LocationManager) context
                  .getSystemService("location");
          List list = locationmanager.getAllProviders();
          boolean i = false;
          Iterator iterator = list.iterator();
          do {
              if (!iterator.hasNext())
                  break;
              String s = (String) iterator.next();
              if (i != false && !locationmanager.isProviderEnabled(s))
                  continue;
              Location location1 = locationmanager.getLastKnownLocation(s);
              if (location1 == null)
                  continue;
              if (location != null) {
                  float f = location.getAccuracy();
                  float f1 = location1.getAccuracy();
                  if (f >= f1) {
                      long l = location1.getTime();
                      long l1 = location.getTime();
                      if (l - l1 <= 600000L)
                          continue;
                  }
              }
              location = location1;
              i = locationmanager.isProviderEnabled(s);
          } while (true);
          return location;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-03
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多