Osmdroid:如何在标记旁边创建上下文菜单?
Osmdroid: How to create a Contextmenu next to a Marker?
我的目标:我想在 osmdroid-marker 旁边显示一个弹出菜单
我的问题:
- 在 Android 中,您可以将 android.widget.PopupMenu 附加到 android.view.View :菜单显示在视图旁边。
- 在 osmdroid 中,一个标记继承自 org.osmdroid.views.overlay.Overlay。不幸的是,Overlay 不是从 View 继承的,所以我无法将弹出窗口附加到它。
目前我正在将菜单附加到位于右下角但可能远离当前标记的图像。
public class LocationMapFragment extends DialogFragment {
...
protected boolean showContextMenu(final View parent, final int markerId,
final IGeoPoint geoPosition, final Object markerData) {
MenuInflater inflater = getActivity().getMenuInflater();
// currently the contextmenu is attached to some image "this.mImage"
// which does not correspont to "geoPosition"
// How to create a temporary 1x1 view at "geoPosition"
// where i can attach the menu to?
PopupMenu menu = new PopupMenu(getActivity(), this.mImage);
inflater.inflate(R.menu.menu_map_context, menu.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.cmd_gallery:
return showGallery(getiGeoPointById(markerId, geoPosition));
case R.id.cmd_zoom:
return zoomToFit(getiGeoPointById(markerId, geoPosition));
default:
return false;
}
}
});
menu.show();
return true;
}
}
我的问题:有没有一种简单的方法可以在我的 IGeoPoint geoPosition
标记顶部创建一个临时的 1x1 像素 window,我可以将弹出窗口附加到该位置?
或者有其他简单的方法来定位菜单吗?
使用 OSMBonusPack Marker InfoWindow(标记气泡)。
你可以用infowindow.getView()得到它的视图,这是一个普通的android.view.View。
您可以根据需要设计其布局以使其尽可能小。
就像MKer说的,你可以使用OSMBonusPack Marker InfoWindow,定制你自己的InfoWindow。
https://mobiledevstories.wordpress.com/2014/03/01/osmdroid-bonus-pack-markers-with-clickable-infowindows/
这是我的最终解决方案。 createTempPopupParentMenuView(GeoPoint position)
包含从 InfoWindow
中提取的代码,为位于标记位置的弹出菜单提供视图:
/** temporary 1x1 pix view where popup-menu is attached to */
private View tempPopupMenuParentView = null;
...
protected boolean showContextMenu(final View parent, final int markerId,
final IGeoPoint geoPosition, final Object markerData) {
MenuInflater inflater = getActivity().getMenuInflater();
mMapView.removeView(tempPopupMenuParentView);
PopupMenu menu = new PopupMenu(getActivity(), createTempPopupParentMenuView(new GeoPoint(geoPosition.getLatitudeE6(), geoPosition.getLongitudeE6())));
inflater.inflate(R.menu.menu_map_context, menu.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
mMapView.removeView(tempPopupMenuParentView);
switch (item.getItemId()) {
case R.id.cmd_gallery:
return showGallery(getiGeoPointById(markerId, geoPosition));
case R.id.cmd_zoom:
return zoomToFit(getiGeoPointById(markerId, geoPosition));
default:
return false;
}
}
});
menu.show();
return true;
}
// inspired by org.osmdroid.bonuspack.overlays.InfoWindow
private View createTempPopupParentMenuView(GeoPoint position) {
if (tempPopupMenuParentView != null) mMapView.removeView(tempPopupMenuParentView);
tempPopupMenuParentView = new View(getActivity());
MapView.LayoutParams lp = new MapView.LayoutParams(
1,
1,
position, MapView.LayoutParams.CENTER,
0, 0);
tempPopupMenuParentView.setVisibility(View.VISIBLE);
mMapView.addView(tempPopupMenuParentView, lp);
return tempPopupMenuParentView;
}
感谢@MKer 指引我正确的方向。
我的目标:我想在 osmdroid-marker 旁边显示一个弹出菜单
我的问题:
- 在 Android 中,您可以将 android.widget.PopupMenu 附加到 android.view.View :菜单显示在视图旁边。
- 在 osmdroid 中,一个标记继承自 org.osmdroid.views.overlay.Overlay。不幸的是,Overlay 不是从 View 继承的,所以我无法将弹出窗口附加到它。
目前我正在将菜单附加到位于右下角但可能远离当前标记的图像。
public class LocationMapFragment extends DialogFragment {
...
protected boolean showContextMenu(final View parent, final int markerId,
final IGeoPoint geoPosition, final Object markerData) {
MenuInflater inflater = getActivity().getMenuInflater();
// currently the contextmenu is attached to some image "this.mImage"
// which does not correspont to "geoPosition"
// How to create a temporary 1x1 view at "geoPosition"
// where i can attach the menu to?
PopupMenu menu = new PopupMenu(getActivity(), this.mImage);
inflater.inflate(R.menu.menu_map_context, menu.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.cmd_gallery:
return showGallery(getiGeoPointById(markerId, geoPosition));
case R.id.cmd_zoom:
return zoomToFit(getiGeoPointById(markerId, geoPosition));
default:
return false;
}
}
});
menu.show();
return true;
}
}
我的问题:有没有一种简单的方法可以在我的 IGeoPoint geoPosition
标记顶部创建一个临时的 1x1 像素 window,我可以将弹出窗口附加到该位置?
或者有其他简单的方法来定位菜单吗?
使用 OSMBonusPack Marker InfoWindow(标记气泡)。
你可以用infowindow.getView()得到它的视图,这是一个普通的android.view.View。
您可以根据需要设计其布局以使其尽可能小。
就像MKer说的,你可以使用OSMBonusPack Marker InfoWindow,定制你自己的InfoWindow。 https://mobiledevstories.wordpress.com/2014/03/01/osmdroid-bonus-pack-markers-with-clickable-infowindows/
这是我的最终解决方案。 createTempPopupParentMenuView(GeoPoint position)
包含从 InfoWindow
中提取的代码,为位于标记位置的弹出菜单提供视图:
/** temporary 1x1 pix view where popup-menu is attached to */
private View tempPopupMenuParentView = null;
...
protected boolean showContextMenu(final View parent, final int markerId,
final IGeoPoint geoPosition, final Object markerData) {
MenuInflater inflater = getActivity().getMenuInflater();
mMapView.removeView(tempPopupMenuParentView);
PopupMenu menu = new PopupMenu(getActivity(), createTempPopupParentMenuView(new GeoPoint(geoPosition.getLatitudeE6(), geoPosition.getLongitudeE6())));
inflater.inflate(R.menu.menu_map_context, menu.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
mMapView.removeView(tempPopupMenuParentView);
switch (item.getItemId()) {
case R.id.cmd_gallery:
return showGallery(getiGeoPointById(markerId, geoPosition));
case R.id.cmd_zoom:
return zoomToFit(getiGeoPointById(markerId, geoPosition));
default:
return false;
}
}
});
menu.show();
return true;
}
// inspired by org.osmdroid.bonuspack.overlays.InfoWindow
private View createTempPopupParentMenuView(GeoPoint position) {
if (tempPopupMenuParentView != null) mMapView.removeView(tempPopupMenuParentView);
tempPopupMenuParentView = new View(getActivity());
MapView.LayoutParams lp = new MapView.LayoutParams(
1,
1,
position, MapView.LayoutParams.CENTER,
0, 0);
tempPopupMenuParentView.setVisibility(View.VISIBLE);
mMapView.addView(tempPopupMenuParentView, lp);
return tempPopupMenuParentView;
}
感谢@MKer 指引我正确的方向。