10

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

4

5 回答 5

23

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

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;
}
于 2014-12-26T08:16:55.570 回答
3

构建.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) );
于 2018-07-15T13:58:26.940 回答
1

很长一段时间后,我从某个地方找到了解决方案。

这是给我最小纬度/经度和最大纬度/经度的方法。基于此,我得到了 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

你可以试试,很酷的东西

于 2012-08-03T11:32:02.980 回答
0

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

它在我的项目中 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;
}
于 2019-03-26T02:23:50.577 回答
0
  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

于 2021-07-03T12:30:31.617 回答