我是初学者,我有2个坐标变量,当他们办理入住手续并在我的应用中结账时,可以根据用户位置动态更改。
获得这两个坐标后,我将使用此函数向地图工具包中添加注释。
func addMapAnnotation(coordinate: Coordinate, color: UIColor) {
let span : MKCoordinateSpan = MKCoordinateSpanMake(0.0005, 0.0005)
let location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude)
let region : MKCoordinateRegion = MKCoordinateRegionMake(location, span)
mapKitView.setRegion(region, animated: true)
let annotation = MyPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
annotation.pinTintColor = color
mapKitView.addAnnotation(annotation)
}
因为我设置了跨度和区域,并且2个坐标是动态的(即这两个坐标之间的距离可以是1公里,或者它可以是5公里,10公里等等),所以会有一个当视图控制器第一次出现时,其中一个注释没有直接显示的可能性,我必须先缩小才能看到第二个注释。
我想要的是,我想根据2个坐标的距离动态设置Map Kit跨度和区域,这样用户就不必缩小以查看两个注释
你能帮我分享一下代码吗?提前谢谢。答案 0 :(得分:1)
Sh_Khan的回答很好,尽管有些过时了。这是为Swift 5更新的扩展程序
extension MKCoordinateRegion {
init(coordinates: [CLLocationCoordinate2D], spanMultiplier: CLLocationDistance = 1.8) {
var topLeftCoord = CLLocationCoordinate2D(latitude: -90, longitude: 180)
var bottomRightCoord = CLLocationCoordinate2D(latitude: 90, longitude: -180)
for coordinate in coordinates {
topLeftCoord.longitude = min(topLeftCoord.longitude, coordinate.longitude)
topLeftCoord.latitude = max(topLeftCoord.latitude, coordinate.latitude)
bottomRightCoord.longitude = max(bottomRightCoord.longitude, coordinate.longitude)
bottomRightCoord.latitude = min(bottomRightCoord.latitude, coordinate.latitude)
}
let cent = CLLocationCoordinate2D.init(latitude: topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5, longitude: topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5)
let span = MKCoordinateSpan.init(latitudeDelta: abs(topLeftCoord.latitude - bottomRightCoord.latitude) * spanMultiplier, longitudeDelta: abs(bottomRightCoord.longitude - topLeftCoord.longitude) * spanMultiplier)
self.init(center: cent, span: span)
}
}
根据需要修改spanMultiplier
答案 1 :(得分:0)
调用此功能可缩小并显示所有注释
func zoomToFitMapAnnotations(aMapView:MKMapView)
{
if(aMapView.annotations.count == 0)
{
return
}
var topLeftCoord = CLLocationCoordinate2D.init(latitude: -90, longitude: 180)
var bottomRightCoord = CLLocationCoordinate2D.init(latitude: 90, longitude: -180)
for i in 0..<myMapView.annotations.count
{
let annotation = myMapView.annotations[i]
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
let resd = CLLocationCoordinate2D.init(latitude: topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5, longitude: topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5)
let span = MKCoordinateSpan.init(latitudeDelta: fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.3, longitudeDelta: fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.3)
var region = MKCoordinateRegion.init(center: resd, span: span);
region = aMapView.regionThatFits(region)
aMapView.setRegion(region, animated: true)
}