android - 在 map View 中添加错误的位置?

标签 android android-mapview

我正在使用 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/

相关文章:

android - 有没有办法专门为三星设备设置应用程序图标?

Android - 任何大小文本的等宽布局

android - MapView 在 Android mvvm 数据绑定(bind)中不显示谷歌地图 [已解决]

android - MapView 和 AsyncTask

android - Android 中更高效的 map 叠加

java - Samsung S4 OS 4.3 上的时间选择器对话框崩溃

android - Android HAL 和 Linux 设备驱动程序的 sysfs 接口(interface)

android - 错误 : No resource identifier found for attribute 'adSize' in package 'com.google.example' main. xml

android - MapView onCLick 事件监听器

安卓 map : How to Long Click a Map?