2017-04-24 55 views
0

我的地圖視圖顯示目標壁畫的用戶位置和註釋,但縮放關閉。我如何根據它們之間的距離和居中距離來使其適合當前位置和目的地註釋?縮放以適合用戶位置和註記地圖視圖swift

這是怎麼縮放(那麼遠)

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) 
} 

回答

0

您可以使用 'FUNC setRegion(_區域:MKCoordinateRegion,動畫:BOOL)'。 MKCoordinateRegion有一箇中心,也爲該地區定義了一個跨度。這裏的中心可以是距離用戶位置和目的地相等的點。您可以相應地決定跨度。

+0

讓latSpan = muralLocation.userLocation.coordinate.latitude - coordinates.latitude 讓大跨度= muralLocation.userLocation.coordinate.longitude - coordinates.longitude 變種地圖= MKCoordinateRegion() 設跨度= MKCoordinateSpanMake(latSpan, longSpan) map.center = muralLocation.userLocation.coordinate map.span = span –

+0

我這樣做了,但是地圖縮放沒有預算......有幫助嗎? –

相關問題