将多段线迅速绘制到一张地图上

问题描述 投票:-1回答:1

我想尝试使用折线将一些不同的步行区域放置到地图上。该应用程序非常适合一个应用程序,但是当我尝试添加步行区域2时,区域2之间根本没有显示多面体。我怎样才能显示区域1和区域2的折线?

我似乎无法弄清楚我的错在哪里,因为它非常适合步行区域一,但是当我添加额外的区域时却不能。

import UIKit
import MapKit

class customPin: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?

init(pinTitle:String, pinSubTitle:String, location:CLLocationCoordinate2D) {
    self.title = pinTitle
    self.subtitle = pinSubTitle
    self.coordinate = location
}
}

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //co-ordinates
    let zone1S = CLLocationCoordinate2D(latitude: 52.100525, longitude: -9.623071)
    let zone1E = CLLocationCoordinate2D(latitude: 52.07241, longitude: -9.575299)

    let zone2S = CLLocationCoordinate2D(latitude: 52.054161, longitude: -9.385031)
    let zone2E = CLLocationCoordinate2D(latitude: 52.081185, longitude: -9.247033)


    //pins
    let zone1PinS = customPin(pinTitle: "Zone 1 Start", pinSubTitle: "", location: zone1S)
    let zone1PinE = customPin(pinTitle: "Zone 1 End", pinSubTitle: "", location: zone1E)
    self.mapView.addAnnotation(zone1PinS)
    self.mapView.addAnnotation(zone1PinE)

    let zone2PinS = customPin(pinTitle: "Zone 2 Start", pinSubTitle: "", location: zone2S)
    let zone2PinE = customPin(pinTitle: "Zone 2 End", pinSubTitle: "", location: zone2E)
    self.mapView.addAnnotation(zone2PinS)
    self.mapView.addAnnotation(zone2PinE)


    let zone1PlacemarkS = MKPlacemark(coordinate: zone1S)
    let zone1PlacemarkE = MKPlacemark(coordinate: zone1E)

    let zone2PlacemarkS = MKPlacemark(coordinate: zone2S)
    let zone2PlacemarkE = MKPlacemark(coordinate: zone2E)


    let directionRequest = MKDirections.Request()
    directionRequest.source = MKMapItem(placemark: zone1PlacemarkS)
    directionRequest.destination = MKMapItem(placemark: zone1PlacemarkE)

    //type of commute
    directionRequest.transportType = .automobile



    let directions = MKDirections(request: directionRequest)
    directions.calculate { (response, error) in
        guard let directionResonse = response else {
            if let error = error {
                print("we have error getting directions==\(error.localizedDescription)")
            }
            return
        }

        let route = directionResonse.routes[0]
        self.mapView.addOverlay(route.polyline, level: .aboveRoads)


        let rect = route.polyline.boundingMapRect
        //zooming in on location
       // self.mapView.setRegion(MKCoordinateRegion(rect), animated: true)
    }

    //set delegate for mapview
    self.mapView.delegate = self
}


func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = UIColor.red
    renderer.lineWidth = 5.0
    return renderer
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
ios swift mapkit polyline cllocationcoordinate2d
1个回答
0
投票

当我尝试添加步行区2时

但是您添加它。你在说

let directionRequest = MKDirections.Request()
directionRequest.source = MKMapItem(placemark: zone1PlacemarkS)
directionRequest.destination = MKMapItem(placemark: zone1PlacemarkE)

您只有一个方向请求,它使用zone1PlacemarkSzone1PlacemarkE,因此您将获得这些方向。您永远不会向两个zone2地标发出指示请求;你只是把它们扔掉,不用。 (实际上,我希望编译器对此发出警告。)

© www.soinside.com 2019 - 2024. All rights reserved.