如何在谷歌地图上添加标记给用户在地图上的选择

时间:2012-11-12 13:49:06

标签: android android-layout android-mapview

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

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

我在写作轨道上吗?我的代码是否正确?也许某人可能有一个关于如何执行此操作的教程的链接。

1 个答案:

答案 0 :(得分:0)

您正尝试将MapView添加为Overlay mapView。因此,在此行mapView.getOverlays().add(mapView);上将mapView替换为您要添加的Overlay。但是你不需要那样,因此,你的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;
 }