0

我创建了显示地图的 android 应用程序。但不幸的是,所有标记显示的位置都略低于其实际位置。我验证了经度和纬度,它们是当前的。

我试过了

marker.setBounds(0, 0, 0 + marker.getIntrinsicWidth(), 0 + marker.getIntrinsicHeight()); 

这是我的标记加载代码

                 while (itr.hasNext()) {


            Business business = itr.next();

            point = new GeoPoint((int)(business.getLatitude() * 1E6),
                    (int)(business.getLongitude() * 1E6));



            List<Overlay> mapOverlays = mMapView.getOverlays();
            LinearLayout markerLayout = (LinearLayout)getLayoutInflater().inflate(
                    R.layout.map_loc_bubble, null);
            CustomItem overlayItem = new CustomItem(point, business, markerLayout,
                    getApplicationContext());

            CustomOverlay itemizedOverlay = new CustomOverlay(overlayItem.getDefaultMarker(),this);
            itemizedOverlay.addOverlay(overlayItem);

            mapOverlays.add(itemizedOverlay);

            mMapView.invalidate();

    }

    if (centerPoint != null) {
        mMapView.getController().setCenter(centerPoint);
        mMapView.getController().animateTo(centerPoint);
    }
}

但没有运气

这是我的覆盖类

 class CustomOverlay extends ItemizedOverlay<CustomItem> {


    public CustomOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
    }


    public CustomOverlay(Drawable defaultMarker, Context context)
    {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    @Override
    protected CustomItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    public void addOverlay(CustomItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    public void addOverlay(CustomItem overlay, Drawable marker) {


        marker.setBounds(0, 0, 0 + marker.getIntrinsicWidth(), 0 + marker.getIntrinsicHeight()); 
        overlay.setMarker(marker);
            addOverlay(overlay);
    }

    @Override
    public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {
        super.draw(canvas, mapView, false);
    }

    }

}
4

1 回答 1

1

使用 Projection 将您的 Overlay 映射到 MapView:如下所示使用它

在您的叠加层中,将此代码添加到绘图方法:

Projection projection = mapView.getProjection();


//GeoPoint class is your latitude, longitude.
GeoPoint point = //TODO assign long, lat

//This is your point on the map
Point myPoint = new Point();

projection.toPixels (point, myPoint);

然后你需要某种坐标系来锚定。您可以使用:

// Mark some points through which to draw your circle, or you can do something else
 //This just draws circles of radius 5
RectF myShape = new RectF (myPoint.x-5, myPoint.y-5, myPoint.x+5, myPoint.y+5);

canvas.drawOval (mShape, paint);
于 2012-12-11T13:45:14.453 回答