简体   繁体   English

将 CLLocationCoordinate2D 转换为可存储的 String

[英]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.我试图在一个 ViewController 中保存用户的坐标,以便它可以用于创建一个可以在另一个 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?它告诉我String?类型的值String? to a value of type CLLocationCoordinate2D .CLLocationCoordinate2D类型的值。

So how do I convert the CLLocationCoordinate2D coordinates into a value of type String ?那么如何将CLLocationCoordinate2D坐标转换为String类型的值?

This way you can store Locations to NSUserDefaults : 这样,您可以将Locations存储到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 : 然后在您的示例代码中使用as:


    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: 将它们包装在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:使用 sprint 格式:

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

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

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