gpt4 book ai didi

ios - MKMapView 选择注解

转载 作者:行者123 更新时间:2023-11-29 02:12:31 25 4
gpt4 key购买 nike

我是 swift 的新手,目前正在尝试弄清楚如何获取有关用户选择的注释的数据。我有一个本地搜索功能,可以添加注释,在用户选择一个后,我希望能够访问它。我正在尝试使用 selectedAnnotations,但它似乎不起作用。

本地搜索:

func performSearch(){
matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchTextField.text
request.region = mapView.region

let search = MKLocalSearch(request: request)

search.startWithCompletionHandler({(response:
MKLocalSearchResponse!,
error: NSError!) in

if error != nil {
println("Error occured in search: \(error.localizedDescription)")
} else if response.mapItems.count == 0 {
println("No matches found")
} else {
println("Matches found")

for item in response.mapItems as [MKMapItem] {
println("Name = \(item.name)")
println("Phone = \(item.phoneNumber)")

self.matchingItems.append(item as MKMapItem)
println("Matching items = \(self.matchingItems.count)")

var annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
annotation.subtitle = item.placemark.title
self.mapView.addAnnotation(annotation)

}
}
})

从那里我正在尝试使用

 var selectedAnnotations: [MKPointAnnotation]!
// print signout location
println(selectedAnnotations)

访问注释,但这只是返回“nil”

注解方法:

    @IBAction func signoutToLocationButton(sender: AnyObject) {
// saves current user location
PFGeoPoint.geoPointForCurrentLocationInBackground {
(geoPoint: PFGeoPoint!, error: NSError!) -> Void in
if error == nil {
// do something with the new geoPoint
println(geoPoint)

var signoutLocation = PFObject(className: "SignoutLocation")
signoutLocation["Location"] = geoPoint
signoutLocation.saveInBackgroundWithBlock {
(success: Bool, error: NSError!)-> Void in
if (success) {
// has been saved
}
else {
//went wrong
}
}

}

// get location of where they are signing out to
self.mapView.selectedAnnotations(AnyObject)
// print signout location
// println(selectedAnnotations)





}

最佳答案

下面是如何使用 selectedAnnotations 属性的示例:

if self.mapView.selectedAnnotations?.count > 0 {

if let ann = self.mapView.selectedAnnotations[0] as? MKAnnotation {

println("selected annotation: \(ann.title!)")

let c = ann.coordinate
println("coordinate: \(c.latitude), \(c.longitude)")

//do something else with ann...
}
}

(尽管您是否需要或想要在 //has been saved block 内而不是在外部执行此操作是您必须弄清楚的事情。)

关于ios - MKMapView 选择注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29132051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com