【问题标题】:Adding pin wrong place in mapview?在地图视图中添加引脚错误的位置?
【发布时间】:2012-01-16 14:39:02
【问题描述】:

我正在使用谷歌地图。我的问题是当我触摸屏幕时,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());
    }

}

【问题讨论】:

  • 如果您使用逐项叠加,那么我认为您的问题与 boundCenterBottom();
  • 我更新了我的代码。boundCenterBottom()相关是什么意思。请帮帮我。

标签: android android-mapview


【解决方案1】:

这里有一个完整的 mapview 教程:comprehensive google maps tutorials

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

这里还有一个关于itemized overlays 的教程,您可以在其中使用boundCenterBottom() 正确定位您的标记

【讨论】:

    【解决方案2】:

    在使用android-mapviewballoons 库时,我遇到了和你一样的问题。我花了半天时间才找到解决方案。这是我的解决方法:

    在您的 CustomItemizedOverlay 上,让覆盖在 boundCenterBottom 中

    public CustomItemizedOverlay(Drawable defaultMarker, MapView mapView) {
        super(boundCenterBottom(defaultMarker), mapView);
        c = mapView.getContext();
    }
    

    在你的 Map Activity 中,当创建可绘制标记时,让我们做一个这样的技巧(感谢 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;
    }
    

    【讨论】:

      【解决方案3】:

      我不确定,但我认为问题可能是在获得投影后设置缩放。其实我。在我贡献给AndroidDev101 的博客上,有一个关于做你想做的事的教程,它应该为你提供完成添加点任务所需的内容。

      Blog

      【讨论】:

        猜你喜欢
        • 2013-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-11
        • 2017-03-14
        相关资源
        最近更新 更多