将跨度值转换为地图视图上的米
每当用户放大或缩小地图时,我需要知道地图上当前显示了多少米(宽度或高度)。
我需要的是 MKCooperativeRegionMakeWithDistance 的反函数来计算当前地图跨度表示的距离。
我尝试了以下代码,但得到了错误的结果:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
MKMapRect mRect = self.map.visibleMapRect;
MKMapPoint northMapPoint = MKMapPointMake(MKMapRectGetMidX(mRect), MKMapRectGetMinY(mRect));
MKMapPoint southMapPoint = MKMapPointMake(MKMapRectGetMidX(mRect), MKMapRectGetMaxY(mRect));
self.currentDist = MKMetersBetweenMapPoints(northMapPoint, southMapPoint);
}
如果我将地图区域设置为 1500 米,结果会得到类似 1800 的结果。
感谢您的帮助, 文森特
Whenever the user zoom in or out the map i need to know how many meters are currently represented on the map (width or height).
What i need is the inverse function of MKCoordinateRegionMakeWithDistance to calculate the distance represented by the current map span.
I tried the following code but i get wrong results :
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
MKMapRect mRect = self.map.visibleMapRect;
MKMapPoint northMapPoint = MKMapPointMake(MKMapRectGetMidX(mRect), MKMapRectGetMinY(mRect));
MKMapPoint southMapPoint = MKMapPointMake(MKMapRectGetMidX(mRect), MKMapRectGetMaxY(mRect));
self.currentDist = MKMetersBetweenMapPoints(northMapPoint, southMapPoint);
}
If i set the map region to 1500 meters i get something like 1800 as a result..
Thanks for your help,
Vincent
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
实际上这是一个非常愚蠢的错误,如果我沿 X 轴执行相同的操作,那么我会得到正确的值:
Actually it was a really stupid mistake, if i do the same operation along the X axis then i get the correct value :
根据文档 http://developer.apple.com/library /ios/#documentation/MapKit/Reference/MapKitDataTypesReference/Reference/reference.html,
latitudeDelta
要在地图上显示的南北距离(以度为单位)。与根据纬度而变化的纵向距离不同,纬度一度始终约为 111 公里(69 英里)。
According to the docs http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MapKitDataTypesReference/Reference/reference.html,
latitudeDelta
The amount of north-to-south distance (measured in degrees) to display on the map. Unlike longitudinal distances, which vary based on the latitude, one degree of latitude is always approximately 111 kilometers (69 miles).
感谢大家的发帖。我有一个应用程序需要一英里半径才能计算出要获取多少位置记录,因此这很方便。对于将来可能遇到此问题的任何人来说,这是快速等效的。
Thanks for the post all. I have an app that required a mile radius to figure out how many location records to fetch so this came in handy. Here is the swift equivalent for anyone who might come across this in the future.
斯威夫特4.2
Swift 4.2
这是一种更简单的方法(以米为单位获取宽度和高度)......
Here's an easier way (to get width and height in meters)...
雨燕4
Swift 4