【问题标题】: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;
我希望这会有所帮助。
【解决方案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");
}
仅在标记落在可见区域时添加标记