【问题标题】:Annotation pins reset after segue back to original map (Mapkit Swift)回到原始地图后注释引脚重置(Mapkit Swift)
【发布时间】:2015-07-13 01:03:39
【问题描述】:

我正在使用 MapKit 的本地搜索功能来根据用户的搜索填充一堆 pin 注释。当您单击一个图钉时,在标注中您将转到该位置的详细视图控制器。当用户从detailviewcontroller 切换到前一个viewcontroller 并单击工具栏上的后退按钮时,所有注释引脚都消失了。有什么办法可以避免吗?

这是我的本地搜索代码:

@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)
            }
        }
    })
}

然后我通过这个方法传递匹配项的地标...

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

任何帮助将不胜感激。

【问题讨论】:

    标签: ios swift maps mapkit segue


    【解决方案1】:

    您可以使用 NSUserDefaults,“用于确定应用程序的默认状态”。看看:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/

    【讨论】:

      猜你喜欢
      • 2011-01-06
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 2021-01-27
      相关资源
      最近更新 更多