在setRegion MapKit中封装路由swift

Encapsulate route in setRegion MapKit swift

我有一个带有标记(注释)和用户地理位置的 mapView。当用户点击其中一个标记时,我会在用户位置和针尖位置之间绘制一条路线。

然而,地图缩放跨度在用户位置上,我唯一能做的就是将地图区域设置为远离用户。这不是正确的方法,所以我愿意找到一种方法来放大或缩小以最大化路线视图。

有办法吗?

这是我的代码:

//来自故事板的地图套接字 @IBOutlet 弱变量 mapView: MKMapView!

//Geolocation variables
var locationManager = CLLocationManager()
var destination: MKMapItem?

在 viewDidLoad 中

//Setting up delegate and CoreLocaton setup
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

mapView.delegate = self
        mapView.showsUserLocation = true

let initialLocation = CLLocation(latitude: 45.5215374, longitude: -73.5811606)

        let regionRadius: CLLocationDistance = 2000
        func centerMapOnLocation(location: CLLocation) {
            let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
                regionRadius * 2.0, regionRadius * 2.0)
            mapView.setRegion(coordinateRegion, animated: true)
        }

        //Ask the map to center itself
        centerMapOnLocation(initialLocation)

代表们

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

            //remove existing routes
            mapView.removeOverlays(mapView.overlays)

            //call for new route
            var destination = MKPlacemark(coordinate: view.annotation.coordinate, addressDictionary: nil)
            getDirection(destination)
    }


    //INTERACTION WITH ANNOTATIONS (MARKER)
    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!,
        calloutAccessoryControlTapped control: UIControl!) {

            if control == view.rightCalloutAccessoryView {
                println("Disclosure Pressed! \(view.annotation.subtitle)")

                if let cpa = view.annotation as? MapArtwork {
                    println("cpa.imageName")
                }
            }

    }

    //UPDATE LOCATION
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

        if (!locations.isEmpty)
        {

            let myLocation  = locations[0] as! CLLocation

            mapView.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude),
                MKCoordinateSpanMake(0.06, 0.06)), animated: true)
        }

        locationManager.stopUpdatingLocation()

    }

    //GET DIRECTION BETWEEN GEOPOSITION AND TOUCHED MARKER
    func getDirection(location:MKPlacemark){
        println("getting direction")
        destination = MKMapItem(placemark:location)

        let request = MKDirectionsRequest()
        request.setSource(MKMapItem.mapItemForCurrentLocation())
        request.setDestination(destination!)
        request.requestsAlternateRoutes = false

        let directions = MKDirections(request: request)

        directions.calculateDirectionsWithCompletionHandler({(response:
            MKDirectionsResponse!, error: NSError!) in

            if error != nil {
                // Handle error
                println(error)
            } else {
                self.showRoute(response)
            }

        })
    }

这是我在显示路线时设置地图区域的部分。

    //DISPLAY ROUTE OVERLAY
    func showRoute(response: MKDirectionsResponse) {
        println("showing route")
        for route in response.routes as! [MKRoute] {
            mapView.addOverlay(route.polyline,
                level: MKOverlayLevel.AboveRoads)

            for step in route.steps {
                println(step.instructions)
            }
        }
        let userLocation = mapView.userLocation
        let region = MKCoordinateRegionMakeWithDistance(
            userLocation.location.coordinate, 6000, 6000)

        mapView.setRegion(region, animated: true)
    }

    func mapView(mapView: MKMapView!, rendererForOverlay
        overlay: MKOverlay!) -> MKOverlayRenderer! {
            let renderer = MKPolylineRenderer(overlay: overlay)

            renderer.strokeColor = UIColor(red: 8.0/255.0, green: 47.0/255.0, blue: 62.0/255.0, alpha: 1.0)
            renderer.lineWidth = 5.0
            return renderer
    }

好的,所以我找到了一些可以解决问题的方法,但这可能不是最好的主意。

在下面的函数中

//DISPLAY ROUTE OVERLAY
    func showRoute(response: MKDirectionsResponse) {
        println("showing route")
        for route in response.routes as! [MKRoute] {
            mapView.addOverlay(route.polyline,
                level: MKOverlayLevel.AboveRoads)

            for step in route.steps {
                println(step.instructions)
            }
        }

        let userLocation = mapView.userLocation
        let region = MKCoordinateRegionMakeWithDistance(
            userLocation.location.coordinate, 6000, 6000)

        mapView.setRegion(region, animated: true)
    }

删除最后一部分:

let userLocation = mapView.userLocation
            let region = MKCoordinateRegionMakeWithDistance(
                userLocation.location.coordinate, 6000, 6000)

            mapView.setRegion(region, animated: true)

并在以下委托中:

func mapView(mapView: MKMapView!, rendererForOverlay
        overlay: MKOverlay!) -> MKOverlayRenderer! {
            let renderer = MKPolylineRenderer(overlay: overlay)

            renderer.strokeColor = UIColor(red: 8.0/255.0, green: 47.0/255.0, blue: 62.0/255.0, alpha: 1.0)
            renderer.lineWidth = 5.0
            return renderer
    }

添加这个:

let mapRect = MKPolygon(points: renderer.polyline.points(), count: renderer.polyline.pointCount)
            mapView.setVisibleMapRect(mapRect.boundingMapRect, edgePadding: UIEdgeInsets(top: 50.0,left: 50.0,bottom: 50.0,right: 50.0), animated: true)