在mapview中添加错误的地方?

时间:2012-01-16 14:39:03

标签: android android-mapview

我正在使用Google地图。我的问题是,当我触摸屏幕mapview没有添加完全正确的地方。添加几乎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());
    }

}

3 个答案:

答案 0 :(得分:0)

以下是mapview的完整教程:comprehensive google maps tutorials

你可以创建一个覆盖类;在onDraw()中,您应该使用getIntrinsicHeight() getIntrinsicWidth()/2

此处还有一个关于itemized overlays的教程,您可以使用boundCenterBottom()正确定位您的标记

答案 1 :(得分:0)

使用android-mapviewballoons库时,我遇到了同样的问题。找到解决方案需要半天时间。这是我的修复:

在CustomItemizedOverlay上,让我们在boundCenterBottom

中制作叠加层
public CustomItemizedOverlay(Drawable defaultMarker, MapView mapView) {
    super(boundCenterBottom(defaultMarker), mapView);
    c = mapView.getContext();
}

在你的Map Activty中,当创建标记drawable时,让我们做一个这样的技巧(感谢Cyril Mottier为他的Polaris mapview library):

drawable = MapViewUtils.boundMarkerCenterBottom(getResources().getDrawable(R.drawable.map_pin_holed_violet));

MapViewUtils来自Polaris库。这是它的boundMarkerCenterBottom函数:

public static Drawable boundMarkerCenterBottom(Drawable marker) {
    return boundMarker(marker, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
}
public static Drawable boundMarker(Drawable marker, int gravity) {
    if (marker == null) {
        return null;
    }

    final int width = marker.getIntrinsicWidth();
    final int height = marker.getIntrinsicHeight();

    if (width < 0 || height < 0) {
        throw new IllegalStateException("The given Drawable has no intrinsic width or height");
    }

    int left, top;

    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
        case Gravity.LEFT:
            left = 0;
            break;
        case Gravity.RIGHT:
            left = -width;
            break;
        case Gravity.CENTER_HORIZONTAL:
        default:
            left = -width / 2;
            break;
        }

    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
        case Gravity.TOP:
            top = 0;
            break;
        case Gravity.CENTER_VERTICAL:
            top = -height / 2;
            break;
        case Gravity.BOTTOM:
        default:
            top = -height;
            break;
    }

    marker.setBounds(left, top, left + width, top + height);
    return marker;
}

答案 2 :(得分:0)

我不确定,但我认为问题可能是在获得投影后设置缩放。其实我。有一个教程可以在我为AndroidDev101提供的博客上完全按照您的要求进行操作,该教程应该为您提供完成添加点的任务所需的内容。

Blog