{"id":2785,"date":"2021-05-11T21:16:46","date_gmt":"2021-05-11T13:16:46","guid":{"rendered":"http:\/\/123.57.164.21\/?p=2785"},"modified":"2021-05-11T21:16:46","modified_gmt":"2021-05-11T13:16:46","slug":"ios-mapkit-%e5%9c%b0%e5%9b%be%e6%8b%96%e6%8b%bd-%e7%bc%a9%e6%94%be%e7%9a%84%e4%ba%8b%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=2785","title":{"rendered":"IOS MapKit \u5730\u56fe\u62d6\u62fd \u7f29\u653e\u7684\u4e8b\u4ef6"},"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-28-1024x612.png\" alt=\"\" class=\"wp-image-2788\" width=\"481\" height=\"287\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-28-1024x612.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-28-300x179.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-28-768x459.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-28-830x496.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-28-230x137.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-28-350x209.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-28-480x287.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-28.png 1252w\" sizes=\"(max-width: 481px) 100vw, 481px\" \/><figcaption>\u62d6\u62fd\u5730\u56fe<\/figcaption><\/figure><\/div>\n\n\n\n<p>\u5f53\u6211\u4eec\u7528MapKit\u5f00\u53d1\u65f6\uff0c\u6709\u65f6\u5019\u5e0c\u671b\u5728\u5730\u56fe\u62d6\u62fd\u65f6\u89e6\u53d1\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\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=\"\">struct MapView: UIViewRepresentable {\n\n    func makeUIView(context: Context) -> MKMapView {\n        let mapView = MKMapView()\n        mapView.delegate = context.coordinator\n        mapView.showsBuildings = true\n        mapView.showsScale = true\n        mapView.isPitchEnabled = true\n        mapView.showsUserLocation = true\n\n        let region = MKCoordinateRegion(center: CLLocationCoordinate2DMake(35.292899, 140.085370), span: MKCoordinateSpan(latitudeDelta: 0.75, longitudeDelta: 0.75))\n        mapView.setRegion(region, animated: true)\n\n        return mapView\n    }\n\n    func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext&lt;MapView>) {\n\n    }\n\n    func makeCoordinator() -> Coordinator {\n        Coordinator(self)\n    }\n\n    class Coordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDelegate {\n\n        var parent: MapView\n        var mkMapView: MKMapView?\n\n        init(_ parent: MapView) {\n            self.parent = parent\n\n        }\n\n        func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {\n            self.mkMapView = mapView\n        }\n\n        func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {\n        }\n\n        \/\/ \u5730\u56fe\u56fe\u9489\u9078\u629e\u5834\u5408\u52d5\u4f5c\n        func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {\n\n        }\n\n        \/\/ \u203b\u62d6\u62fd\u5730\u56fe\u65f6\u89e6\u53d1\n        func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {\n            \/\/ \u8fd9\u91cc\u505a\u4e9b\u81ea\u5b9a\u4e49\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u91cd\u753b\u67d0\u4e9b\u56fe\u9489\u4e4b\u7c7b\u7684\u3002\n            drawCenterPoint(mapView)\n        }\n        \n        \/\/ \u203b\u62d6\u62fd\u5730\u56fe\u65f6\u89e6\u53d1\n        func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {\n            drawCenterPoint(mapView)\n        }\n\n        \/\/ \u81ea\u5b9a\u4e49\u56fe\u9489\u505a\u6210\n        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {\n            guard annotation is MKPointAnnotation else { return nil }\n\n            let identifier = \"Annotation\" + String(arc4random())\n            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)\n            if annotationView == nil {\n                annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)\n            } else {\n                annotationView!.annotation = annotation\n            }\n\n            let image = UIImage(named: \"\\(annotation.annotationType)\")!\n            annotationView!.image = image.scaleImage(scaleSize: annotation.iconScaleSize)\n            if annotation.annotationType == \"Center Point\" {\n                let offset = CGPoint(x: 0, y: -(image.size.height \/ 2))\n                annotationView!.centerOffset = offset\n            }\n\n            let nameLbl: UILabel! = UILabel(frame: CGRect(x: -24, y: 40, width: 100, height: 30))\n            nameLbl.text = \"\u81ea\u7531\u6587\u672c\"\n            nameLbl.textColor = UIColor.black\n            nameLbl.font = UIFont(name: \"Atari Classic Extrasmooth\", size: 10)\n            nameLbl.textAlignment = NSTextAlignment.center\n            annotationView!.addSubview(nameLbl)\n\n\n            annotationView!.displayPriority = .required\n            return annotationView\n        }\n\n        func drawCenterPoint(_ uiView: MKMapView) {\n\n            for annotation_loop in uiView.annotations {\n                if annotation_loop is MKUserLocation {\n                    continue\n                }\n\n                if annotation_loop.annotationType == \"Center Point\" {\n                    uiView.removeAnnotation(annotation_loop)\n                    break;\n                }\n\n            }\n            let annotation = MKPointAnnotation()\n            annotation.annotationID = String(\"001\")\n            annotation.annotationType = \"Center Point\"\n            annotation.iconScaleSize = 0.25\n            annotation.title = \"123\"\n            annotation.latitude = String(uiView.region.center.latitude)\n            annotation.longitude = String(uiView.region.center.longitude)\n            annotation.coordinate = CLLocationCoordinate2DMake(uiView.region.center.latitude, uiView.region.center.longitude)\n            uiView.addAnnotation(annotation)\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u6211\u4eec\u7528MapKit\u5f00\u53d1\u65f6\uff0c\u6709\u65f6\u5019\u5e0c\u671b\u5728\u5730\u56fe\u62d6\u62fd\u65f6\u89e6\u53d1\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u3002<\/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\/2785"}],"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=2785"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2785\/revisions"}],"predecessor-version":[{"id":2789,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2785\/revisions\/2789"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}