我刚刚编写了一些代码来为地图上的所有标记设置onclick监听器(我想在点击它时导航到标记)。然而,这种方法似乎并没有起作用 - 实际上我在调试中弹出了几个Toast,而且他们似乎也没有打印。我会很感激我对失踪的一些看法......我只是在onCreate()中调用navigateToMarkers。
//method to set onclicks for markers - when you click one, you navigate to it.
private void navigateToMarkers(){
for(int i=0; i<bookMap.getOverlays().size(); i++){
if(bookMap.getOverlays().get(i) instanceof Marker){
((Marker) bookMap.getOverlays().get(i)).setOnMarkerClickListener(new Marker.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker, MapView mapView) {
DrawRoad drawRoad = new DrawRoad(getCurrentLocation(), marker.getPosition());
drawRoad.execute();
Toast.makeText(MapActivity.this, "Marker clicked!", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
}
//we do this in a different thread as it takes some amount of processing power to get route to ucd.
private class DrawRoad extends AsyncTask<Void, Void, Road> {
GeoPoint startPoint;
GeoPoint endPoint;
public DrawRoad(GeoPoint startPoint, GeoPoint endPoint){
this.startPoint = startPoint;
this.endPoint = endPoint;
}
@Override
protected Road doInBackground(Void... voids) {
RoadManager roadManager = new OSRMRoadManager(MapActivity.this);
ArrayList<GeoPoint> waypoints = new ArrayList<>();
waypoints.add(startPoint);
waypoints.add(endPoint);
Road road = roadManager.getRoad(waypoints);
return road;
}
protected void onPostExecute(Road road){
updateUIWithRoad(road);
}
}
答案 0 :(得分:1)
您的代码是正确的,它应该有效。
使用哪个osmdroid版本?
调试时,您能否检查到达该行:
((Marker) bookMap.getOverlays().get(i)).setOnMarkerClickListener(