缩放以适应用户位置和注释地图视图swift

时间:2017-04-24 00:35:18

标签: swift mkmapview zoom rect

我的地图视图显示目标壁画的用户位置和注释,但缩放是关闭的。如何根据它们之间的距离和居中距离来缩放以适应当前位置和目标注释?

这是它如何缩放(如此遥远)

func mapPoints(){
    let annotations = MKPointAnnotation()

    annotations.title = muralModel.muralName!

    muralModel.getMainArtistModel(completion: { (anArtistModel) in
        annotations.subtitle  = " \(anArtistModel!.firstName!) \(anArtistModel!.lastName!)"
    })

    annotations.coordinate = coordinates

    muralLocation.addAnnotation(annotations)

    self.muralLocation.showsUserLocation = true

    var zoomRect: MKMapRect = MKMapRectNull
    let mapEdgePadding = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)

    //convert CLLocationCoordinate2D to MKMapPointForCoordinate so you don't end up in Bering Sea
    let userPoint = MKMapPointForCoordinate(muralLocation.userLocation.coordinate)
    let destinationPoint = MKMapPointForCoordinate(coordinates)

    let userPointRect = MKMapRectMake(userPoint.x, userPoint.y, 0.1, 0.1)
    let destinationPointRect = MKMapRectMake(destinationPoint.x, destinationPoint.y, 0.1, 0.1)

    zoomRect = userPointRect
    zoomRect = MKMapRectUnion(zoomRect, destinationPointRect)

    muralLocation.setVisibleMapRect(zoomRect, edgePadding: mapEdgePadding, animated: true)
}

1 个答案:

答案 0 :(得分:0)

您可以使用' func setRegion(_ region:MKCoordinateRegion,animated:Bool)'。 MKCoordinateRegion有一个中心,也定义了该地区的跨度。这里的中心可以是距离userLocation和目的地相等距离的点。您可以相应地决定跨度。