【问题标题】:How to find zoom level based on circle draw on map如何根据地图上的圆圈绘制来查找缩放级别
【发布时间】:2012-07-03 11:07:25
【问题描述】:

我正在地图上绘制圆并指定半径,它会成功绘制圆。但是当我使用 seekbar 更改圆的大小时,我需要在屏幕上踩下圆并缩放该级别的地图,我对此一无所知,需要您的指导,谢谢。

【问题讨论】:

  • 如果是 Javascript API 你可以做 map.fitBounds(myCircle.getBounds());但我不知道Android中是否提供相同的功能。
  • 这是 android 我在这里指定了标签,我知道这一点,也知道 android 中可用的 spantozoom()。但是我有单点这个方法不可用,如果我发现从中心点到半径距离的纬度任何纬度那么这个方法很有用
  • 我找到了解决方案检查我的答案
  • 请在上面找到我的解决方案。我在我的一个项目中做了同样的事情。谢谢

标签: android google-maps


【解决方案1】:

我们还可以从绘制的圆圈中获取地图的缩放级别

Circle circle = googleMap.addCircle(circleOptions);

googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                        circleOptions.getCenter(), getZoomLevel(circle)

// 缩放级别的方法

public int getZoomLevel(Circle circle) {
    int zoomLevel = 11;
    if (circle != null) {
        double radius = circle.getRadius() + circle.getRadius() / 2;
        double scale = radius / 500;
        zoomLevel = (int) (16 - Math.log(scale) / Math.log(2));
    }
    return zoomLevel;
}

【讨论】:

  • 完美解决方案!谢谢@Anand Tiwari
  • @AnandTiwari 工作得几乎完美,对于某些值,我得到了 90% 的圆圈。你能解释一下/ 50016 - 吗?
  • 完美运行!非常感谢您的解决方案。
  • 你知道 500 代表什么吗?放大 500 是什么意思?
【解决方案2】:

build.gradle:

dependencies {
    compile 'com.google.maps.android:android-maps-utils:0.4+'
}

public static LatLngBounds getLatLngBoundsFromCircle(Circle circle){
    if(circle != null){
        return new LatLngBounds.Builder()
                .include(SphericalUtil.computeOffset(circle.getCenter(), circle.getRadius() * Math.sqrt(2), 45))
                .include(SphericalUtil.computeOffset(circle.getCenter(), circle.getRadius() * Math.sqrt(2), 225))
                .build();
    }
    return null;
}

map.animateCamera( CameraUpdateFactory
    .newLatLngBounds(MapUtils.getLatLngBoundsFromCircle(mapCircle),20) );

【讨论】:

    【解决方案3】:

    很久之后,我从某个地方找到了解决方案。

    这是给我最小纬度/经度和最大纬度/经度的方法。 基于此,我得到了 latspan 和 longspan。

    public void boundingCoordinates(double distance, double radius) {
    
        if (radius < 0d || distance < 0d)
            throw new IllegalArgumentException();
    
        // angular distance in radians on a great circle
        double radDist = distance / radius;
    
        double radLat = Math.toRadians(gp.getLatitudeE6()/1e6); // here is your single point latitude gp.getLatitude
        double radLon = Math.toRadians(gp.getLongitudeE6()/1e6); // here is your single point longitude gp.getlongitude
    
        double minLat = radLat - radDist;
        double maxLat = radLat + radDist;
    
        double minLon, maxLon;
        if (minLat > MIN_LAT && maxLat < MAX_LAT) {
            double deltaLon = Math.asin(Math.sin(radDist) /Math.cos(radLat));
            minLon = radLon - deltaLon;
    
            if (minLon < MIN_LON) 
                minLon += 2d * Math.PI;
    
            maxLon = radLon + deltaLon;
    
            if (maxLon > MAX_LON) 
                maxLon -= 2d * Math.PI;
        } else {
            // a pole is within the distance
            minLat = Math.max(minLat, MIN_LAT);
            maxLat = Math.min(maxLat, MAX_LAT);
            minLon = MIN_LON;
            maxLon = MAX_LON;
        }
    
        minLat = Math.toDegrees(minLat);
        minLon = Math.toDegrees(minLon);
        maxLat = Math.toDegrees(maxLat);
        maxLon = Math.toDegrees(maxLon);
    
        minGeo = new GeoPoint((int)(minLat*1e6),(int)(minLon*1e6));
        maxGeo = new GeoPoint((int)(maxLat*1e6),(int)(maxLon*1e6));
    }
    

    现在你通过任何单位的距离,例如你必须通过地球的半径,例如如果你通过2 km,那么地球的半径就是公里,比如6370.997

    你可以试试,很酷的东西

    【讨论】:

    • 感谢您的代码。但是 minGeo 和 maxGeo 有什么用呢?我如何缩放地图视图并适合圆圈
    • minGeo 和 maxGeo 用于最小/最大纬度和经度值,通过此您可以获取跨度纬度和经度值,以便使用 mapControl 对象传入 zoomToSpan 方法
    【解决方案4】:

    在我的代码中,我在标记周围添加了一个透明圆圈,该标记具有动态半径并缩放地图相机以适应屏幕。

    它在我的项目中 100% 运行良好。

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setOnMarkerClickListener(this);
    
        MapsInitializer.initialize(Objects.requireNonNull(getContext()));
    
        // Add a marker on Property location
        LatLng propertyLatlng = new LatLng(getLatitude(), getLongitude());
    
        //  draw transparent blue circle around marker
        try {
            CircleOptions circleOptions = new CircleOptions()
                    .center(propertyLatlng)
                    .radius(Double.parseDouble(radius) / 0.00062137)
                    .strokeColor(BLUE_TRANSPARENT)
                    .strokeWidth(0)
                    .fillColor(BLUE_TRANSPARENT);
            Circle circle = mMap.addCircle(circleOptions);
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                    circleOptions.getCenter(), getZoomLevel(circle)));
        } catch (Exception e) {
            AppLogger.logError(TAG, e.getMessage());
        }
    
    }
    
    /**
     * @param circle : circle
     * @return : return zoom level according to circle radius
     */
    public float getZoomLevel(Circle circle) {
        int zoomLevel = 11;
        if (circle != null) {
            double radius = circle.getRadius() + circle.getRadius() / 2;
            double scale = radius / 500;
            zoomLevel = (int) (16 - Math.log(scale) / Math.log(2));
        }
        return zoomLevel+.4f;
    }
    

    【讨论】:

      【解决方案5】:
        double getZoomLevel() {
          double zoomLevel = 0.0;
          double newRadius = radiusOfCircle + radiusOfCircle / 2;
          double scale = newRadius / 500;
          zoomLevel = (6 - log(scale) / log(2));
          return zoomLevel;
        }
      

      这对我有用

      感谢@Anand Tiwari

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多