简体   繁体   English

搜索回到原始地图后,注释图钉会重置(Mapkit Swift)

[英]Annotation pins reset after segue back to original map (Mapkit Swift)

I am using MapKit's local search feature to populate a bunch of pin annotations based on a user's search. 我正在使用MapKit的本地搜索功能根据用户的搜索填充一堆图钉注释。 When you click on a pin, in the callout you go to a detail viewcontroller of the location. 单击图钉时,在标注中,您将转到该位置的详细信息视图控制器。 When the user segues and clicks the back button on the toolbar from the detailviewcontroller to the previous viewcontroller, all the annotation pins are gone. 当用户搜索并单击工具栏上的从detailviewcontroller到前一个viewcontroller的后退按钮时,所有注释钉都消失了。 Is there any way to avoid this? 有什么办法可以避免这种情况?

here is my code for the localsearch: 这是我的本地搜索代码:

@IBAction func returnText(sender: AnyObject) {
    sender.resignFirstResponder();
    attractionsMap.removeAnnotations(attractionsMap.annotations);
    performSearch();
}
func performSearch() {


    matchingItems.removeAll()

    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchText.text;

    request.region = attractionsMap.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)")

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

                var placemark = item.placemark;
                var subThoroughfare:String = "";
                var thoroughfare:String = "";
                var locality:String = "";
                var postalCode:String = "";
                var administrativeArea:String = "";
                var country:String = "";
                var title = "";
                var subtitle = "";

                if (placemark.subThoroughfare != nil) {
                    subThoroughfare = placemark.subThoroughfare;
                }
                if(placemark.thoroughfare != nil) {
                    thoroughfare = placemark.thoroughfare;
                }
                if(placemark.locality != nil) {
                    locality = placemark.locality;
                }
                if(placemark.postalCode != nil) {
                    postalCode = placemark.postalCode;
                }
                if(placemark.administrativeArea != nil) {
                    administrativeArea = placemark.administrativeArea;
                }
                if(placemark.country != nil) {
                    country = placemark.country;
                }
                println("viewcontroller placmark data:");
                println(locality);
                println(postalCode);
                println(administrativeArea);
                println(country);

                title = " \(subThoroughfare) \(thoroughfare) \n \(locality), \(administrativeArea) \n \(postalCode)\(country)";
                subtitle = " \(subThoroughfare) \(thoroughfare)";
                println(title);

                var annotation = MKPointAnnotation()
                annotation.coordinate = item.placemark.coordinate
                annotation.title = item.name + " " + subtitle;
                self.attractionsMap.addAnnotation(annotation)
            }
        }
    })
}

I then pass the placemark of the matching items through this method... 然后,我通过此方法传递匹配项的地标...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var attractionsDetailViewController:AttractionsDetailViewController = segue.destinationViewController as! AttractionsDetailViewController
    attractionsDetailViewController.attractionLocation = indicatedMapItem;
}

ANy help would be greatly appreciated. 任何帮助将不胜感激。

You can make use of NSUserDefaults , "used to determine an application's default state". 您可以使用NSUserDefaults ,“用于确定应用程序的默认状态”。 Have a look at: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/ 看看: https : //developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM