我目前有一个应用程序,使用MapView向用户显示谷歌地图。
我一直在尝试使用此代码在地图上放置标记
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),
(int) event.getY());
DemoOverlay demoOverlay = new DemoOverlay();
mapView.getOverlays().add(demoOverlay);
mapView.invalidate();
}
return false;
}
我有一个Demo Overlay设置,它在地图上放置一个标记,但是当我触摸屏幕时,标记永远不会出现。
任何帮助将不胜感激!!!
答案 0 :(得分:2)
GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
Drawable srcdrawable = getApplicationContext().getResources().getDrawable(R.drawable.pin_blue);
CustomItemizedOverlay srcitemizedOverlay = new CustomItemizedOverlay(srcdrawable, getApplicationContext());
OverlayItem srcoverlayitem = new OverlayItem(p, "Hello!", "This is your Location.");
srcitemizedOverlay.addOverlay(srcoverlayitem);
mapView.getOverlays().clear();
mapView.getOverlays().add(srcitemizedOverlay);
mapController.animateTo(srcpoint);
mapController.setZoom(16);
在ontouch方法中使用上面的代码也使用下面的CustomItemizedOverlay.java类
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private final ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}
答案 1 :(得分:2)
android-mapviewballoons实际上是Android上最好的地图库之一。你为什么不试一试? :)
答案 2 :(得分:2)
答案 3 :(得分:2)
像这样修改你的代码---
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1) {
p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),p.getLatitudeE6() / 1E6 + "," +p.getLongitudeE6() /1E6 ,Toast.LENGTH_SHORT).show();
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.map_point);
AddItemizedOverlay itemizedOverlay =
new AddItemizedOverlay(drawable, this);
OverlayItem overlayitem = new OverlayItem(p, "", "");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
return true;
}
return false;
}
制作叠加层 -
AddItemizedOverlay.class -
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
@Override
protected boolean onTap(int index) {
Log.e("Tap", "Tap Performed");
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}
当你触摸屏幕时它会显示出来。
答案 4 :(得分:0)
感谢您的建议,但我决定按照这些教程,因为它们涵盖了我需要的内容, http://thenewboston.org/watch.php?cat=6&number=133
此视频特别是涵盖我的问题的视频,