【发布时间】:2017-03-23 12:19:24
【问题描述】:
我正在尝试创建一个用户位置位于中心的 MKCoordinateRegion,并且在该区域内应该可以看到另一个坐标。简而言之,我试图以用户位置为中心,同时显示一定数量的注释。我的方法是查看第 N:th 最远坐标所在的位置,并尝试从用户位置坐标计算到该坐标的距离或坐标区域。
var topLeftCoord = CLLocationCoordinate2D(latitude: -90.0, longitude: 180.0)
var bottomRightCoord = CLLocationCoordinate2D(latitude: 90.0, longitude: -180.0)
topLeftCoord.longitude = fmin(topLeftCoord.longitude, outerLongitude)
topLeftCoord.latitude = fmax(topLeftCoord.latitude, outerLatitude)
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, outerLongitude)
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, outerLatitude)
topLeftCoord.longitude = fmin(topLeftCoord.longitude, userCoordinates.longitude)
topLeftCoord.latitude = fmax(topLeftCoord.latitude, userCoordinates.latitude)
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, userCoordinates.longitude)
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, userCoordinates.latitude)
let regionSpan = MKCoordinateSpanMake(fabs((topLeftCoord.latitude - bottomRightCoord.latitude)), fabs((bottomRightCoord.longitude - topLeftCoord.longitude)))
var region = MKCoordinateRegion(center: userCoordinates, span: regionSpan)
region = self.mapView.regionThatFits(region)
self.mapView.setRegion(region, animated: true)
这种工作,但我得到了一点抵消。我认为问题在于这里我没有考虑到用户位置应该在中心,所以这段代码只是确保用户和outerLatitude 和outerLongitude 的坐标在范围内。但是当以用户坐标为中心时,外部坐标不再可见。
我这辈子都想不通。我也试过这样做:
let outerLocation = CLLocation(latitude: outerLatitude, longitude: outerLongitude)
let userLocation = CLLocation(latitude: userCoordinates.latitude, longitude: userCoordinates.longitude)
let distance = userLocation.distance(from: outerLocation)
var region = MKCoordinateRegionMakeWithDistance(userCoordinates, distance, distance)
region = self.mapView.regionThatFits(region)
self.mapView.setRegion(region, animated: true)
但我最终得到了相同的结果,我真的不明白。我得到一个大约 340m 的距离(这是相当合理的)。但是MKCoordinateRegionMakeWithDistance 要求latitudinalMeters 和longitudinalMeters,我只是将distance 从坐标A 发送到B。我找不到将组件拆分为latitudinalMeters 和longitudinalMeters 的方法,我可以简单地得到两者之间的1距离。
【问题讨论】: