0

我无法成功在我的地图上放置一个自定义的居中叠加项目。它总是出现在底部和中间的中心。这是我的代码:

------------我的叠加项目类-------------

public class EditionThumbOverlayItem extends OverlayItem {

    public EditionThumbOverlayItem(GeoPoint aGeoPoint, Resources resources) {
        super("", "", aGeoPoint);
        Drawable pinThumbDrawable = resources.getDrawable(R.drawable.pin_thumb);
        pinThumbDrawable.setBounds(pinThumbDrawable.getIntrinsicWidth() * (-2 / 5),
                pinThumbDrawable.getIntrinsicHeight() * (-2 / 5), pinThumbDrawable.getIntrinsicWidth() * 3 / 5,
                pinThumbDrawable.getIntrinsicHeight() * 3 / 5);
        setMarker(pinThumbDrawable);
    }

}

-------------我的逐项叠加类-----------------

public class PinThumbOverlay extends ItemizedOverlay<OverlayItem> {
    // Only one item can be set on this overlay
    private OverlayItem mEditionThumb;

    public PinThumbOverlay(Drawable pDefaultMarker, ResourceProxy pResourceProxy) {
        super(pDefaultMarker, pResourceProxy);
    }

    @Override
    public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3) {
        return false;
    }

    @Override
    protected OverlayItem createItem(int arg0) {
        return mEditionThumb;
    }

    @Override
    public int size() {
        if (mEditionThumb == null) {
            return 0;
        } else {
            return 1;
        }
    }

    public void addOverlayItem(OverlayItem overlay) {
        mEditionThumb = overlay;
        populate();
    }

    public void removeOverlayItem() {
        mEditionThumb = null;
        populate();
    }
}

------------------逐项叠加创建------------------

DefaultResourceProxyImpl defaultResouceProxyImpl = new DefaultResourceProxyImpl(getApplicationContext());
Drawable defaultPinEdition = getResources().getDrawable(R.drawable.pin_thumb);
                mPinThumbOverlay = new PinThumbOverlay(defaultPinEdition, defaultResouceProxyImpl);
mMapView.getOverlays().clear();
mMapView.getOverlays().add(mPinThumbOverlay);

-------------------叠加项目创建--------------

EditionThumbOverlayItem editionThumb = new EditionThumbOverlayItem(new GeoPoint(mCurrentUserLocation),
                getResources());
        mPinThumbOverlay.addOverlayItem(editionThumb);

注意:我使用 osmdroid 地图,而不是默认的 google map api。

4

2 回答 2

3

似乎 osmdroid 会覆盖您在 ItemizedOverlay 的 onDrawItem 方法中使用 drawble.setBounds() 方法设置的任何值:

protected void onDrawItem(final Canvas canvas, final Item item, final Point curScreenCoords) {
    final int state = (mDrawFocusedItem && (mFocusedItem == item) ? OverlayItem.ITEM_STATE_FOCUSED_MASK
            : 0);
    final Drawable marker = (item.getMarker(state) == null) ? getDefaultMarker(state) : item
            .getMarker(state);
    final HotspotPlace hotspot = item.getMarkerHotspot();

    boundToHotspot(marker, hotspot);

    // draw it
    Overlay.drawAt(canvas, marker, curScreenCoords.x, curScreenCoords.y, false);
}

或者更确切地说,实际覆盖将在 boundToHotspot(marker,hotspot); 中完成。方法。

要克服这个问题,请将热点设置在 OverlayItem 上,而不是设置可绘制对象的边界。例子:

GeoPoint point = new GeoPoint(currentLoc);
OverlayItem item = new OverlayItem("my location", "my loc", point);
item.setMarkerHotspot(HotspotPlace.BOTTOM_CENTER); 

这应该会产生您想要的结果。

于 2012-12-10T16:28:48.567 回答
0

如果您只想在地图中心放置一个标记,请使用以下代码片段:

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setMultiTouchControls(true);
        mapView.setClickable(true);
        mapView.setUseDataConnection(true);
        mapView.getController().setZoom(MAP_DEFAULT_ZOOM);
        mapView.setTileSource(TileSourceFactory.MAPNIK);
        resourceProxy = new DefaultResourceProxyImpl(getApplicationContext()); 

        GeoPoint location = getLocation()
        ItemizedIconOverlay<OverlayItem> currentLocationOverlay; 
        Drawable marker = this.getResources().getDrawable(R.drawable.custom_marker);
        OverlayItem myLocationOverlayItem = new OverlayItem("Here", "Current Position", location); 
        myLocationOverlayItem.setMarker(marker);  
        final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
        items.add(myLocationOverlayItem);
        currentLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                        return true;
                    }
                    public boolean onItemLongPress(final int index, final OverlayItem item) {
                        return true;
                    }
                }, resourceProxy);        

     mapView.getOverlays().add(this.currentLocationOverlay);
     mapView.getController().setCenter(location);

 } 
于 2012-07-12T06:50:14.920 回答