我如何才能再添加一次pin谷歌地图



我对谷歌地图有问题。我想在触摸地图时添加pin。我在做项目时使用了这篇文章。这是地图链接

我的问题是我只能添加一次pin。但我想添加很多时间。我不明白问题出在哪里。有人能帮我吗?

我的代码在这里:

package com.example;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import com.google.android.maps.*;
import java.util.List;
public class HelloGoogleMaps2 extends MapActivity
{
    MapView mapView;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setSatellite(true);
        mapView.isClickable();
      // GeoPoint point = new GeoPoint(50443769,-71158458);
      // GeoPoint point=new GeoPoint(0,0);
       // AddMyPin(point);
        mapView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                 return onTouch2(view,motionEvent);
            }
        });
    }

    public boolean onTouch2(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == 0) {
            GeoPoint p = mapView.getProjection().fromPixels(
                    (int) motionEvent.getX(),
                    (int) motionEvent.getY());
           GeoPoint gp= new GeoPoint((int)motionEvent.getX(),(int)motionEvent.getY());
            AddMyPin(p);
        }
       return  false;
    }
    @Override
    protected boolean isRouteDisplayed()
    {
        return false;
    }
    public void AddMyPin(GeoPoint point)
    {
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.bubble);
        HelloItemizedOverlays itemizedoverlay = new HelloItemizedOverlays(drawable,this);
        OverlayItem overlayitem = new OverlayItem(point,null,null);

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

    }
} 

尝试将您的方法更改为:

public void AddMyPin(GeoPoint point)
{    
        Drawable drawable = this.getResources().getDrawable(R.drawable.bubble);
        HelloItemizedOverlays itemizedoverlay = new HelloItemizedOverlays(drawable,this);
        OverlayItem overlayitem = new OverlayItem(point,null,null);

        itemizedoverlay.addOverlay(overlayitem);
        mapView.getOverlays().add(itemizedoverlay);
        //this will cause the map to redraw with the new overlay 
        mapView.invalidate();
}

问题可能来自HelloItemizedOverlays类。我认为,一旦你创建了它,你就不会再填充它了。这就是为什么你只得到一个引脚,然后在第一次之后就没有其他引脚了。

因为populate是最后一个方法,您不能在itemizedoverlay类之外调用它。您可以在HelloItemizedOverlays类中创建一个公共方法,该方法可以根据您的请求调用populate。

在你的HelloItemdOverlays类中有这样的东西。

public void populateList() {
    populate();
}

在你的AddMyPin方法中。

 public void AddMyPin(GeoPoint point)
{
    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.bubble);
    HelloItemizedOverlays itemizedoverlay = new HelloItemizedOverlays(drawable,this);
    OverlayItem overlayitem = new OverlayItem(point,null,null);

    itemizedoverlay.addOverlay(overlayitem);
    itemizedoverlay.populateList();
    mapOverlays.add(itemizedoverlay);
    mapView.invalidate();

}

最新更新