如何将 CLLocation 传递给不同的 ViewController

How to pass CLLocation to different ViewController

我的应用程序有两个选项卡。在第一个选项卡上,根据景点创建了一堆选项卡注释。当您单击注释视图时,它会转到第二个选项卡,这是景点的详细视图控制器。我想知道如何将景点的位置传递给 detailVC,这样我就可以在同一地址获得 pin。这是我目前所知道的,并且有一些不一致,因为详情中显示的地址 vc 不等于景点的地址。

func performSearch(input:String) {

        attractionsMap.removeAnnotations(attractionsMap.annotations);

        matchingItems.removeAll()

        let request = MKLocalSearchRequest()
        request.naturalLanguageQuery = input
        println(input);
        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)
                }
            }
        })
    }

这是搜索和添加引脚注释。

然后我尝试发送下面的 attractionLocation

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

然后是我的详细信息 ViewController 来实现给定的地址:

func getInfo() {
    var latitude = attractionLocation.latitude;
    var longitude  = attractionLocation.longitude;
    var latDelta:CLLocationDegrees = 0.000001
    var longDelta: CLLocationDegrees = 0.000001
    var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta);
    var location = CLLocationCoordinate2DMake(latitude, longitude);
    var realLocation = CLLocation(latitude: latitude, longitude: longitude);
    CLGeocoder().reverseGeocodeLocation(realLocation, completionHandler: { (placemarks, error) -> Void in
        var title = ""
        var subtitle = ""
        var locality = ""
        if(error == nil) {
            if let placemark = CLPlacemark(placemark: placemarks?[0] as! CLPlacemark) {
                var subThoroughfare:String = "";
                var thoroughfare:String = "";
                var locality:String = "";
                var postalCode:String = "";
                var administrativeArea:String = "";
                var country:String = "";

                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);
                self.addressLabel.text = title;

            }
        }
        var overallLoc = CLLocationCoordinate2DMake(latitude, longitude);
        var region:MKCoordinateRegion = MKCoordinateRegionMake(overallLoc, span);
        var annotation = MKPointAnnotation();
        annotation.coordinate = location;
        annotation.title = subtitle;
        self.detailMap.addAnnotation(annotation);
        self.detailMap.setRegion(region, animated: true)
    })
}

如果你像这样在detailsVC中设置你的attractionLocation 属性...

var attractionLocation: CLLocation?
var attractionAddress: String?

据我所知,getInfo 函数只需要设置地图的区域并添加适当的注释。 由于 attractionLocation 属性 将是一个 CLLocation,我们可以访问它的坐标 属性。如果您从第一个屏幕通过 attractionAddress,则不需要使用 CLGeocoder。

func getInfo() {

    // Create span for the map region
    var latDelta:CLLocationDegrees = 0.000001
    var longDelta: CLLocationDegrees = 0.000001
    var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta);


    // Create region for map
    var region:MKCoordinateRegion = MKCoordinateRegionMake(attractionLocation!.coordinate, span);
    var annotation = MKPointAnnotation();
    annotation.coordinate = attractionLocation!.coordinate;
    annotation.title = attractionAddress!;
    self.detailMap.addAnnotation(annotation);
    self.detailMap.setRegion(region, animated: true)
}

过去,我发现 CLGeocoder 在反向地理编码方面并不那么一致。所以至少这样你可以确定两个屏幕之间的地址是一致的。

可以存储全局变量,一般不推荐,也可以通过segue传递,推荐。最好不要再次进行反向地理编码,因为反向地理编码并不总是查找位置的最准确方法...