3

几周前我开始学习 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方法?还有一个关于 profy 的问题:如何正确地做(我的意思是如何按类划分这个程序)?非常感谢!=)

4

1 回答 1

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

于 2012-10-24T17:00:18.367 回答