如何使地图视图缩放到当前位置半径 5 英里

发布于 2024-10-17 13:24:56 字数 90 浏览 2 评论 0原文

我知道这是一个非常常见的问题,但我没有得到这件事的确切答案。

如何使 MKMapView 默认缩放当前位置 5 英里半径。

提前致谢。

I know its a very common issue but I am not getting the exact answer for this thing.

How to make MKMapView defaults to a zoom of a 5 mile radius of current location.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稚然 2024-10-24 13:24:56

使用 MapKit 中的MKCooperativeRegionMakeWithDistance 函数。

由于 MapKit 使用米,因此请使用转换函数将英里转换为米。

float MilesToMeters(float miles) {
    // 1 mile is 1609.344 meters
    // source: http://www.google.com/search?q=1+mile+in+meters
    return 1609.344f * miles;
}

然后在你的代码中将地图区域设置为(感谢@DylanBettermann指出要获得5英里的半径,距离需要加倍)

mapView.region = MKCoordinateRegionMakeWithDistance(
    centerCoordinate, 
    MilesToMeters(10.0f),
    MilesToMeters(10.0f)
);

swift 4版本:

mapView.region = MKCoordinateRegion(
            center: centerCoordinate,
            latitudinalMeters: MilesToMeters(10.0f),
            longitudinalMeters: MilesToMeters(10.0f)
)

Use the MKCoordinateRegionMakeWithDistance function from MapKit.

Use a conversion function to convert miles to meters since MapKit uses meters.

float MilesToMeters(float miles) {
    // 1 mile is 1609.344 meters
    // source: http://www.google.com/search?q=1+mile+in+meters
    return 1609.344f * miles;
}

Then in your code set the map region as (thanks to @DylanBettermann for pointing out that to get a radius of 5 miles, the distance needs to be doubled)

mapView.region = MKCoordinateRegionMakeWithDistance(
    centerCoordinate, 
    MilesToMeters(10.0f),
    MilesToMeters(10.0f)
);

swift 4 version :

mapView.region = MKCoordinateRegion(
            center: centerCoordinate,
            latitudinalMeters: MilesToMeters(10.0f),
            longitudinalMeters: MilesToMeters(10.0f)
)
陌上芳菲 2024-10-24 13:24:56

当您想要缩放到 5 英里半径时,请使用以下代码:

double miles = 5.0;
double scalingFactor = ABS( (cos(2 * M_PI * newLocation.coordinate.latitude / 360.0) ));

MKCoordinateSpan span; 

span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/(scalingFactor * 69.0); 

MKCoordinateRegion region;
region.span = span;
region.center = newLocation.coordinate;

[mapView setRegion:region animated:YES];

Use the following code when ever you want to zoom to 5 miles radius:

double miles = 5.0;
double scalingFactor = ABS( (cos(2 * M_PI * newLocation.coordinate.latitude / 360.0) ));

MKCoordinateSpan span; 

span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/(scalingFactor * 69.0); 

MKCoordinateRegion region;
region.span = span;
region.center = newLocation.coordinate;

[mapView setRegion:region animated:YES];
独﹏钓一江月 2024-10-24 13:24:56
[self.theMapView setRegion:MKCoordinateRegionMake(
                    [theConsumer.latLong.clLocation coordinate], 
                    MKCoordinateSpanMake( MAP_SPAN, MAP_SPAN ))
                 animated:YES];

MKCoordinateSpanMake 的参数以度为单位,1 度大约是赤道处的 69 英里。因此 MAP_SPAN 将为 5.0/69.0

[self.theMapView setRegion:MKCoordinateRegionMake(
                    [theConsumer.latLong.clLocation coordinate], 
                    MKCoordinateSpanMake( MAP_SPAN, MAP_SPAN ))
                 animated:YES];

The parameters to MKCoordinateSpanMake are in degrees, and 1 degree is approx 69 miles at the equator. Thus MAP_SPAN would be 5.0/69.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文