{"id":4903,"date":"2022-01-16T09:18:55","date_gmt":"2022-01-16T01:18:55","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4903"},"modified":"2022-01-16T09:18:55","modified_gmt":"2022-01-16T01:18:55","slug":"swift2-0-%e5%9c%a8%e8%a7%86%e5%9b%be%e4%b8%ad%e6%98%be%e7%a4%ba%e5%9c%b0%e5%9b%be","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4903","title":{"rendered":"Swift2.0 \u5728\u89c6\u56fe\u4e2d\u663e\u793a\u5730\u56fe"},"content":{"rendered":"\n<p>Swift2.0 \u4e2d\uff0c\u82f9\u679c\u6dfb\u52a0\u4e86 Map\uff0c\u8ba9\u5f00\u53d1\u8005\u53ef\u4ee5\u975e\u5e38\u5bb9\u6613\u7684\u5728 View \u4e2d\u6dfb\u52a0\u9700\u8981\u7684\u5730\u56fe\u5143\u7d20\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=\"\">import SwiftUI\nimport MapKit\n\nstruct MapView: View{\n    \/\/\u8bbe\u7f6e\u521d\u59cb\u663e\u793a\u533a\u57df\n    @State private var region:MKCoordinateRegion = MKCoordinateRegion(\n        center: CLLocationCoordinate2D(latitude: 38.92083, longitude: 121.63917),\n        span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)\n    )\n    \n    \/\/\u8bbe\u7f6e\u662f\u5426\u6301\u7eed\u8ddf\u8e2a\u7528\u6237\u5f53\u524d\u4f4d\u7f6e\n    @State private var trackmode = MapUserTrackingMode.follow\n    \n    \/\/\u8bbe\u7f6e\u6807\u8bb0\u70b9\u4fe1\u606f\n    let dots:[MapDot] = [\n        MapDot(title:\"point1\",\n               coordinate:CLLocationCoordinate2D(latitude: 38.92083, longitude: 121.63917),\n               color:.red),\n        MapDot(title:\"point2\",\n               coordinate:CLLocationCoordinate2D(latitude: 38.92183, longitude: 121.62717),\n               color:.blue)\n    ]\n    \n    @StateObject var store = Store()\n    \n    var body: some View {\n        ZStack(alignment:.bottom){\n            Map(coordinateRegion: $region,\n                interactionModes: .all, \/\/.pan .zoom .all\n                showsUserLocation: true, \/\/\u662f\u5426\u663e\u793a\u7528\u6237\u5f53\u524d\u4f4d\u7f6e\n                userTrackingMode:$trackmode, \/\/\u662f\u5426\u66f4\u65b0\u7528\u6237\u4f4d\u7f6e\n                annotationItems:dots \/\/\u6807\u8bb0\u70b9\u6570\u636e\n            ){item in\n                \/\/\u6807\u8bb0\u70b9\u663e\u793a\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5185\u7f6e\u7684 MapPin, \u4e0d\u8fc7 MapPin \u65e0\u6cd5\u54cd\u5e94\u7528\u6237\u8f93\u5165\n                MapAnnotation(coordinate: item.coordinate  ){\n                    \/\/\u4e0d\u77e5\u9053\u662f\u5426\u662f bug, \u76ee\u524d iOS \u4e0b\u65e0\u6cd5\u663e\u793a Text,maxOS \u53ef\u4ee5\u663e\u793a\n                    Label(item.title, systemImage: \"star.fill\")\n                        .font(.body)\n                        .foregroundColor(item.color)\n                        .onTapGesture {\n                            print(item.title)\n                        }\n                }\n            }\n        }\n        .edgesIgnoringSafeArea(.all)\n    }\n}\n\n\/\/\u6807\u8bb0\u70b9\u6570\u636e\uff0c\u8981\u6c42\u7b26\u5408 Identifiable\nstruct MapDot:Identifiable{\n    let id = UUID()\n    let title:String\n    let coordinate:CLLocationCoordinate2D\n    let color:Color\n}\n\nclass Store:ObservableObject {\n    let manager = CLLocationManager()\n    init() {\n        \/\/\u8bf7\u6c42\u4f4d\u7f6e\u8bbf\u95ee\u6743\u9650\u3002\u9700\u8981\u5728 plist \u4e2d\u8bbe\u7f6e Privacy - Location When In Use Usage Description\n        \/\/\u5982\u679c\u4e0d\u9700\u8981\u663e\u793a\u5f53\u524d\u7528\u6237\u4f4d\u7f6e\uff0c\u5219\u65e0\u9700\u7533\u8bf7\u6743\u9650\n        #if os(iOS)\n            manager.requestWhenInUseAuthorization()\n        #endif\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Swift2.0 \u4e2d\uff0c\u82f9\u679c\u6dfb\u52a0\u4e86 Map\uff0c\u8ba9\u5f00\u53d1\u8005\u53ef\u4ee5\u975e\u5e38\u5bb9\u6613\u7684\u5728 View \u4e2d\u6dfb\u52a0\u9700\u8981\u7684\u5730\u56fe\u5143\u7d20\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4903"}],"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=4903"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4903\/revisions"}],"predecessor-version":[{"id":4904,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4903\/revisions\/4904"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}