{"id":2795,"date":"2021-05-15T22:09:45","date_gmt":"2021-05-15T14:09:45","guid":{"rendered":"http:\/\/123.57.164.21\/?p=2795"},"modified":"2021-05-15T22:11:27","modified_gmt":"2021-05-15T14:11:27","slug":"mapkit-%e5%9c%b0%e5%9b%be%e4%b8%8a%e7%94%bb%e5%a4%9a%e7%a7%8d%e9%a2%9c%e8%89%b2%e7%9a%84%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=2795","title":{"rendered":"iOS MapKit \u5730\u56fe\u4e0a\u753b\u591a\u79cd\u989c\u8272\u7684\u7ebf"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/05\/image-29-595x1024.png\" alt=\"\" class=\"wp-image-2796\" width=\"301\" height=\"520\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-29-174x300.png 174w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-29-230x396.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-29-350x603.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-29-480x827.png 480w\" sizes=\"(max-width: 301px) 100vw, 301px\" \/><\/figure><\/div>\n\n\n\n<p>\u76f4\u63a5\u9644\u4e0a\u4ee3\u7801\uff1a\u753b\u7ebf\u7684\u6837\u5f0f\u8bbe\u5b9a\u7528func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -&gt; MKOverlayRenderer\u8fd9\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    \/\/ \u6e32\u67d3overlay\u7684\u51fd\u6570\n    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {\n        \/\/\u753b\u51fa\u4e24\u6761\u7ebf\uff1a\n        let routeLineView = MKPolylineRenderer(overlay: overlay)\n          routeLineView.lineWidth = 4.0\n        \/\/ \u6839\u636eoverlay\u7684title\u533a\u5206\u6837\u5f0f\n        if overlay is MKPolyline{\n            if overlay.title == \"one\"{\n                routeLineView.strokeColor = UIColor.red\n            }else\n                if overlay.title == \"two\" {\n                routeLineView.strokeColor = UIColor.green\n            }\n        }\n            return routeLineView\n    }<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u51c6\u5907\u6570\u636e\uff0c\u6dfb\u52a0\u6570\u636e\uff1a\nimport UIKit\nimport MapKit\n\nclass MapViewController: UIViewController {\n\n    \n    @IBOutlet var mapView: MKMapView!\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \/\/ 1.\u5728ViewController\u5b9e\u73b0MKMapViewDelegate\u534f\u8bae\u7684\u59d4\u6258\n        mapView.delegate = self\n        \/\/2.\u8bbe\u7f6e\u4f4d\u7f6e\u7684\u7eac\u5ea6\u548c\u7ecf\u5ea6\n        let sourceLocation = CLLocationCoordinate2D(latitude: 40.759011, longitude: -73.984472)\n        let destinationLocation = CLLocationCoordinate2D(latitude: 40.748441, longitude: -73.985564)\n        \/\/ 3.\u521b\u5efa\u5305\u542b\u4f4d\u7f6e\u5750\u6807\u7684\u5730\u6807\u5bf9\u8c61\n        let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)\n        let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)\n        \/\/ 4.MKMapitems\u7528\u4e8e\u8def\u7ebf\u7684\u7ed8\u5236\u3002 \u6b64\u7c7b\u5c01\u88c5\u6709\u5173\u5730\u56fe\u4e0a\u7279\u5b9a\u70b9\u7684\u4fe1\u606f\n        let sourceMapItem = MKMapItem(placemark: sourcePlacemark)\n        let destinationMapItem = MKMapItem(placemark: destinationPlacemark)\n        \n        \/\/ 5. \u6dfb\u52a0\u6ce8\u91ca\uff0c\u663e\u793a\u5730\u6807\u7684\u540d\u79f0\n        let sourceAnnotation = MKPointAnnotation()\n        sourceAnnotation.title = \"Times Square\"\n        \/\/ \u5982\u679c\u5730\u6807\u5bf9\u8c61\u7684\u5750\u6807\u5b58\u5728\uff0c\u5c31\u7ed9MK\u70b9\u89e3\u91ca\u5bf9\u8c61\u6307\u70b9\u5750\u6807\n        if let location = sourcePlacemark.location {\n            sourceAnnotation.coordinate = location.coordinate\n        }\n\n        let destinationAnnotation = MKPointAnnotation()\n        destinationAnnotation.title = \"Empire State Building\"\n        \n        if let location = destinationPlacemark.location {\n            destinationAnnotation.coordinate = location.coordinate\n        }\n        \n        \/\/ 6.\u6807\u8bb0\u6ce8\u91ca\u663e\u793a\u5728\u5730\u56fe\u4e0a\n     self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )\n        \n        \/\/ 7. MKDirectionsRequest\u7c7b\u7528\u4e8e\u8ba1\u7b97\u8def\u7ebf\u3002\u6298\u7ebf\n        let directionRequest = MKDirections.Request()\n        directionRequest.source = sourceMapItem\n        directionRequest.destination = destinationMapItem\n        directionRequest.transportType = .automobile\n        \n        \/\/ Calculate the direction\n        let directions = MKDirections(request: directionRequest)\n        \n        \/\/ 8.\u5c06\u4f7f\u7528\u6298\u7ebf\u4f5c\u4e3a\u5730\u56fe\u9876\u90e8\u7684\u53e0\u52a0\u89c6\u56fe\u7ed8\u5236\u8def\u7ebf\u3002\u533a\u57df\u8bbe\u7f6e\u4e3a\u4e24\u4e2a\u4f4d\u7f6e\u90fd\u53ef\u89c1\n        directions.calculate {\n            (response, error) -> Void in\n\n            guard let response = response else {\n                if let error = error {\n                    print(\"Error: \\(error)\")\n                }\n\n                return\n            }\n\n            let route = response.routes[0]\n            route.polyline.title = \"one\"\n            self.mapView.addOverlay((route.polyline), level: MKOverlayLevel.aboveRoads)\n            let rect = route.polyline.boundingMapRect\n            self.mapView.setRegion(MKCoordinateRegion(rect), animated: true)\n\n        }\n        \/\/ \u6dfb\u52a0\u4e24\u4e2a\u70b9\u76f4\u63a5\u76f8\u8fde\u7684\u659c\u7ebf\n        let routeLine = MKPolyline(coordinates: [sourceLocation,destinationLocation], count: 2)\n        \/\/ \u203b\u8fd9\u91cc\u9762\u7ed9title\u8bbe\u5b9a\u503c\uff0c \u6765\u533a\u5206\u7ebf\u7684\u6837\u5f0f\u3002\n        routeLine.title = \"two\"\n        self.mapView.addOverlay(routeLine)\n        let rect1 = routeLine.boundingMapRect\n\n        self.mapView.setRegion(MKCoordinateRegion(rect1), animated: true)\n    }<\/pre>\n\n\n\n<p>\u81ea\u5b9a\u4e49\u70b9\u7684\u6837\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  \/\/ \u81ea\u5b9a\u4e49\u6807\u8bb0\u7684\u6837\u5f0f\uff1a\n    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {\n        if annotation is MKUserLocation {\n            return nil\n        }\n        \n        else {\n            let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: \"annotationView\") ?? MKAnnotationView()\n            annotationView.image = UIImage(systemName: \"hare.fill\")\n\/\/            annotationView.annotation.\n            return annotationView\n        }\n    }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u76f4\u63a5\u9644\u4e0a\u4ee3\u7801\uff1a\u753b\u7ebf\u7684\u6837\u5f0f\u8bbe\u5b9a\u7528func mapView(_ mapView: MKMapView, rende [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2795"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2795"}],"version-history":[{"count":6,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2795\/revisions"}],"predecessor-version":[{"id":2802,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2795\/revisions\/2802"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}