确定当前的增量/跨度

时间:2014-11-11 11:01:24

标签: xcode swift mapkit

当点击注释时,我将注释集中在地图上,没问题。问题是如果用户放大或缩小原始缩放级别,我想要缩放级别。

这就是我将地图居中的方式。但同样,我如何获得当前的跨度?

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {

    let span = MKCoordinateSpanMake(1.1, 1.1) //Get current span?
    let region = MKCoordinateRegion(center: view.annotation.coordinate, span: span)
    mapView.setRegion(region, animated: true)

}

2 个答案:

答案 0 :(得分:4)

您可以简单地询问当前MKMapView.region并在其上设置新的中心坐标:

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
    var region = mapView.region  // get the current region
    region.center = view.annotation.coordinate

    mapView.setRegion(region, animated: true)    
}

答案 1 :(得分:0)

要专门解决如何在保留跨度的同时围绕纬度/经度,允许用户向下钻取,但总是以点为中心,这里是我使用的一些代码。在地图上执行操作时,会重新定位引脚。

func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {


        //Get the location that I am going to center on from a variable with apreviously set location.  
        //This is what I want to center on
        let location = CLLocationCoordinate2D(

            //getPropertyLocation.latitude/longitude already set to CLLocationDegrees in a prior process.
            latitude: self.getPropertyLocation.latitude ,
            longitude: self.getPropertyLocation.longitude
        )

        // Get the span that the mapView is set to by the user. "propertyMapView" is the MKMapView in this example.
        let span = self.propertyMapView.region.span


        // Now setup the region based on the lat/lon of the property and retain the span that already exists.
        let region = MKCoordinateRegion(center: location, span: span)

        //Center the view with some animation.
        mapView.setRegion(region, animated: true)


}