5

嗨,我想让我的地图放大到我当前的位置。当前位置是通过向模拟器发送 lat 和 long 来定义的。我该怎么做呢?

我当前的 mapactivity.java

public class MapsActivity extends MapActivity {

    private MapView mapView;
    private MyLocationOverlay myLocOverlay;
    MapController mc;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapactivity);
        initMap();
        initMyLocation();


    }

    /**
     * Initialise the map and adds the zoomcontrols to the LinearLayout.
     */
    private void initMap() {
        mapView = (MapView) findViewById(R.id.mapView);

        View zoomView = mapView.getZoomControls();
        LinearLayout myzoom = (LinearLayout) findViewById(R.id.zoom);
        myzoom.addView(zoomView);
        mapView.displayZoomControls(true);
        mapView.getController().setZoom(17);
    }

    /**
     * Initialises the MyLocationOverlay and adds it to the overlays of the map
     */
    private void initMyLocation() {
        myLocOverlay = new MyLocationOverlay(this, mapView);
        myLocOverlay.enableMyLocation();
        mapView.getOverlays().add(myLocOverlay);

    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

谢谢你。

4

3 回答 3

9

尝试在您的 initMyLocation 代码末尾添加:

controller = mapView.getController();
mMyLocOverlay.runOnFirstFix(new Runnable() {
            public void run() {
                controller.setZoom(17);
                controller.animateTo(mMyLocOverlay.getMyLocation());
            }
        });

您可以从 initMap 方法中删除此行:

mapView.getController().setZoom(17);

让我知道这是否有效

格尔

于 2010-11-10T17:15:59.750 回答
3

您需要实现 onLocationChanged()。在我的代码中,我也在这里添加了叠加层。至少您需要 setCenter() 与您当前的位置

public void onLocationChanged(Location location) {

   List<Overlay> overlays = mapView.getOverlays();
   myLocOverlay = new MyLocationOverlay(this, mapView);
   overlays.add(myLocOverlay);
   myLocOverlay.enableMyLocation();*

   // definitely need what's below
   int lat = (int) (location.getLatitude() * 1E6);
   int lng = (int) (location.getLongitude() * 1E6);
   GeoPoint point = new GeoPoint(lat, lng);
   mc.setCenter(point);
   mapView.invalidate();

}
于 2010-11-10T17:18:20.397 回答
1

由于您已经有了位置数据,您可以创建一个 LatLng 对象/结构并直接在 Map 对象上使用此行:

map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15.0f));

我使用 Android Google Maps API v2

于 2013-09-04T12:22:53.150 回答