{"id":7366,"date":"2025-06-01T12:35:34","date_gmt":"2025-06-05T05:04:34","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7366"},"modified":"2025-06-05T12:35:34","modified_gmt":"2025-06-05T05:04:34","slug":"https-badgameshow-com-steven-354","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-354\/","title":{"rendered":"2025 \u6700\u65b0\u7248 Swift \u5730\u5716\u6a19\u8a18\u6559\u5b78 \ud83d\uddfa\ufe0f\ud83d\udd0d \u2014 \u5b8c\u6574\u6307\u5357\u8207\u5be6\u4f5c\u7bc4\u4f8b"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u5730\u5716\u6a19\u8a18, MKAnnotation, iOS \u5730\u5716\u61c9\u7528, MapKit \u6559\u5b78\"><\/p>\n<h1>2025 \u6700\u65b0\u7248 Swift \u5730\u5716\u6a19\u8a18\u6559\u5b78 \ud83d\uddfa\ufe0f\ud83d\udd0d<\/h1>\n<p>Swift \u5730\u5716\u6a19\u8a18\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728 iOS \u548c macOS \u4e0a\u66f4\u8f15\u9b06\u5730\u64cd\u4f5c\u5730\u5716\u6a19\u8a18\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u8a73\u7d30\u4ecb\u7d39 Swift \u5730\u5716\u6a19\u8a18\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u69cb\u5efa\u4e00\u500b\u5b8c\u6574\u7684\u5730\u5716\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<h2>Swift \u5730\u5716\u6a19\u8a18\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u5730\u5716\u6a19\u8a18\u662f\u4e00\u500b\u53ef\u4ee5\u5728\u5730\u5716\u4e0a\u6a19\u8a18\u4f4d\u7f6e\u7684\u7269\u4ef6\uff0c\u5b83\u53ef\u4ee5\u662f\u4e00\u500b\u5716\u6a19\u3001\u4e00\u500b\u6587\u5b57\u6a19\u7c64\u6216\u8005\u4e00\u500b\u5f71\u50cf\u3002\u5730\u5716\u6a19\u8a18\u53ef\u4ee5\u7528\u4f86\u6a19\u8a18\u4e00\u500b\u5730\u9ede\u3001\u4e00\u689d\u8def\u7dda\u6216\u4e00\u500b\u5340\u57df\u3002<\/p>\n<h2>\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 MKAnnotation<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u5730\u5716\u6a19\u8a18\u662f\u4e00\u500b <strong>MKAnnotation<\/strong> \u7684\u5b50\u985e\u3002\u5730\u5716\u6a19\u8a18\u53ef\u4ee5\u6709\u4e00\u500b <strong>title<\/strong> \u548c\u4e00\u500b <strong>subtitle<\/strong>\uff0c\u7528\u65bc\u63d0\u4f9b\u66f4\u591a\u8cc7\u8a0a\uff0c\u5982\u5730\u5740\u3001\u96fb\u8a71\u865f\u78bc\u6216\u5176\u4ed6\u8cc7\u8a0a\u3002<\/p>\n<h3>\u5275\u5efa MKAnnotation \u5b50\u985e<\/h3>\n<p>\u8981\u5728\u5730\u5716\u4e0a\u6dfb\u52a0\u6a19\u8a18\uff0c\u9996\u5148\u9700\u8981\u5275\u5efa\u4e00\u500b <strong>MKAnnotation<\/strong> \u7684\u5b50\u985e\uff0c\u4e26\u5be6\u73fe\u5176 <strong>coordinate<\/strong> \u5c6c\u6027\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">class MyAnnotation: NSObject, MKAnnotation {\n    var coordinate: CLLocationCoordinate2D\n    var title: String?\n    var subtitle: String?\n    \n    init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String?) {\n        self.coordinate = coordinate\n        self.title = title\n        self.subtitle = subtitle\n    }\n}\n<\/code><\/pre>\n<h3>\u5c07\u6a19\u8a18\u6dfb\u52a0\u5230\u5730\u5716<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>addAnnotation<\/strong> \u65b9\u6cd5\u5c07\u6a19\u8a18\u6dfb\u52a0\u5230\u5730\u5716\u4e0a\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let annotation = MyAnnotation(coordinate: CLLocationCoordinate2D(latitude: 37.3317, longitude: -122.0301), title: \"Apple Park\", subtitle: \"Apple's headquarters\")\nmapView.addAnnotation(annotation)\n<\/code><\/pre>\n<h3>\u81ea\u5b9a\u7fa9\u5730\u5716\u6a19\u8a18\u7684\u5916\u89c0<\/h3>\n<p>\u6700\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>MKAnnotationView<\/strong> \u4f86\u81ea\u5b9a\u7fa9\u5730\u5716\u6a19\u8a18\u7684\u5916\u89c0\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -&gt; MKAnnotationView? {\n    let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: \"myAnnotation\")\n    annotationView.image = UIImage(named: \"myImage\")\n    return annotationView\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u6280\u5de7<\/h2>\n<ul>\n<li><strong>\u7121\u6cd5\u986f\u793a\u6a19\u8a18\uff1a<\/strong>\u78ba\u4fdd\u5df2\u7d93\u5c07 <strong>mapView.delegate<\/strong> \u8a2d\u7f6e\u70ba\u7576\u524d\u8996\u5716\u63a7\u5236\u5668\u3002<\/li>\n<li><strong>\u6a19\u8a18\u4f4d\u7f6e\u4e0d\u6b63\u78ba\uff1a<\/strong>\u78ba\u8a8d <strong>coordinate<\/strong> \u7684\u7d93\u7def\u5ea6\u503c\u662f\u5426\u6b63\u78ba\u3002<\/li>\n<li><strong>\u81ea\u5b9a\u7fa9\u5716\u6a19\u4e0d\u986f\u793a\uff1a<\/strong>\u6aa2\u67e5\u5716\u50cf\u8cc7\u6e90\u662f\u5426\u6b63\u78ba\u547d\u540d\u4e26\u6dfb\u52a0\u5230\u5c08\u6848\u4e2d\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u5730\u5716\u6a19\u8a18\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u4f7f\u7528\u5730\u5716\u8986\u84cb\u5c64\uff08\u5982\u8def\u5f91\u3001\u5340\u57df\u7b49\uff09\u4f86\u589e\u5f37\u5730\u5716\u61c9\u7528\u7684\u529f\u80fd\uff0c\u9019\u4e9b\u90fd\u53ef\u4ee5\u901a\u904e MapKit \u63d0\u4f9b\u7684\u5176\u4ed6 API \u5be6\u73fe\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5730\u5716\u6a19\u8a18-\ud83d\uddfa\ufe0f\ud83d\udd0d_1675474866.535411.webp\" alt=\"Swift \u5730\u5716\u6a19\u8a18 \ud83d\uddfa\ufe0f\ud83d\udd0d\" title=\"Swift \u5730\u5716\u6a19\u8a18\u793a\u7bc4\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 Q&#038;A<\/h2>\n<h3>Q1: \u5982\u4f55\u5728\u5730\u5716\u4e0a\u6dfb\u52a0\u591a\u500b\u6a19\u8a18\uff1f<\/h3>\n<p>A1: \u53ef\u4ee5\u5275\u5efa\u591a\u500b <strong>MyAnnotation<\/strong> \u5be6\u4f8b\uff0c\u4e26\u4f7f\u7528 <strong>mapView.addAnnotation()<\/strong> \u65b9\u6cd5\u4f9d\u6b21\u6dfb\u52a0\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u8655\u7406\u6a19\u8a18\u7684\u9ede\u64ca\u4e8b\u4ef6\uff1f<\/h3>\n<p>A2: \u53ef\u4ee5\u5be6\u4f5c <strong>mapView(_:didSelect:) <\/strong> \u65b9\u6cd5\uff0c\u4f86\u8655\u7406\u7528\u6236\u9ede\u64ca\u6a19\u8a18\u5f8c\u7684\u884c\u70ba\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u66f4\u6539\u6a19\u8a18\u7684\u986f\u793a\u6a23\u5f0f\uff1f<\/h3>\n<p>A3: \u53ef\u4ee5\u900f\u904e <strong>MKAnnotationView<\/strong> \u7684\u5c6c\u6027\u4f86\u8abf\u6574\u6a19\u8a18\u7684\u986f\u793a\u6a23\u5f0f\uff0c\u4f8b\u5982\u5716\u50cf\u3001\u984f\u8272\u7b49\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u6a19\u8a18\u5730\u5716\uff0c\u4e26\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u7684methods()\u51fd\u6578\u4f86\u64cd\u4f5c\u5730\u5716\u6a19\u8a18\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Swift\u5730\u5716\u6a19\u8a18\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":7365,"comment_status":"open","ping_status":"closed","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-7366","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5730\u5716\u6a19\u8a18-\ud83d\uddfa\ufe0f\ud83d\udd0d_1675474866.535411.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1UO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7366","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=7366"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7366\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7365"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}