永远不会调用地图事件

时间:2015-11-24 19:58:10

标签: android openstreetmap osmdroid

我试图在用户点击或长按地图中的某个位置时放置一个标记,我在方法singleTapConfirmedHelper中祝酒并登录longPressHelper以了解会发生什么,我注意到了从来没有调用过这些方法,我做错了什么?我正在使用最新的osmdroid和osmbonuspack版本并且运行良好,这是我的代码:

public class MapActivity3 extends AppCompatActivity implements MapEventsReceiver {
MapView map;
TextView tv;
ArrayList<OverlayItem> overlayItemArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map3);

    tv=(TextView)findViewById(R.id.textView);

    map = (MapView) findViewById(R.id.map);
    map.setTileSource(TileSourceFactory.MAPNIK);

    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);

    IMapController mapController = map.getController();
    mapController.setZoom(14);
    GeoPoint startPoint = new GeoPoint(19.7059504, -101.19498249999998);
    mapController.setCenter(startPoint);

    Toast.makeText(MapActivity3.this, "Long press to add a marker", Toast.LENGTH_LONG).show();

    overlayItemArray = new ArrayList<>();

}

@Override
public boolean singleTapConfirmedHelper(GeoPoint geoPoint) {
    Toast.makeText(this, "Tapped", Toast.LENGTH_SHORT).show();
    return true;
}

@Override
public boolean longPressHelper(GeoPoint geoPoint) {
    Log.e("LongPress", "Inside");

    overlayItemArray.add(new OverlayItem("", "", geoPoint));
    ItemizedIconOverlay<OverlayItem> itemizedIconOverlay = new ItemizedIconOverlay<>(this, overlayItemArray, null);

    map.getOverlays().add(itemizedIconOverlay);

    Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
    String result;
    try {
        List<Address> addressList = geocoder.getFromLocation(
                geoPoint.getLatitude(), geoPoint.getLongitude(), 1);
        if (addressList != null && addressList.size() > 0) {
            Log.e("LongPress","1st if");
            Address address = addressList.get(0);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                sb.append(address.getAddressLine(i)).append("\n");
            }
            sb.append(address.getLocality()).append("\n");
            sb.append(address.getCountryName());
            result = sb.toString();
                tv.setText(result);
        }
    } catch (IOException e) {
        Log.e("LngPrss, Geocoder", "Unable connect to Geocoder", e);
    }

    return true;

} }

1 个答案:

答案 0 :(得分:0)

您需要做的是谨慎 tutorial

你错过了关于MapEventsOverlay的第1步。