当用户单击地图时,我试图在地图上添加一个标记,
当我在 onTouchEvent 中添加选项卡式位置标记时,地图会在选项卡式位置显示相应的标记,(标题和片段的值是硬编码的) .
但是,问题是,当我应该从用户那里获取标题和片段的值时,我创建了一个自定义提示对话框来输入详细信息。
当我将 addOverlay() 放在按钮的 onclick 事件处理程序中时,未显示相应的标记。
*使用 AVD
以下代码工作正常
public class MarkerOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> locationOverlayItems = new ArrayList<OverlayItem>();
private Context locationContext;
private OverlayItem tabbedLocation;
private AlertDialog promptDialog;
private LayoutInflater inflater;
private TextView info;
private EditText title;
private EditText description;
public MarkerOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
locationContext = context;
}
public void addOverlay(OverlayItem overlay) {
locationOverlayItems.add(overlay);
populate();
}
public void removeOverLay(OverlayItem overlay){
locationOverlayItems.remove(overlay);
populate();
}
@Override
protected OverlayItem createItem(int arg0) {
return locationOverlayItems.get(arg0);
}
@Override
public int size() {
return locationOverlayItems.size();
}
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == 1) {
int x = (int) event.getX();
int y = (int) event.getY();
final GeoPoint geoPoint = mapView.getProjection().fromPixels(x, y);
tabbedLocation = new OverlayItem(geoPoint, "title","description");
addOverlay(tabbedLocation);
return false;
}
}
}
这没有给出预期的结果
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == 1) {
int x = (int) event.getX();
int y = (int) event.getY();
final GeoPoint geoPoint = mapView.getProjection().fromPixels(x, y);
inflater = LayoutInflater.from(locationContext);
View prompt = inflater.inflate(R.layout.marker_prompt, null);
info = (TextView) prompt.findViewById(R.id.info);
info.setText(geoPoint.getLatitudeE6() / 1E6 + "," + geoPoint.getLongitudeE6() / 1E6);
title = (EditText) prompt.findViewById(R.id.title_text);
description = (EditText) prompt.findViewById(R.id.description_text);
promptDialog = new AlertDialog.Builder(locationContext)
.setView(prompt)
.setTitle("Add marker")
.setMessage("Specify the details")
.setPositiveButton("Add", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(!title.getText().toString().equals("") && !description.getText().toString().equals("")){
tabbedLocation = new OverlayItem(geoPoint, title.getText().toString(),
description.getText().toString());
addOverlay(tabbedLocation);
}
}
})
.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
promptDialog.cancel();
}
}).create();
promptDialog.show();
}
return false;
}
请帮忙解决
提前谢谢