Android Map缩放到覆盖项目

时间:2012-12-01 04:51:49

标签: android

我正在尝试在我的Android应用上使用Google maps API。我在特定的地理点坐标上设置了一个叠加项。在打开活动时,它使用谷歌地图的默认地图视图以及该地理位置的重叠项目的正确位置。我的问题是,如何设置要为该地理点放大的视图,以便实际上以更好的方式查看位置,而不必通过跟踪覆盖项来手动放大。我希望缩放它以便我可以看到本地区域,以便用户至少可以从中获取有用的信息。 我的代码如下:

/ ---- HelloItemizedOverlay.java ------- /

public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;

public void addOverlay(OverlayItem overlay)
{
    mOverlays.add(overlay);
    populate();
}

public HelloItemizedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    // TODO Auto-generated constructor stub
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
      super(boundCenterBottom(defaultMarker));
      mContext = context;
    }


@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return mOverlays.get(i);
}

@Override
public int size() {
    // TODO Auto-generated method stub
    return mOverlays.size();
}

@Override
protected boolean onTap(int index) {
  OverlayItem item = mOverlays.get(index);
  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}
}

/ * ---- MainActivity.java ---- * /

public class MainActivity extends MapActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MapView view = (MapView)findViewById(R.id.themap);
    view.setBuiltInZoomControls(true);

    final GeoPoint point = new GeoPoint(19240000,-99120000);
    OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");

    GeoPoint point2 = new GeoPoint(35410000, 139460000);
    OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");


    final MapController control = view.getController();
    LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    LocationListener listener = new LocationListener() {

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location arg0) {
            // TODO Auto-generated method stub
            control.setCenter(point);
        }
    };

    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
    List<Overlay> mapOverlays = view.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);


    itemizedoverlay.addOverlay(overlayitem2);
    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);




}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

2 个答案:

答案 0 :(得分:2)

@noobcoder我使用了相同的示例,如果我的问题正确,您可以使用以下方法缩放到特定点。

https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/MapController#animateTo(com.google.android.maps.GeoPoint,java.lang.Runnable)

至少对我有用。

答案 1 :(得分:1)

view = (MapView)findViewById(R.id.themap);
mapController = view.getController();

point = new GeoPoint((int) (38.897089* 1E6), (int) (-77.051437* 1E6));

mapControler .animateTo(point );
mapControler .setZoom(17);    //Set zoom level as you want..,.

试试这个..,。