OSMDroid:onTap示例

时间:2012-10-20 17:59:31

标签: android onclick osmdroid

我几周前开始学习Android,现在我需要你的帮助。 我的任务是创建离线地图(使用OSMDroid和Mobile Atlas Creator),其上有两个标记,它们之间的路径以及点击此标记后的一些活动。我做过地图,标记和路径。这是代码(Android 2.3.3):

公共类MainActivity扩展了Activity {

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方法?还有额外的问题:如何做正确的(我的意思是如何按类划分这个程序)? 非常感谢! =)

1 个答案:

答案 0 :(得分:3)

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

    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