{"id":4033,"date":"2025-06-01T11:17:07","date_gmt":"2025-06-05T04:00:06","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4033"},"modified":"2025-06-05T11:17:07","modified_gmt":"2025-06-05T04:00:06","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e5%9c%b0%e5%9c%96%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e5%9c%b0%e5%9c%96%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u5168\u9762\u638c\u63e1 Swift \u4e2d\u7684\u5730\u5716\u529f\u80fd\uff1a2025 \u6700\u65b0 MKMapView \u6559\u5b78"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, MKMapView, \u5730\u56fe, \u6807\u6ce8, \u81ea\u5b9a\u4e49\u6807\u6ce8\u89c6\u56fe, Swift 2025, \u5730\u56fe\u529f\u80fd\"><\/p>\n<h1>\u5168\u9762\u638c\u63e1 Swift \u4e2d\u7684\u5730\u5716\u529f\u80fd<\/h1>\n<p>\u5728 Swift \u4e2d\u4f7f\u7528\u5730\u5716\u529f\u80fd\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u4e92\u52d5\u6027\uff0c\u8b93\u7528\u6236\u66f4\u597d\u5730\u9ad4\u9a57\u61c9\u7528\u7684\u529f\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4e2d\u7684 MKMapView \u985e\u4f86\u986f\u793a\u5730\u5716\uff0c\u5305\u62ec\u5982\u4f55\u8a2d\u7f6e\u5730\u5716\u7684\u986f\u793a\u7bc4\u570d\u548c\u4e2d\u5fc3\u9ede\u3001\u6dfb\u52a0\u6a19\u8a3b\u3001\u4ee5\u53ca\u81ea\u5b9a\u7fa9\u6a19\u8a3b\u8996\u5716\u7b49\uff0c\u4e26\u63d0\u4f9b 2025 \u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u6dfb\u52a0 MKMapView \u985e\u986f\u793a\u5730\u5716<\/h2>\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u6dfb\u52a0\u4e00\u500b MKMapView \u7684\u5be6\u4f8b\u3002\u9019\u53ef\u4ee5\u901a\u904e\u5728 storyboard \u4e2d\u62d6\u653e MKMapView\uff0c\u6216\u5728\u4ee3\u78bc\u4e2d\u5275\u5efa\u4e00\u500b\u5be6\u4f8b\u4f86\u5b8c\u6210\uff1a<\/p>\n<pre class=\"brush: swift\">\nlet mapView = MKMapView()\n\/\/ \u8a2d\u7f6e\u5730\u5716\u7684\u6846\u67b6\nmapView.frame = view.bounds\nview.addSubview(mapView)\n<\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u8a2d\u7f6e\u5730\u5716\u7684\u986f\u793a\u7bc4\u570d\u548c\u4e2d\u5fc3\u9ede\uff0c\u9019\u6a23\u7528\u6236\u624d\u80fd\u770b\u5230\u9810\u671f\u7684\u5167\u5bb9\u3002\u6211\u5011\u4f7f\u7528 MKCoordinateRegion \u985e\u4f86\u8a2d\u7f6e\u986f\u793a\u7bc4\u570d\uff0c\u4f7f\u7528 MKCoordinateSpan \u4f86\u6307\u5b9a\u7bc4\u570d\u7684\u5927\u5c0f\uff0c\u4e26\u7528 CLLocationCoordinate2D \u4f86\u8a2d\u7f6e\u4e2d\u5fc3\u9ede\uff1a<\/p>\n<pre class=\"brush: swift\">\nlet region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 37.783333, longitude: -122.416667), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))\nmapView.setRegion(region, animated: true)\n<\/pre>\n<h2>\u6dfb\u52a0\u6a19\u8a3b\u5230\u5730\u5716<\/h2>\n<p>\u8981\u5728\u5730\u5716\u4e0a\u6dfb\u52a0\u6a19\u8a3b\uff0c\u53ef\u4ee5\u4f7f\u7528 MKPointAnnotation \u985e\u3002\u4ee5\u4e0b\u662f\u6dfb\u52a0\u6a19\u8a3b\u7684\u4ee3\u78bc\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: swift\">\nlet annotation = MKPointAnnotation()\nannotation.coordinate = CLLocationCoordinate2D(latitude: 37.783333, longitude: -122.416667)\nannotation.title = \"San Francisco\"\nmapView.addAnnotation(annotation)\n<\/pre>\n<h2>\u81ea\u5b9a\u7fa9\u6a19\u8a3b\u8996\u5716<\/h2>\n<p>\u82e5\u8981\u81ea\u5b9a\u7fa9\u6a19\u8a3b\u8996\u5716\uff0c\u4f7f\u7528 MKAnnotationView \u985e\u4f86\u8a2d\u7f6e\u6a19\u8a3b\u7684\u5716\u7247\u548c\u6a23\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u81ea\u5b9a\u7fa9\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: swift\">\nclass CustomAnnotationView: MKAnnotationView {\n    override var annotation: MKAnnotation? {\n        willSet {\n            canShowCallout = true\n            image = UIImage(named: \"pin\")\n            backgroundColor = UIColor.blue\n        }\n    }\n}\n<\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5be6\u4f5c MKMapViewDelegate \u4ee5\u8655\u7406\u6a19\u8a3b\u8996\u5716\u7684\u9ede\u64ca\u4e8b\u4ef6\uff1a<\/p>\n<pre class=\"brush: swift\">\nfunc mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {\n    if annotation is MKPointAnnotation {\n        let annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: \"myAnnotation\")\n        return annotationView\n    }\n    return nil\n}\n\nfunc mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {\n    \/\/ \u8655\u7406\u6a19\u8a3b\u8996\u5716\u7684\u9078\u64c7\u4e8b\u4ef6\n}\n<\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5728\u4f7f\u7528 MKMapView \u7684\u904e\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u554f\u984c\uff1a<\/p>\n<p>1. **\u5730\u5716\u672a\u986f\u793a**\uff1a\u78ba\u4fdd\u5df2\u6b63\u78ba\u6dfb\u52a0 MKMapView \u7684\u5be6\u4f8b\u4e26\u8a2d\u7f6e\u6846\u67b6\u3002<br \/>\n2. **\u6a19\u8a3b\u672a\u986f\u793a**\uff1a\u6aa2\u67e5\u6a19\u8a3b\u7684\u5ea7\u6a19\u662f\u5426\u6b63\u78ba\uff0c\u4e26\u78ba\u4fdd\u5df2\u6dfb\u52a0\u6a19\u8a3b\u5230\u5730\u5716\u4e0a\u3002<br \/>\n3. **\u81ea\u5b9a\u7fa9\u6a19\u8a3b\u8996\u5716\u7121\u6cd5\u986f\u793a**\uff1a\u78ba\u4fdd\u6b63\u78ba\u5be6\u4f5c MKMapViewDelegate \u4e26\u8fd4\u56de\u81ea\u5b9a\u7fa9\u6a19\u8a3b\u8996\u5716\u3002<\/p>\n<p>\u5ef6\u4f38\u61c9\u7528\u65b9\u9762\uff0c\u4f60\u53ef\u4ee5\u8003\u616e\u5c07\u5730\u5716\u548c\u4f7f\u7528\u8005\u7684\u4f4d\u7f6e\u529f\u80fd\u7d50\u5408\uff0c\u5be6\u73fe\u5982 GPS \u5c0e\u822a\u3001\u9644\u8fd1\u5546\u5bb6\u67e5\u8a62\u7b49\u529f\u80fd\u3002<\/p>\n<h2>\u7d50\u8a9e<\/h2>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528\u5730\u5716\u7684\u57fa\u672c\u6b65\u9a5f\u3002\u901a\u904e\u9019\u4e9b\u793a\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\uff0c\u4f60\u53ef\u4ee5\u5275\u5efa\u4e00\u500b\u5f15\u4eba\u5165\u52dd\u7684\u5730\u5716\u61c9\u7528\u7a0b\u5e8f\uff0c\u70ba\u7528\u6236\u63d0\u4f9b\u66f4\u8c50\u5bcc\u7684\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5728\u5730\u5716\u4e0a\u986f\u793a\u7528\u6236\u7576\u524d\u4f4d\u7f6e\uff1f<\/h3>\n<p>\u4f7f\u7528 MKMapView \u7684 showsUserLocation \u5c6c\u6027\u4f86\u986f\u793a\u7528\u6236\u7576\u524d\u4f4d\u7f6e\uff0c\u4e26\u78ba\u4fdd\u5728 Info.plist \u4e2d\u6dfb\u52a0 NSLocationWhenInUseUsageDescription\u3002<\/p>\n<h3>2. \u5982\u4f55\u5728\u5730\u5716\u4e0a\u6dfb\u52a0\u591a\u500b\u6a19\u8a3b\uff1f<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4e00\u500b\u6578\u7d44\u4f86\u5b58\u5132\u591a\u500b MKPointAnnotation \u5be6\u4f8b\u4e26\u8fed\u4ee3\u6dfb\u52a0\u5230 mapView \u4e2d\u3002<\/p>\n<h3>3. \u5982\u4f55\u81ea\u5b9a\u7fa9\u6a19\u8a3b\u7684\u986f\u793a\u6a23\u5f0f\uff1f<\/h3>\n<p>\u5275\u5efa\u81ea\u5b9a\u7fa9\u7684 MKAnnotationView \u985e\u5225\uff0c\u4e26\u5728 viewFor annotation \u65b9\u6cd5\u4e2d\u8fd4\u56de\u6b64\u81ea\u5b9a\u7fa9\u8996\u5716\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u5730\u5716\u529f\u80fd\uff0c\u5305\u62ec\u5982\u4f55\u4f7f\u7528MKMapView\u985e\u4f86\u986f\u793a\u5730\u5716\u3001\u5982\u4f55\u8a2d\u7f6e\u5730\u5716\u7684\u986f\u793a\u7bc4\u570d\u548c\u4e2d\u5fc3\u9ede\u3001\u5982\u4f55\u6dfb\u52a0\u6a19\u8a18\u3001\u5982\u4f55\u81ea\u5b9a\u7fa9\u6a19\u8a18\u8996\u5716\u7b49\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-4033","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-133","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=4033"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4033\/revisions"}],"predecessor-version":[{"id":4034,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4033\/revisions\/4034"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}