我基本上有一张地图,我添加了两个注释,它们按预期显示。
然后我使用以下代码:
let annotations = [start, end]
mapView?.showAnnotations(annotations, animated: false)
这会缩放以显示带有两个注释的地图,但地图可以放大更多并显示更详细的地图视图。
我想要的This is what I get today和this is the expected result。
正如您所看到的,地图可以放大很多。
任何想法如何实现这一目标?
答案 0 :(得分:9)
使用此:
extension MKMapView {
/// when we call this function, we have already added the annotations to the map, and just want all of them to be displayed.
func fitAll() {
var zoomRect = MKMapRectNull;
for annotation in annotations {
let annotationPoint = MKMapPointForCoordinate(annotation.coordinate)
let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.01, 0.01);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(100, 100, 100, 100), animated: true)
}
/// we call this function and give it the annotations we want added to the map. we display the annotations if necessary
func fitAll(in annotations: [MKAnnotation], andShow show: Bool) {
var zoomRect:MKMapRect = MKMapRectNull
for annotation in annotations {
let aPoint = MKMapPointForCoordinate(annotation.coordinate)
let rect = MKMapRectMake(aPoint.x, aPoint.y, 0.1, 0.1)
if MKMapRectIsNull(zoomRect) {
zoomRect = rect
} else {
zoomRect = MKMapRectUnion(zoomRect, rect)
}
}
if(show) {
addAnnotations(annotations)
}
setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100), animated: true)
}
}
然后,在为MapView创建一个插座后,例如map
,并将注释添加到名为annotations
的数组的地图中,访问上述方法,如下所示:< / p>
map.fitAll()
OR
map.fitAll(in:annotations, true)
分别
在最后一个语句中使用true或false,具体取决于您之前是否已将注释添加到地图中...
答案 1 :(得分:4)
In Swift:
inverse.rle
This is what I use to show all annotations. almost the same as you are doing yes. the map zoom this to fit the visible or usable section of the map, on my app the zoom is good and does add some padding so that none of the pins are touching the edges.
so possibly once the maps is done you can quickly use setVisibleMapRect:edgePadding:animated: using the new visibleRect with a negative padding.
答案 2 :(得分:3)
nyxee答案的Swift 4.2版本
extension MKMapView {
/// When we call this function, we have already added the annotations to the map, and just want all of them to be displayed.
func fitAll() {
var zoomRect = MKMapRect.null;
for annotation in annotations {
let annotationPoint = MKMapPoint(annotation.coordinate)
let pointRect = MKMapRect(x: annotationPoint.x, y: annotationPoint.y, width: 0.01, height: 0.01);
zoomRect = zoomRect.union(pointRect);
}
setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100), animated: true)
}
/// We call this function and give it the annotations we want added to the map. we display the annotations if necessary
func fitAll(in annotations: [MKAnnotation], andShow show: Bool) {
var zoomRect:MKMapRect = MKMapRect.null
for annotation in annotations {
let aPoint = MKMapPoint(annotation.coordinate)
let rect = MKMapRect(x: aPoint.x, y: aPoint.y, width: 0.1, height: 0.1)
if zoomRect.isNull {
zoomRect = rect
} else {
zoomRect = zoomRect.union(rect)
}
}
if(show) {
addAnnotations(annotations)
}
setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100), animated: true)
}
}
答案 3 :(得分:1)
Swift 5.3 版本,可调整内边距。
extension MKMapView {
func fitAllAnnotations(with padding: UIEdgeInsets = UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100)) {
var zoomRect: MKMapRect = .null
annotations.forEach({
let annotationPoint = MKMapPoint($0.coordinate)
let pointRect = MKMapRect(x: annotationPoint.x, y: annotationPoint.y, width: 0.01, height: 0.01)
zoomRect = zoomRect.union(pointRect)
})
setVisibleMapRect(zoomRect, edgePadding: padding, animated: true)
}
func fit(annotations: [MKAnnotation], andShow show: Bool, with padding: UIEdgeInsets = UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100)) {
var zoomRect: MKMapRect = .null
annotations.forEach({
let aPoint = MKMapPoint($0.coordinate)
let rect = MKMapRect(x: aPoint.x, y: aPoint.y, width: 0.1, height: 0.1)
zoomRect = zoomRect.isNull ? rect : zoomRect.union(rect)
})
if show {
addAnnotations(annotations)
}
setVisibleMapRect(zoomRect, edgePadding: padding, animated: true)
}
}