所以我在 Android Studio 中使用 OpenStreetMap 创建一个路线规划器应用程序,我想让用户能够点击地图并在该位置添加标记,这样添加标记的过程就像这样
到目前为止,我还没有完成 firebase 的工作,但是每当我单击地图甚至移动地图以查看它时,都会出现此对话框,我想将其更改为如果用户单击地图视图超过2秒或类似的东西然后对话框出现
这是我的标记的 Java 代码:
//Creating a new marker that is to be used by the user
marker = new Marker(mapView);
marker.setIcon(getResources().getDrawable(R.drawable.ic_marker));
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
mapView.getOverlays().add(marker);
mapView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
GeoPoint tapLocation = (GeoPoint) mapView.getProjection().fromPixels((int) event.getX(), (int) event.getY());
marker.setPosition(tapLocation);
alertDialog.show();
}
return false;
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Add a marker here?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Launch your form activity and pass the latitude and longitude of the clicked position
Intent intent = new Intent(MainActivity.this, MarkerOnMapActivity.class);
intent.putExtra("latitude", marker.getPosition().getLatitude());
intent.putExtra("longitude", marker.getPosition().getLongitude());
startActivity(intent);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mapView.getOverlays().remove(marker);
}
});
alertDialog = builder.create();
我厌倦了改用 onLongClickListner 方法,但这对我来说根本不起作用,但这是我不使用的 onLongClickListener 的代码:
// Add long-click listener to the map
mapView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Show confirmation dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Add a marker here?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Start new activity to display form
Intent intent = new Intent(MainActivity.this, MarkerOnMapActivity.class);
intent.putExtra("latitude", mapView.getMapCenter().getLatitude());
intent.putExtra("longitude", mapView.getMapCenter().getLongitude());
startActivity(intent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.create().show();
return true;
}
});
也很累 setOnMapLongClickListener 对我也不起作用