Android zoomToSpan for MapsForge地图

时间:2012-12-30 21:32:11

标签: android zoom

MapsForge地图是否存在zoomToSpan方法? 如果不存在,我该如何实现它? 是否有任何代码示例或算法描述?

提前致谢

2 个答案:

答案 0 :(得分:2)

您可以使用以下方法扩展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));
    }
}

很可能你有一套应该在地图上显示的叠加层。你必须找到经度和纬度的'min'和'max'值,然后调用上面的方法:

mapView.fitToBoundingBox(new BoundingBox(minLat, minLng, maxLat, maxLng), 20);

找到了这个解决方案here

答案 1 :(得分:0)

假设您已经在模拟器中显示了地图,我已经为地图中的缩放视图功能提供了示例代码。希望这有帮助

保留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;
         }
     }