【问题标题】:How to zoom a MapView so it always includes two geopoints?如何缩放 MapView 使其始终包含两个地理点?
【发布时间】:2011-09-22 10:22:32
【问题描述】:

我有两个间歇性变化的地理点,并希望 MapView 调整大小和平移以确保两个点始终可见。我可以轻松地将地图重新​​居中于两者之间的中间点,但如何设置缩放级别以确保我的两个点可见?

【问题讨论】:

    标签: android android-mapview


    【解决方案1】:

    在另一个帖子中查看他的答案: Google Map V2 how to set ZoomToSpan?

    它毫不费力地解决了它。请注意,您只能在地图至少加载一次后使用该功能。如果不在屏幕上使用指定地图大小的函数

    LatLngBounds bounds = new LatLngBounds.Builder()
                        .include(new LatLng(CURRENTSTOP.latitude, CURRENTSTOP.longitude))
                        .include(new LatLng(MYPOSITION.latitude, MYPOSITION.longitude)).build();
    
    Point displaySize = new Point();
    getWindowManager().getDefaultDisplay().getSize(displaySize);
    
    map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, displaySize.x, 250, 30));
    

    像魅力一样工作,非常有活力!

    【讨论】:

    • 优秀的解决方案。为我工作。
    • 当你有 2 个或更多点时效果很好,当你只有一个时,你应该使用另一种解决方案。
    【解决方案2】:

    试试这个看看:

    double latitudeSpan = Math.round(Math.abs(firstLat - 
                    secLat));
    double longitudeSpan = Math.round(Math.abs(firstLong - 
                    secLong));
    
    double currentLatitudeSpan = (double)mapView.getLatitudeSpan();
    double currentLongitudeSpan = (double)mapView.getLongitudeSpan();
    
    double ratio = currentLongitudeSpan/currentLatitudeSpan;
    if(longitudeSpan < (double)(latitudeSpan+2E7) * ratio){
        longitudeSpan = ((double)(latitudeSpan+2E7) * ratio);
    }
    
    mapController.zoomToSpan((int)(latitudeSpan*2), (int)(longitudeSpan*2));                
    mapView.invalidate();
    

    【讨论】:

    • 嗯,这对我不起作用——总是将缩放级别设置为 0(即向我展示整个世界地图)。我删除了 longitudeSpan 的 if (longitudeSpan &lt; ...){...} 适配,现在它工作正常。还要记住 firstLat,firstLong,secLat,secLong 来自 GeoPoints,即原始纬度/经度值 * 1E6。
    【解决方案3】:

    在新的 Maps API (v2) 中,您可以这样做:

    LatLng southwest = new LatLng(Math.min(laglng1.latitude, laglng2.latitude), Math.min(laglng1.longitude, laglng2.longitude));
    LatLng northeast = new LatLng(Math.max(laglng1.latitude, laglng2.latitude), Math.max(laglng1.longitude, laglng2.longitude));
    googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds(southwest, northeast),500,500, 0));
    

    【讨论】:

      猜你喜欢
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      • 2011-04-14
      • 1970-01-01
      相关资源
      最近更新 更多