问题
I've started to study Android few weeks ago and now I need your help. My task is create off-line map (using OSMDroid and Mobile Atlas Creator), with two markers on it, path between them and some activity after clicking on this markers. I've done map, markers and path. Here is the code (Android 2.3.3):
public class MainActivity extends 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;
}
} Question: how to realize onClick-method for this markers? And additional question for profy: how to do it right (I mean how to divide this program by classes)? Thanks a lot! =)
回答1:
You need to create new class, like this:
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;
}
}
Here is how to use it in the first (main) class:
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
That's all. Good luck! links: http://code.google.com/p/osmdroid/issues/detail?id=245#makechanges
来源:https://stackoverflow.com/questions/12991175/osmdroid-ontap-example