0

我正在尝试将自定义标记添加到地图上的用户选择中。这是我现在拥有的代码,但似乎在“mapView.getOverlays().add(mapView);”的“add”下出现错误 我希望用户选择各种图标并将它们放置在它们所在的地图上。

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;

 public class MainActivity extends MapActivity {



@Override
  public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 MapView mapView = (MapView) findViewById(R.id.mapview);
 mapView.setBuiltInZoomControls(true);
 List<Overlay> mapOverlays = mapView.getOverlays();
 Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
 CustomOverlay itemizedoverlay = new CustomOverlay(drawable, this);
 GeoPoint point = new GeoPoint((int)(24.0110 * 1E6), (int)(31.4850 * 1E6));
 MapController controller = mapView.getController();
 controller.setCenter(point);
 controller.setZoom(11);
 OverlayItem overlayitem = new OverlayItem(point, "Hello!", "Guess where I am?")
 itemizedoverlay.addOverlay(overlayitem);
 mapOverlays.add(itemizedoverlay);

 }

public boolean onTouchEvent(MotionEvent event, MapView mapView) {
    if (event.getAction() == 1) {
        GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),
                (int) event.getY());
        MainActivity Overlay = new MainActivity(); 
        mapView.getOverlays().add(mapView);

        mapView.invalidate();
        GeoPoint point = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY ());
        Drawable srcdrawable = getApplicationContext().getResources().getDrawable (R.drawable.icon);
        CustomOverlay srcitemizedOverlay = new CustomOverlay(srcdrawable, getApplicationContext ());
        OverlayItem srcoverlayitem = new OverlayItem(p, "Hello!", "This is your Location.");
        srcitemizedOverlay.addOverlay(srcoverlayitem);
        mapView.getOverlays().clear();
        mapView.getOverlays().add(srcitemizedOverlay);

    }
    return false;
 }


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

我在写轨道上吗?我的代码正确吗?有人可能有一个关于如何做到这一点的教程的链接。

4

1 回答 1

0

您正在尝试将 a 添加MapViewOverlay您的mapView. 因此,在这一行上mapView.getOverlays().add(mapView);替换mapViewOverlay您要添加的。但是你不需要那个,因此,你onTouchEvent()应该是这样的:

public boolean onTouchEvent(MotionEvent event, MapView mapView) {
    if (event.getAction() == 1) {
        GeoPoint point = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY ());
        Drawable srcdrawable = getApplicationContext().getResources().getDrawable (R.drawable.icon);
        CustomOverlay srcitemizedOverlay = new CustomOverlay(srcdrawable, getApplicationContext ());
        OverlayItem srcoverlayitem = new OverlayItem(point, "Hello!", "This is your Location.");
        srcitemizedOverlay.addOverlay(srcoverlayitem);
        mapView.getOverlays().clear();
        mapView.getOverlays().add(srcitemizedOverlay);

    }
    return false;
 }
于 2012-11-12T14:35:36.443 回答