【发布时间】:2011-09-22 10:22:32
【问题描述】:
我有两个间歇性变化的地理点,并希望 MapView 调整大小和平移以确保两个点始终可见。我可以轻松地将地图重新居中于两者之间的中间点,但如何设置缩放级别以确保我的两个点可见?
【问题讨论】:
我有两个间歇性变化的地理点,并希望 MapView 调整大小和平移以确保两个点始终可见。我可以轻松地将地图重新居中于两者之间的中间点,但如何设置缩放级别以确保我的两个点可见?
【问题讨论】:
在另一个帖子中查看他的答案: Google Map V2 how to set ZoomToSpan?
它毫不费力地解决了它。请注意,您只能在地图至少加载一次后使用该功能。如果不在屏幕上使用指定地图大小的函数
LatLngBounds bounds = new LatLngBounds.Builder()
.include(new LatLng(CURRENTSTOP.latitude, CURRENTSTOP.longitude))
.include(new LatLng(MYPOSITION.latitude, MYPOSITION.longitude)).build();
Point displaySize = new Point();
getWindowManager().getDefaultDisplay().getSize(displaySize);
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, displaySize.x, 250, 30));
像魅力一样工作,非常有活力!
【讨论】:
试试这个看看:
double latitudeSpan = Math.round(Math.abs(firstLat -
secLat));
double longitudeSpan = Math.round(Math.abs(firstLong -
secLong));
double currentLatitudeSpan = (double)mapView.getLatitudeSpan();
double currentLongitudeSpan = (double)mapView.getLongitudeSpan();
double ratio = currentLongitudeSpan/currentLatitudeSpan;
if(longitudeSpan < (double)(latitudeSpan+2E7) * ratio){
longitudeSpan = ((double)(latitudeSpan+2E7) * ratio);
}
mapController.zoomToSpan((int)(latitudeSpan*2), (int)(longitudeSpan*2));
mapView.invalidate();
【讨论】:
if (longitudeSpan < ...){...} 适配,现在它工作正常。还要记住 firstLat,firstLong,secLat,secLong 来自 GeoPoints,即原始纬度/经度值 * 1E6。
在新的 Maps API (v2) 中,您可以这样做:
LatLng southwest = new LatLng(Math.min(laglng1.latitude, laglng2.latitude), Math.min(laglng1.longitude, laglng2.longitude));
LatLng northeast = new LatLng(Math.max(laglng1.latitude, laglng2.latitude), Math.max(laglng1.longitude, laglng2.longitude));
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds(southwest, northeast),500,500, 0));
【讨论】: