如何根据地图上的圆绘制找到缩放级别

时间:2012-07-03 11:07:26

标签: android google-maps

我在地图上绘制圆圈并指定半径,它将成功绘制圆圈。但是当我使用搜索栏更改圆圈的大小时,我需要在屏幕上放置圆圈并缩放该级别的地图,我不知道这一点,需要你的指南谢谢。

5 个答案:

答案 0 :(得分:13)

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

Circle circle = googleMap.addCircle(circleOptions);

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

// Methode for zoomlevel

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;
}

答案 1 :(得分:1)

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

这是给我min min / lng和max lat / lng的方法。 基于此,我得到了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,那么地球的半径就是km 6370.997

你可以尝试一下,很酷的东西

答案 2 :(得分:0)

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 :(得分: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;
}

答案 4 :(得分: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