OSMdroid 边界标记

Posted

技术标签:

【中文标题】OSMdroid 边界标记【英文标题】:OSMdroid bounds marker 【发布时间】:2014-07-13 14:21:30 【问题描述】:

////OSMdroid 以标记为中心////

我添加了标记,我需要以所有标记都可见的方式映射最大增加或减少

我的代码:

public class mapcode extends Activity 
    globalvar appState;
    int stats=0;
    private MapView mapView;
    private IMapController mapController;
    private SimpleLocationOverlay mMyLocationOverlay;
    private ScaleBarOverlay mScaleBarOverlay;  
    ItemizedIconOverlay<OverlayItem> currentLocationOverlay;
    DefaultResourceProxyImpl resourceProxy;
    
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.map);
        
        appState = ((globalvar) getApplicationContext());

        
        
        
        
        mapView = (MapView) this.findViewById(R.id.mapview);  
        mapView.setTileSource(TileSourceFactory.MAPNIK);
      //  mapView.setBuiltInZoomControls(true); //кнопка ZOOM +-
        mapView.setMultiTouchControls(true);
        
        mapController = this.mapView.getController();
        mapController.setZoom(2);
        
        this.mMyLocationOverlay = new SimpleLocationOverlay(this);                          
        this.mapView.getOverlays().add(mMyLocationOverlay);
        
        this.mScaleBarOverlay = new ScaleBarOverlay(this);                          
        this.mapView.getOverlays().add(mScaleBarOverlay);
//      this.mapView
        
        /////////////////
        resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
        GeoPoint  currentLocation = new GeoPoint(55.860863,37.115046); 
        GeoPoint  currentLocation2 = new GeoPoint(63.557413,-156.102119); 
      
        
        OverlayItem myLocationOverlayItem = new OverlayItem("Here", "Current Position", currentLocation);
        Drawable myCurrentLocationMarker = this.getResources().getDrawable(R.drawable.a);
        myLocationOverlayItem.setMarker(myCurrentLocationMarker);
       // myLocationOverlayItem.setMarkerHotspot(HotspotPlace.CENTER); //no working/
        
        final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
        items.add(myLocationOverlayItem);
        
        
        myLocationOverlayItem = new OverlayItem("Here", "Current Position", currentLocation2);
        myCurrentLocationMarker = this.getResources().getDrawable(R.drawable.a);
        myLocationOverlayItem.setMarker(myCurrentLocationMarker);

  //    myLocationOverlayItem.setMarkerHotspot(HotspotPlace.CENTER); // no working

        
        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);
        
        
        this.mapView.getOverlays().add(this.currentLocationOverlay);

我添加了两个标记,但只有一个可见:

我需要将 osmdroid 居中并立即显示两个标记

【问题讨论】:

您可以使用zoomToBoundingBox 方法,但您需要遍历所有标记以确定顶部、左侧、底部和右侧坐标。 【参考方案1】:

我想你想要这样的东西:

int minLat = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int minLong = Integer.MAX_VALUE;
int maxLong = Integer.MIN_VALUE;
ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
for (OverlayItem item : items) 
    GeoPoint point = item.getPoint();
    if (point.getLatitudeE6() < minLat)
        minLat = point.getLatitudeE6();
    if (point.getLatitudeE6() > maxLat)
        maxLat = point.getLatitudeE6();
    if (point.getLongitudeE6() < minLong)
        minLong = point.getLongitudeE6();
    if (point.getLongitudeE6() > maxLong)
        maxLong = point.getLongitudeE6();


BoundingBoxE6 boundingBox = new BoundingBoxE6(maxLat, maxLong, minLat, minLong);
mMapView.zoomToBoundingBox(boundingBox);

【讨论】:

mMapView.zoomToBoundingBox(boundingBox); 之后添加mapController = mMapView.getController(); mapController.zoomToSpan(boundingBox.getLatitudeSpanE6(), boundingBox.getLongitudeSpanE6()); mapController.setCenter(boundingBox.getCenter()); 对我来说效果很好。 即使我设置了 BoundingBox boundingBox = new BoundingBox(north, west, south, east)也无法看到所有标记; boundingBox.getLongitudeSpanWithDateLine(); mmapView.zoomToBoundingBox(boundingBox, true);

以上是关于OSMdroid 边界标记的主要内容,如果未能解决你的问题,请参考以下文章

Android:Osmdroid 在触摸时添加标记不准确

在 osmdroid 中将叠加添加到 MapView

在osmdroid中获取地图上点击位置的经纬度

OSMDROID:使用 osmdroid 地图实时获取当前坐标

在没有移动地图集创建器的情况下获取 osmdroid 的地图图块

osmdroid 的用户代理