OSMDroid:例如的onTap(OSMDroid: onTap example)

2019-07-01 12:45发布

我已经开始研究Android的几个星期前,现在我需要你的帮助。 我的任务是创建离线地图(使用OSMDroid和移动阿特拉斯造物主),上面有两个标志,点击这个标记后,他们与活动之间的路径。 我所做的地图上,标记和路径。 下面是代码(安卓2.3.3):

公共类MainActivity延伸活动{

private MapView mapView;
LocationManager locationManager;
ArrayList<OverlayItem> overlayItemArray;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mapView = new MapView(this, 256);
    mapView.setClickable(true);
    mapView.setBuiltInZoomControls(true);
    mapView.getController().setZoom(15); 
    mapView.getController().setCenter(new GeoPoint(54.332, 48.389));  
    mapView.setUseDataConnection(false);

    overlayItemArray = new ArrayList<OverlayItem>();        
    OverlayItem olItem = new OverlayItem("Here", "SampleDescription", new GeoPoint(54.332, 48.389));       
    overlayItemArray.add(olItem);
    overlayItemArray.add(new OverlayItem("Hi", "You're here", new GeoPoint(54.327, 48.389)));

    PathOverlay myPath = new PathOverlay(Color.RED, this);
    myPath.addPoint(new GeoPoint(54.327, 48.389));
    myPath.addPoint(new GeoPoint(54.332, 48.389));
    mapView.getOverlays().add(myPath);

DefaultResourceProxyImpl defaultResourceProxyImpl = new DefaultResourceProxyImpl(this);
ItemizedIconOverlay<OverlayItem> myItemizedIconOverlay  = new ItemizedIconOverlay<OverlayItem>(overlayItemArray, null, defaultResourceProxyImpl);
    mapView.getOverlays().add(myItemizedIconOverlay);

    setContentView(mapView); //displaying the MapView
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}    

}问题:如何实现此标记的onClick法? 而对于profy另一个问题:如何做是正确的(我的意思是如何除以类此程序)? 非常感谢! =)

Answer 1:

您需要创建新类,如下所示:

    public class MyOwnItemizedOverlay extends ItemizedIconOverlay<OverlayItem> {
    protected Context mContext;

    public MyOwnItemizedOverlay(final Context context, final List<OverlayItem> aList) {
         super(context, aList, new OnItemGestureListener<OverlayItem>() {
                @Override public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                        return false;
                }
                @Override public boolean onItemLongPress(final int index, final OverlayItem item) {
                        return false;
                }
              } );
        // TODO Auto-generated constructor stub
         mContext = context;
    }

    @Override 
    protected boolean onSingleTapUpHelper(final int index, final OverlayItem item, final MapView mapView) {
        //Toast.makeText(mContext, "Item " + index + " has been tapped!", Toast.LENGTH_SHORT).show();
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;
    }
}

下面是如何在第一(主)类使用它:

MapView mapView = new MapView(this, 256); //constructor
//some code from te question

ArrayList<OverlayItem> overlayItemArray = new ArrayList<OverlayItem>();                
OverlayItem olItem = new OverlayItem("Here", "SampleDescription", new GeoPoint(54.332, 48.389));//marker

MyOwnItemizedOverlay overlay = new MyOwnItemizedOverlay(this, overlayItemArray);
mapView.getOverlays().add(overlay);        
setContentView(mapView); //displaying the MapView

就这样。 祝好运! 链接: http://code.google.com/p/osmdroid/issues/detail?id=245#makechanges



文章来源: OSMDroid: onTap example