2

我正在尝试在我的 android 应用程序上使用 Google 地图 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;
}

}
4

2 回答 2

2

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

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

它至少对我有用。

于 2012-12-01T05:07:13.817 回答
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..,.

试试这个..,。

于 2012-12-01T05:15:58.593 回答