存储CLLocation并保留注释 [英] Storing CLLocation and keeping annotation

查看:86
本文介绍了存储CLLocation并保留注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序当前具有三个选项卡,一个用于固定位置的选项卡,以及第二个选项卡上固定位置的detailView.我正在尝试将引脚的位置保存到NSUserdefaults中.然后,我希望该位置在重新加载应用程序时保持固定,因此详细视图仍将显示固定位置的详细视图.这是我到目前为止所拥有的,

My app currently has three tabs, a tab for pinning a location, and a detailView of the pinned location on the second tab. I am trying to save the location of the pin into NSUserdefaults. I would then like that location to stay pinned upon reloading the app, and therefore the detail view would still display the detail view of the pinned location. Here is what I have so far,

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    // Find location of user
    var userLocation:CLLocation = locations[0] as! CLLocation
    var latitude = userLocation.coordinate.latitude
    var longitude = userLocation.coordinate.longitude
    var latDelta:CLLocationDegrees = 0.01
    var longDelta: CLLocationDegrees = 0.01
    var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var location:MKUserLocation = currentLocation;
    var region: MKCoordinateRegion = MKCoordinateRegionMake(location.coordinate, span)
    var coordinate:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude);
    carInitialLocation = userLocation;
    let locationData = NSKeyedArchiver.archivedDataWithRootObject(carInitialLocation);
    NSUserDefaults.standardUserDefaults().setObject(locationData, forKey: "locationData");
    carInitialCoordinate = coordinate;

    self.map.setRegion(region, animated: true);
}


override func viewDidLoad() {

    super.viewDidLoad()
    if let loadedData = NSUserDefaults.standardUserDefaults().dataForKey("locationData") {
        if let loadedLocation = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData) as? CLLocation {
            println(loadedLocation.coordinate.latitude);
            println(loadedLocation.coordinate.longitude);
            var annotation = MKPointAnnotation()
            annotation.coordinate = loadedLocation.coordinate
            annotation.title = title
            self.map.addAnnotation(annotation)

        }
    }

    map.addAnnotations(artworks)
    map.delegate = self;
    manager.delegate = self;
    manager.desiredAccuracy = kCLLocationAccuracyBest;
    manager.requestWhenInUseAuthorization();
    manager.startUpdatingLocation();
    self.map.showsUserLocation = true;
    currentLocation = map.userLocation;
}

然后,我希望一旦用户固定一次位置后停用pinLocation按钮.我尝试这样做:

I then want the pinLocation button to be deactivated once the user has pinned a location once. I try to do this as so:

@IBAction func pinLocationButton(sender: AnyObject) {
    // add location to the array, so it can be retrieved and put it into temporary storage
    //places.append(["name":title,"lat":"\(newCoordinate.latitude)","lon":"\(newCoordinate.longitude)"])
    if let loadedData = NSUserDefaults.standardUserDefaults().dataForKey("locationData") {
        if let loadedLocation = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData) as? CLLocation {
            println(loadedLocation.coordinate.latitude);
            println(loadedLocation.coordinate.longitude);
            pinLocationButton.enabled = false;

        }
    }


    var location = carInitialLocation
    var coordinate = carInitialCoordinate

    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
        var title = ""
        if (error == nil) {
            if let p = CLPlacemark(placemark: placemarks?[0] as! CLPlacemark) {

                var subThoroughfare:String = ""
                var thoroughfare:String = ""

                if p.subThoroughfare != nil {

                    subThoroughfare = p.subThoroughfare
                }
                if p.thoroughfare != nil {

                    thoroughfare = p.thoroughfare

                }
                completeAddress = self.displayLocationInfo(p);
                title = "\(subThoroughfare) \(thoroughfare)"
            }
        }
                    // annotation, i.e pins

    var annotation = MKPointAnnotation()
    annotation.coordinate = coordinate
    annotation.title = title
    self.map.addAnnotation(annotation)
       // NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places")
    })
}

然后,在我的detailVC中,我尝试对固定的位置进行地理编码,但是它默认为当前位置. 这是代码:

Then, in my detailVC I attempt to reverse geocode the pinned location, but it is defaulting to the current location.. which I don't understand why Here's the code:

super.viewDidLoad()

    addressLabel.font = UIFont(name: addressLabel.font.fontName, size: 18)
    smallMapView.delegate = self;
    locationManager.delegate = self;
    smallMapView.mapType = MKMapType.Hybrid;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.requestWhenInUseAuthorization();
    locationManager.startUpdatingLocation();
    smallMapView.zoomEnabled = true;
    smallMapView.rotateEnabled = true;

    if let loadedData = NSUserDefaults.standardUserDefaults().dataForKey("locationData") {
        if let loadedLocation = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData) as? CLLocation {
            println(loadedLocation.coordinate.latitude);
            println(loadedLocation.coordinate.longitude);

            CLGeocoder().reverseGeocodeLocation(loadedLocation, 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 latitude = loadedLocation.coordinate.latitude;
                var longitude = loadedLocation.coordinate.longitude;
                var latDelta:CLLocationDegrees = 0.001;
                var longDelta:CLLocationDegrees = 0.001;

                var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta);
                var overallLoc = CLLocationCoordinate2DMake(latitude, longitude);
                var region:MKCoordinateRegion = MKCoordinateRegionMake(overallLoc, span);
                var annotation = MKPointAnnotation();
                annotation.coordinate = loadedLocation.coordinate;
                annotation.title = subtitle;
                self.smallMapView.addAnnotation(annotation);
                self.smallMapView.setRegion(region, animated: true)

            })

        }
    }


}

推荐答案

在pinLocationButton函数中,您使用的是manager.location错误,应该使用

in the pinLocationButton function, you use the manager.location which is wrong, you should use

CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
    var title = ""
    if (error == nil) {
    // ....

这篇关于存储CLLocation并保留注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆