MapsForge 地图是否存在 zoomToSpan 方法?如果不存在,我该如何实施?是否有一些代码示例或算法描述?
提前致谢
您可以使用以下方法扩展 MapView(MapsForge 版本 0.3.1):
public synchronized void fitToBoundingBox(final BoundingBox pBoundingBox, final int pMaximumZoom) {
int width = this.getWidth();
int height = this.getHeight();
if (width <= 0 || height <= 0) {
this.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
MapView.this.getViewTreeObserver().removeGlobalOnLayoutListener(this);
fitToBoundingBox(pBoundingBox, pMaximumZoom);
}
});
} else {
Projection projection1 = this.getProjection();
GeoPoint pointSouthWest = new GeoPoint(pBoundingBox.minLatitude, pBoundingBox.minLongitude);
GeoPoint pointNorthEast = new GeoPoint(pBoundingBox.maxLatitude, pBoundingBox.maxLongitude);
Point pointSW = new Point();
Point pointNE = new Point();
byte maxLvl = (byte) Math.min(pMaximumZoom, this.getMapZoomControls().getZoomLevelMax());
byte zoomLevel = 0;
while (zoomLevel < maxLvl) {
byte tmpZoomLevel = (byte) (zoomLevel + 1);
projection1.toPoint(pointSouthWest, pointSW, tmpZoomLevel);
projection1.toPoint(pointNorthEast, pointNE, tmpZoomLevel);
if (pointNE.x - pointSW.x > width) {
break;
}
if (pointSW.y - pointNE.y > height) {
break;
}
zoomLevel = tmpZoomLevel;
}
this.getMapViewPosition().setMapPosition(new MapPosition(pBoundingBox.getCenterPoint(), zoomLevel));
}
}
很可能您有一组应显示在地图上的叠加层。您必须找到经度和纬度的“最小”和“最大”值,然后调用上面的方法:
mapView.fitToBoundingBox(new BoundingBox(minLat, minLng, maxLat, maxLng), 20);
在这里找到了这个解决方案
假设您已经在模拟器中显示了地图,我已经给出了地图中缩放视图功能的示例代码。希望这可以帮助
保持 Main.xml 文件如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0l4sCTTyRmXTNo7k8DREHvEaLar2UmHGwnhZVHQ"
/>
<LinearLayout android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
和 mapsActivity 如下:
package net.learn2develop.GoogleMaps;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MapsActivity extends MapActivity
{
MapView mapView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}