J D*_*Doe 7 zoom mkmapview swift
我基本上有一个地图,我添加了两个注释,它们按预期显示.
然后我使用以下代码:
let annotations = [start, end]
mapView?.showAnnotations(annotations, animated: false)
Run Code Online (Sandbox Code Playgroud)
这会缩放以显示带有两个注释的地图,但地图可以放大更多并显示更详细的地图视图.
正如您所看到的,地图可以放大很多.
任何想法如何实现这一目标?
Zan*_*ith 16
Swift 5.0 / 4.2 版本nyxee 的回答
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)
}
}
Run Code Online (Sandbox Code Playgroud)
nyx*_*xee 14
用这个:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在为MapView创建一个插座之后,例如map
,当你和注释一起添加到一个被调用的数组的地图中时annotations
,访问上面的方法,如下所示:
map.fitAll()
Run Code Online (Sandbox Code Playgroud)
要么
map.fitAll(in:annotations, true)
Run Code Online (Sandbox Code Playgroud)
分别.
在最后一个语句中使用true或false,具体取决于您之前是否已将注释添加到地图中...
在Swift中:
self.mapView.showAnnotations(self.mapView.annotations, animated: true)
Run Code Online (Sandbox Code Playgroud)
这就是我用来显示所有注释的内容。几乎与您所做的相同。地图将其缩放以适合地图的可见部分或可用部分,在我的应用程序上,缩放效果很好,并且确实添加了一些填充,因此所有图钉都不会碰到边缘。
因此,完成地图后,您可以快速使用setVisibleMapRect:edgePadding:animated:使用带有负填充的新visibleRect。
归档时间: |
|
查看次数: |
4589 次 |
最近记录: |