MKCoordinateRegionMakeWithDistance相当于Android

前端之家收集整理的这篇文章主要介绍了MKCoordinateRegionMakeWithDistance相当于Android前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们可以在iPhone上为地图上的特定位置设置周边地区,如下所示
CLLocationCoordinate2D coord = {latitude:37.09024,longitude:-95.712891};
CLLocationDistance latitudinalMeters;
latitudinalMeters =NoOfMiles * 1609.344;
CLLocationDistance longitudinalMeters;
longitudinalMeters = NoOfMiles * 1609.344;
mapViewHome.region = MKCoordinateRegionMakeWithDistance(coord,latitudinalMeters,longitudinalMeters);

Android有什么等同的方法吗?

解决方法

代码不生产质量.使用Chris的建议来代表这里: https://code.google.com/p/gmaps-api-issues/issues/detail?id=5704

原来这个问题是Maps API v1.这个答案是针对v2,但是可以很容易地变成v1,所以…

没有简单的方法来做到这一点.

你可能想要request this feature on gmaps-api-issues.

等待这个在Google方面实施可能需要几个月的时间,所以这就是我会做的:

private static final double ASSUMED_INIT_LATLNG_DIFF = 1.0;
private static final float ACCURACY = 0.01f;

public static LatLngBounds boundsWithCenterAndLatLngDistance(LatLng center,float latDistanceInMeters,float lngDistanceInMeters) {
    latDistanceInMeters /= 2;
    lngDistanceInMeters /= 2;
    LatLngBounds.Builder builder = LatLngBounds.builder();
    float[] distance = new float[1];
    {
        boolean foundMax = false;
        double foundMinLngDiff = 0;
        double assumedLngDiff = ASSUMED_INIT_LATLNG_DIFF;
        do {
            Location.distanceBetween(center.latitude,center.longitude,center.latitude,center.longitude + assumedLngDiff,distance);
            float distanceDiff = distance[0] - lngDistanceInMeters;
            if (distanceDiff < 0) {
                if (!foundMax) {
                    foundMinLngDiff = assumedLngDiff;
                    assumedLngDiff *= 2;
                } else {
                    double tmp = assumedLngDiff;
                    assumedLngDiff += (assumedLngDiff - foundMinLngDiff) / 2;
                    foundMinLngDiff = tmp;
                }
            } else {
                assumedLngDiff -= (assumedLngDiff - foundMinLngDiff) / 2;
                foundMax = true;
            }
        } while (Math.abs(distance[0] - lngDistanceInMeters) > lngDistanceInMeters * ACCURACY);
        LatLng east = new LatLng(center.latitude,center.longitude + assumedLngDiff);
        builder.include(east);
        LatLng west = new LatLng(center.latitude,center.longitude - assumedLngDiff);
        builder.include(west);
    }
    {
        boolean foundMax = false;
        double foundMinLatDiff = 0;
        double assumedLatDiffNorth = ASSUMED_INIT_LATLNG_DIFF;
        do {
            Location.distanceBetween(center.latitude,center.latitude + assumedLatDiffNorth,distance);
            float distanceDiff = distance[0] - latDistanceInMeters;
            if (distanceDiff < 0) {
                if (!foundMax) {
                    foundMinLatDiff = assumedLatDiffNorth;
                    assumedLatDiffNorth *= 2;
                } else {
                    double tmp = assumedLatDiffNorth;
                    assumedLatDiffNorth += (assumedLatDiffNorth - foundMinLatDiff) / 2;
                    foundMinLatDiff = tmp;
                }
            } else {
                assumedLatDiffNorth -= (assumedLatDiffNorth - foundMinLatDiff) / 2;
                foundMax = true;
            }
        } while (Math.abs(distance[0] - latDistanceInMeters) > latDistanceInMeters * ACCURACY);
        LatLng north = new LatLng(center.latitude + assumedLatDiffNorth,center.longitude);
        builder.include(north);
    }
    {
        boolean foundMax = false;
        double foundMinLatDiff = 0;
        double assumedLatDiffSouth = ASSUMED_INIT_LATLNG_DIFF;
        do {
            Location.distanceBetween(center.latitude,center.latitude - assumedLatDiffSouth,distance);
            float distanceDiff = distance[0] - latDistanceInMeters;
            if (distanceDiff < 0) {
                if (!foundMax) {
                    foundMinLatDiff = assumedLatDiffSouth;
                    assumedLatDiffSouth *= 2;
                } else {
                    double tmp = assumedLatDiffSouth;
                    assumedLatDiffSouth += (assumedLatDiffSouth - foundMinLatDiff) / 2;
                    foundMinLatDiff = tmp;
                }
            } else {
                assumedLatDiffSouth -= (assumedLatDiffSouth - foundMinLatDiff) / 2;
                foundMax = true;
            }
        } while (Math.abs(distance[0] - latDistanceInMeters) > latDistanceInMeters * ACCURACY);
        LatLng south = new LatLng(center.latitude - assumedLatDiffSouth,center.longitude);
        builder.include(south);
    }
    return builder.build();
}

用法

LatLngBounds bounds = AndroidMapsExtensionsUtils.boundsWithCenterAndLatLngDistance(new LatLng(51.0,19.0),1000,2000);
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds,0));

笔记:

>这段代码还没有被完全测试,可能不适用于边缘情况>您可能需要调整私有常量以使其执行得更快>您可以删除LatLng南方计算的第三部分,并对经度进行操作:对于较小的latDistance值,这将是准确的(猜测您在100公里以下看不到差异)>代码是丑陋的,所以随便重构

原文链接:https://www.f2er.com/android/310619.html

猜你在找的Android相关文章