我在SherlockFragmentActivity
中使用com.google.android.gms.maps.GoogleMap
。
XML代码是这样的:
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="fill_parent"
android:layout_height="150dip" />
int zoomLevel =
?//如何计算两个不同纬度值的缩放级别由于安卓地图 v3 需要告诉缩放级别为 int
map.setZoom(zoomLevel);
我的起始值和目标值com.google.android.gms.maps.model.LatLng
LatLng开始,结束;
我正在添加一个像GoogleLocation.addPolyLineOnGMap(mMap, startPoint, endPoint, startMarker, endMarker)
我的问题是如何计算谷歌地图的缩放级别,以便它可以在地图上适当地显示这两个标记。
使用 LatLngBounds.Builder 添加其中的所有边界并构建它,然后创建 CameraUpdate 对象并在其中传递边界 updatefactory 与填充。使用此 CameraUpdate 对象可对地图摄像机进行动画处理。
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker m : markers) {
builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
int padding = ((width * 10) / 100); // offset from edges of the map
// in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,
padding);
mMap.animateCamera(cu);
,我需要按 GoogleMapOptions
计算初始地图设置的缩放比例,因此使用 LatLngBounds.Builder
不会工作,也不会优化。这就是我根据城市的东北和西南坐标计算缩放的方式
它引用了这里和这个答案,你可以简单地将下面的代码放到你的帮助程序类中:
final static int GLOBE_WIDTH = 256; // a constant in Google's map projection
final static int ZOOM_MAX = 21;
public static int getBoundsZoomLevel(LatLng northeast,LatLng southwest,
int width, int height) {
double latFraction = (latRad(northeast.latitude) - latRad(southwest.latitude)) / Math.PI;
double lngDiff = northeast.longitude - southwest.longitude;
double lngFraction = ((lngDiff < 0) ? (lngDiff + 360) : lngDiff) / 360;
double latZoom = zoom(height, GLOBE_WIDTH, latFraction);
double lngZoom = zoom(width, GLOBE_WIDTH, lngFraction);
double zoom = Math.min(Math.min(latZoom, lngZoom),ZOOM_MAX);
return (int)(zoom);
}
private static double latRad(double lat) {
double sin = Math.sin(lat * Math.PI / 180);
double radX2 = Math.log((1 + sin) / (1 - sin)) / 2;
return Math.max(Math.min(radX2, Math.PI), -Math.PI) / 2;
}
private static double zoom(double mapPx, double worldPx, double fraction) {
final double LN2 = .693147180559945309417;
return (Math.log(mapPx / worldPx / fraction) / LN2);
}
只需new LatLng(lat-double, lng-double)
即可创建LatLng
width
和height
是地图布局大小(以像素为单位)
在 Android 中:
LatLngBounds group = new LatLngBounds.Builder()
.include(tokio) // LatLgn object1
.include(sydney) // LatLgn object2
.build();
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(group, 100)); // Set Padding and that's all!