简体   繁体   中英

Converting CLLocationCoordinate2D to a String that can be stored

I'm trying to save the coordinates of a user while in one ViewController so that it can be used to create an Annotation that can displayed in another ViewController.

In the view controller that stores the coordinates I'm using the code

NSUserDefaults.standardUserDefaults().setObject( Location, forKey: "Location")

In the map view controller that displays the annotation I'm trying to get the coordinates using the code

let Location = NSUserDefaults.standardUserDefaults().stringForKey("Location")
var Annotation = MKPointAnnotation()
Annotation.coordinate = Location    

It is telling me that the value of type String? to a value of type CLLocationCoordinate2D .

So how do I convert the CLLocationCoordinate2D coordinates into a value of type String ?

This way you can store Locations to NSUserDefaults :

//First Convert it to NSNumber.
let lat : NSNumber = NSNumber(double: Location.latitude)
let lng : NSNumber = NSNumber(double: Location.longitude)

//Store it into Dictionary
let locationDict = ["lat": lat, "lng": lng]

//Store that Dictionary into NSUserDefaults
NSUserDefaults.standardUserDefaults().setObject(locationDict, forKey: "Location")

After that you can access it this way:

//Access that stored Values
let userLoc = NSUserDefaults.standardUserDefaults().objectForKey("Location") as! [String : NSNumber]

//Get user location from that Dictionary
let userLat = userLoc["lat"]
let userLng = userLoc["lng"]

var Annotation = MKPointAnnotation()

Annotation.coordinate.latitude = userLat as! CLLocationDegrees  //Convert NSNumber to CLLocationDegrees
Annotation.coordinate.longitude = userLng as! CLLocationDegrees //Convert NSNumber to CLLocationDegrees

UPDATE:

HERE is your Example project.

extension CLLocationCoordinate2D:Printable
{
    init(coords : String)
    {
        var fullNameArr = split(coords) {$0 == ";"}
        self.latitude = NSNumberFormatter().numberFromString(fullNameArr[0])!.doubleValue
        self.longitude = (fullNameArr.count > 1) ? NSNumberFormatter().numberFromString(fullNameArr[1])!.doubleValue : 0
    }

    public var description : String
    {
        return "\(self.latitude);\(self.longitude)"
    }
}

Then use as in your sample code :


    var coord = CLLocationCoordinate2D(latitude: 3.2, longitude: 6.4)
    NSUserDefaults.standardUserDefaults().setObject(coord.description, forKey: "Location")
    var readedCoords = CLLocationCoordinate2D(coords: NSUserDefaults.standardUserDefaults().stringForKey("Location")!)

You can store the latitude or the longitude (or both in a dictionary or a tuple). The way to wrap them in String:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    var locValue:CLLocationCoordinate2D = manager.location!.coordinate        
    var lat : String = locValue.latitude.description
    var lng : String = locValue.longitude.description
    //do whatever you want with lat and lng
}

Using an sprint kind of formatting:

func Coord2String(location : CLLocationCoordinate2D) -> String {
    return  String(format : "latitude : %f, longitude : %f", location.latitude, location.longitude)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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