【问题标题】:How to get Latitude/Longitude span in Google Map V2 for Android如何在适用于 Android 的 Google Map V2 中获取纬度/经度跨度
【发布时间】:2012-12-06 07:45:59
【问题描述】:

我的任务是将我的应用程序迁移到 Google Maps Android APIs V2。现在,我需要获取纬度/经度跨度。我在以前版本的 API 中使用了 MapView.getLatitudeSpan()MapView.getLongitudeSpan()。现在我在 V2 中找不到这样的东西。

有人遇到同样的问题吗?

【问题讨论】:

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


    【解决方案1】:

    您可以使用以下代码获取纬度/经度跨度:

    VisibleRegion vr = mMap.getProjection().getVisibleRegion();
    double left = vr.latLngBounds.southwest.longitude;
    double top = vr.latLngBounds.northeast.latitude;
    double right = vr.latLngBounds.northeast.longitude;
    double bottom = vr.latLngBounds.southwest.latitude;
    

    我希望这会有所帮助。

    【讨论】:

    • 我们可以得到地图中心可见部分 lat lng
    • 太棒了!谢谢。
    【解决方案2】:

    首先使用GoogleMap.getProjection() 获得一个投影。然后你可以调用Projection.getVisibleRegion()来获取一个有LatLngBounds的VisibleRegion。

    LatitudeSpan 和 Longitude 跨度不再有意义的原因是地图现在可以旋转和倾斜,因此视口不再是地图上经纬度对齐的矩形。

    【讨论】:

      【解决方案3】:

      这种方式适合我:

      CameraPosition camPos2 = mapa.getCameraPosition();
      LatLng pos = camPos2.target;
      Toast.makeText(MainActivity.this,"Lat: " + pos.latitude + " - Lng: " +pos.longitude,  Toast.LENGTH_LONG).show();
      


      糟糕,我误解了这个问题,我的意思是我没有看到“span”这个词。 根据API,正确的是:

      首先得到边界:

      LatLngBounds bounds = gMap.getProjection().getVisibleRegion().latLngBounds;
      

      然后询问是否有任何点在边界内:

      LatLng point = new LatLng (latitude, longitude);
      if(bounds.contains(point)){
          //do something
      }
      

      【讨论】:

        【解决方案4】:

        这就是答案

        LatLngBounds bounds = googleMap.getProjection().getVisibleRegion().latLngBounds;
        if (bounds.contains(ROMA)) {
           marker = googleMap.addMarker(
             new MarkerOptions()
              .position(ROMA)
              .title("Hello")
              .snippet("Nice Place")
              .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
             );     
           System.out.println("Marker added");
        }
        

        仅在标记落在可见区域时添加标记

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-02-28
          • 2017-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-02
          • 2012-06-30
          相关资源
          最近更新 更多