Android:我如何将地图视图的缩放级别设置为我当前位置周围1公里的半径?

我想将地图视图放大到1公里半径,但不知道如何?

该文件说,缩放级别1将地球赤道映射到256像素。 那么如何计算我需要设置的缩放级别,以便地图视图显示半径为1KM的区域?

更新:
阅读了一些博客文章后,我写了下面的代码:

private int calculateZoomLevel() { double equatorLength = 6378140; // in meters double widthInPixels = screenWidth; double metersPerPixel = equatorLength / 256; int zoomLevel = 1; while ((metersPerPixel * widthInPixels) > 2000) { metersPerPixel /= 2; ++zoomLevel; } Log.i("ADNAN", "zoom level = "+zoomLevel); return zoomLevel; } 

这个想法是,我首先计算缩放级别1中的每像素的米数 ,根据谷歌显示,使用256像素的赤道地球。 现在,随后的每个缩放级别都会放大2个级别,所以每个缩放级别的每个像素只有一个米。 我这样做,直到我有一个缩放级别米每像素乘以屏幕宽度给我less于2000年,即2公里。

但我不认为我得到的缩放级别是显示半径2公里的地图。 有人能告诉我我在做什么错吗?

虽然这个答案是合乎逻辑的,我觉得它的工作,但结果是不准确的,我不知道为什么,但我厌倦了这种方法,这种技术是更准确的。

1)在所需半径的物体上做一个圆圈

 Circle circle = mGoogleMap.addCircle(new CircleOptions().center(new LatLng(latitude, longitude)).radius(getRadiusInMeters()).strokeColor(Color.RED)); circle.setVisible(true); getZoomLevel(circle); 

2)将该对象传递给此function并设置缩放级别这里是一个链接

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

以下代码是结束使用。 鉴于屏幕宽度和事实,在缩放级别1的地球赤道是256像素长,每一个后续的缩放级别双倍表示地球赤道所需的像素的数量,以下函数返回缩放级别,屏幕将显示一个区域宽度2Km。

 private int calculateZoomLevel(int screenWidth) { double equatorLength = 40075004; // in meters double widthInPixels = screenWidth; double metersPerPixel = equatorLength / 256; int zoomLevel = 1; while ((metersPerPixel * widthInPixels) > 2000) { metersPerPixel /= 2; ++zoomLevel; } Log.i("ADNAN", "zoom level = "+zoomLevel); return zoomLevel; } 

我结束了使用从以下使用情况:

https://github.com/googlemaps/android-maps-utils

我从lib中提取类,所以你不需要整个库。 您不用设置缩放级别,而是使用边界。 结果是一样的。

代码正好显示1公里:

 animateToMeters(1000); private void animateToMeters(int meters){ int mapHeightInDP = 200; Resources r = getResources(); int mapSideInPixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mapHeightInDP, r.getDisplayMetrics()); LatLng point = new LatLng(0, 0); LatLngBounds latLngBounds = calculateBounds(point, meters); if(latLngBounds != null){ cameraUpdate = CameraUpdateFactory.newLatLngBounds(latLngBounds, mapSideInPixels, mapSideInPixels, MARKER_BOUNDS); if(mMap != null) mMap.animateCamera(cameraUpdate); } } private LatLngBounds calculateBounds(LatLng center, double radius) { return new LatLngBounds.Builder(). include(SphericalUtil.computeOffset(center, radius, 0)). include(SphericalUtil.computeOffset(center, radius, 90)). include(SphericalUtil.computeOffset(center, radius, 180)). include(SphericalUtil.computeOffset(center, radius, 270)).build(); } 

该类从lib中提取(稍微更改):

 public class SphericalUtil { static final double EARTH_RADIUS = 6371009; /** * Returns hav() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere. */ static double havDistance(double lat1, double lat2, double dLng) { return hav(lat1 - lat2) + hav(dLng) * cos(lat1) * cos(lat2); } /** * Returns haversine(angle-in-radians). * hav(x) == (1 - cos(x)) / 2 == sin(x / 2)^2. */ static double hav(double x) { double sinHalf = sin(x * 0.5); return sinHalf * sinHalf; } /** * Computes inverse haversine. Has good numerical stability around 0. * arcHav(x) == acos(1 - 2 * x) == 2 * asin(sqrt(x)). * The argument must be in [0, 1], and the result is positive. */ static double arcHav(double x) { return 2 * asin(sqrt(x)); } private SphericalUtil() {} /** * Returns the heading from one LatLng to another LatLng. Headings are * expressed in degrees clockwise from North within the range [-180,180). * @return The heading in degrees clockwise from north. */ public static double computeHeading(LatLng from, LatLng to) { // http://williams.best.vwh.net/avform.htm#Crs double fromLat = toRadians(from.latitude); double fromLng = toRadians(from.longitude); double toLat = toRadians(to.latitude); double toLng = toRadians(to.longitude); double dLng = toLng - fromLng; double heading = atan2( sin(dLng) * cos(toLat), cos(fromLat) * sin(toLat) - sin(fromLat) * cos(toLat) * cos(dLng)); return wrap(toDegrees(heading), -180, 180); } /** * Returns the LatLng resulting from moving a distance from an origin * in the specified heading (expressed in degrees clockwise from north). * @param from The LatLng from which to start. * @param distance The distance to travel. * @param heading The heading in degrees clockwise from north. */ public static LatLng computeOffset(LatLng from, double distance, double heading) { distance /= EARTH_RADIUS; heading = toRadians(heading); // http://williams.best.vwh.net/avform.htm#LL double fromLat = toRadians(from.latitude); double fromLng = toRadians(from.longitude); double cosDistance = cos(distance); double sinDistance = sin(distance); double sinFromLat = sin(fromLat); double cosFromLat = cos(fromLat); double sinLat = cosDistance * sinFromLat + sinDistance * cosFromLat * cos(heading); double dLng = atan2( sinDistance * cosFromLat * sin(heading), cosDistance - sinFromLat * sinLat); return new LatLng(toDegrees(asin(sinLat)), toDegrees(fromLng + dLng)); } /** * Returns the location of origin when provided with a LatLng destination, * meters travelled and original heading. Headings are expressed in degrees * clockwise from North. This function returns null when no solution is * available. * @param to The destination LatLng. * @param distance The distance travelled, in meters. * @param heading The heading in degrees clockwise from north. */ public static LatLng computeOffsetOrigin(LatLng to, double distance, double heading) { heading = toRadians(heading); distance /= EARTH_RADIUS; // http://lists.maptools.org/pipermail/proj/2008-October/003939.html double n1 = cos(distance); double n2 = sin(distance) * cos(heading); double n3 = sin(distance) * sin(heading); double n4 = sin(toRadians(to.latitude)); // There are two solutions for b. b = n2 * n4 +/- sqrt(), one solution results // in the latitude outside the [-90, 90] range. We first try one solution and // back off to the other if we are outside that range. double n12 = n1 * n1; double discriminant = n2 * n2 * n12 + n12 * n12 - n12 * n4 * n4; if (discriminant < 0) { // No real solution which would make sense in LatLng-space. return null; } double b = n2 * n4 + sqrt(discriminant); b /= n1 * n1 + n2 * n2; double a = (n4 - n2 * b) / n1; double fromLatRadians = atan2(a, b); if (fromLatRadians < -PI / 2 || fromLatRadians > PI / 2) { b = n2 * n4 - sqrt(discriminant); b /= n1 * n1 + n2 * n2; fromLatRadians = atan2(a, b); } if (fromLatRadians < -PI / 2 || fromLatRadians > PI / 2) { // No solution which would make sense in LatLng-space. return null; } double fromLngRadians = toRadians(to.longitude) - atan2(n3, n1 * cos(fromLatRadians) - n2 * sin(fromLatRadians)); return new LatLng(toDegrees(fromLatRadians), toDegrees(fromLngRadians)); } /** * Returns the LatLng which lies the given fraction of the way between the * origin LatLng and the destination LatLng. * @param from The LatLng from which to start. * @param to The LatLng toward which to travel. * @param fraction A fraction of the distance to travel. * @return The interpolated LatLng. */ public static LatLng interpolate(LatLng from, LatLng to, double fraction) { // http://en.wikipedia.org/wiki/Slerp double fromLat = toRadians(from.latitude); double fromLng = toRadians(from.longitude); double toLat = toRadians(to.latitude); double toLng = toRadians(to.longitude); double cosFromLat = cos(fromLat); double cosToLat = cos(toLat); // Computes Spherical interpolation coefficients. double angle = computeAngleBetween(from, to); double sinAngle = sin(angle); if (sinAngle < 1E-6) { return from; } double a = sin((1 - fraction) * angle) / sinAngle; double b = sin(fraction * angle) / sinAngle; // Converts from polar to vector and interpolate. double x = a * cosFromLat * cos(fromLng) + b * cosToLat * cos(toLng); double y = a * cosFromLat * sin(fromLng) + b * cosToLat * sin(toLng); double z = a * sin(fromLat) + b * sin(toLat); // Converts interpolated vector back to polar. double lat = atan2(z, sqrt(x * x + y * y)); double lng = atan2(y, x); return new LatLng(toDegrees(lat), toDegrees(lng)); } /** * Returns distance on the unit sphere; the arguments are in radians. */ private static double distanceRadians(double lat1, double lng1, double lat2, double lng2) { return arcHav(havDistance(lat1, lat2, lng1 - lng2)); } /** * Returns the angle between two LatLngs, in radians. This is the same as the distance * on the unit sphere. */ static double computeAngleBetween(LatLng from, LatLng to) { return distanceRadians(toRadians(from.latitude), toRadians(from.longitude), toRadians(to.latitude), toRadians(to.longitude)); } /** * Returns the distance between two LatLngs, in meters. */ public static double computeDistanceBetween(LatLng from, LatLng to) { return computeAngleBetween(from, to) * EARTH_RADIUS; } /** * Returns the length of the given path, in meters, on Earth. */ public static double computeLength(List<LatLng> path) { if (path.size() < 2) { return 0; } double length = 0; LatLng prev = path.get(0); double prevLat = toRadians(prev.latitude); double prevLng = toRadians(prev.longitude); for (LatLng point : path) { double lat = toRadians(point.latitude); double lng = toRadians(point.longitude); length += distanceRadians(prevLat, prevLng, lat, lng); prevLat = lat; prevLng = lng; } return length * EARTH_RADIUS; } /** * Returns the area of a closed path on Earth. * @param path A closed path. * @return The path's area in square meters. */ public static double computeArea(List<LatLng> path) { return abs(computeSignedArea(path)); } /** * Returns the signed area of a closed path on Earth. The sign of the area may be used to * determine the orientation of the path. * "inside" is the surface that does not contain the South Pole. * @param path A closed path. * @return The loop's area in square meters. */ public static double computeSignedArea(List<LatLng> path) { return computeSignedArea(path, EARTH_RADIUS); } /** * Returns the signed area of a closed path on a sphere of given radius. * The computed area uses the same units as the radius squared. * Used by SphericalUtilTest. */ static double computeSignedArea(List<LatLng> path, double radius) { int size = path.size(); if (size < 3) { return 0; } double total = 0; LatLng prev = path.get(size - 1); double prevTanLat = tan((PI / 2 - toRadians(prev.latitude)) / 2); double prevLng = toRadians(prev.longitude); // For each edge, accumulate the signed area of the triangle formed by the North Pole // and that edge ("polar triangle"). for (LatLng point : path) { double tanLat = tan((PI / 2 - toRadians(point.latitude)) / 2); double lng = toRadians(point.longitude); total += polarTriangleArea(tanLat, lng, prevTanLat, prevLng); prevTanLat = tanLat; prevLng = lng; } return total * (radius * radius); } /** * Returns the signed area of a triangle which has North Pole as a vertex. * Formula derived from "Area of a spherical triangle given two edges and the included angle" * as per "Spherical Trigonometry" by Todhunter, page 71, section 103, point 2. * See http://books.google.com/books?id=3uBHAAAAIAAJ&pg=PA71 * The arguments named "tan" are tan((pi/2 - latitude)/2). */ private static double polarTriangleArea(double tan1, double lng1, double tan2, double lng2) { double deltaLng = lng1 - lng2; double t = tan1 * tan2; return 2 * atan2(t * sin(deltaLng), 1 + t * cos(deltaLng)); } /** * Wraps the given value into the inclusive-exclusive interval between min and max. * @param n The value to wrap. * @param min The minimum. * @param max The maximum. */ static double wrap(double n, double min, double max) { return (n >= min && n < max) ? n : (mod(n - min, max - min) + min); } /** * Returns the non-negative remainder of x / m. * @param x The operand. * @param m The modulus. */ static double mod(double x, double m) { return ((x % m) + m) % m; } } 

谷歌地图似乎紧密地运行到英里/像素。 在缩放= 13,1英里= 100像素。 2英里= 200像素。 每个变焦镜头增加或减less2倍。因此,在变焦14,1英里= 200像素和变焦12,1英里= 50像素。

我已将接受的答案转换为返回双精度值,因为Android Google地图库使用浮点缩放级别,并且还考虑了远离赤道的纬度。

 public static double getZoomForMetersWide ( final double desiredMeters, final double mapWidth, final double latitude ) { final double latitudinalAdjustment = Math.cos( Math.PI * latitude / 180.0 ); final double arg = EQUATOR_LENGTH * mapWidth * latitudinalAdjustment / ( desiredMeters * 256.0 ); return Math.log( arg ) / Math.log( 2.0 ); } 

另外,为了达到Android上的最佳效果,不要通过视图的真实像素数,而是根据设备的像素密度缩放尺寸。

 DisplayMetrics metrics = getResources().getDisplayMetrics(); float mapWidth = mapView.getWidth() / metrics.scaledDensity; 

希望这有助于某人。