ユーザーがマップをクリックしたときにマップにマーカーを追加しようとしています
。onTouchEvent内にタブ付きの場所マーカーを追加すると、タブ付きの場所に対応するマーカーを示すマップが表示されます(タイトルとスニペットの値はハードコーディングされています) 。
しかし、問題は、ユーザーからタイトルとスニペットの値を取得することになっているときに、詳細を入力するためのカスタムプロンプトダイアログを作成したことです。
ボタンのonclickイベントハンドラー内にaddOverlay()を配置すると、対応するマーカーが表示されません。
*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;
}
事前に感謝を解決するのを手伝ってください