我正在使用 Google map 。我的问题是当我触摸屏幕 mapview 时没有在完全正确的位置添加 pin。添加接近 Ycoordinate-50。
我也添加了 CustomitemzedOverlay。
代码有什么问题?
谁能帮帮我?
代码:
public void AddPoint(Drawable drawable, MapView mapView, MotionEvent motionEvent) {
p = mapView.getProjection().fromPixels((int) (motionEvent.getX()),(int) (motionEvent.getY()));
final MapController mc = mapView.getController();
mc.setZoom(16);
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView);
itemizedOverlay.addOverlay(new CustomOverlayItem(p,"","",""));
mapView.getOverlays().add(itemizedOverlay);
mc.animateTo(p);
mapView.invalidate();
}
package com.example;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Toast;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import java.util.ArrayList;
public class CustomItemizedOverlay<Item extends OverlayItem> extends BalloonItemizedOverlay<CustomOverlayItem> {
private ArrayList<CustomOverlayItem> m_overlays = new ArrayList<CustomOverlayItem>();
private Context c;
public CustomItemizedOverlay(Drawable defaultMarker, MapView mapView) {
super(boundCenter(defaultMarker), mapView);
c = mapView.getContext();
}
public void addOverlay(CustomOverlayItem overlay) {
m_overlays.add(overlay);
populate();
}
public void removeOverlay(CustomOverlayItem overlay) {
m_overlays.remove(overlay);
populate();
}
@Override
protected CustomOverlayItem createItem(int i) {
return m_overlays.get(i);
}
@Override
public int size() {
return m_overlays.size();
}
@Override
protected boolean onBalloonTap(int index, CustomOverlayItem item) {
Toast.makeText(c, "onBalloonTap for overlay index " + index,
Toast.LENGTH_LONG).show();
return true;
}
@Override
protected BalloonOverlayView<CustomOverlayItem> createBalloonOverlayView() {
// use our custom balloon view with our custom overlay item type:
return new CustomBalloonOverlayView<CustomOverlayItem>(getMapView().getContext(), getBalloonBottomOffset());
}
}
最佳答案
这是一个关于 mapview 的完整教程:comprehensive google maps tutorials
你可以创建一个覆盖类;在 onDraw()
中,你应该使用 getIntrinsicHeight()
getIntrinsicWidth()/2
这里还有关于 itemized overlays 的教程你可以在哪里使用 boundCenterBottom()
来正确定位你的标记
关于android - 在 map View 中添加错误的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8881525/