Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将CLLocationCoordinate2D转换为可存储的字符串_Ios_Swift_Location_Cllocation_Nscoding - Fatal编程技术网

Ios 将CLLocationCoordinate2D转换为可存储的字符串

Ios 将CLLocationCoordinate2D转换为可存储的字符串,ios,swift,location,cllocation,nscoding,Ios,Swift,Location,Cllocation,Nscoding,我试图在一个ViewController中保存用户的坐标,以便可以使用它创建可以在另一个ViewController中显示的注释 在存储坐标的视图控制器中,我正在使用代码 NSUserDefaults.standardUserDefaults().setObject( Location, forKey: "Location") 在显示注释的地图视图控制器中,我尝试使用代码获取坐标 let Location = NSUserDefaults.standardUserDefaults().stri

我试图在一个ViewController中保存用户的坐标,以便可以使用它创建可以在另一个ViewController中显示的注释

在存储坐标的视图控制器中,我正在使用代码

NSUserDefaults.standardUserDefaults().setObject( Location, forKey: "Location")
在显示注释的地图视图控制器中,我尝试使用代码获取坐标

let Location = NSUserDefaults.standardUserDefaults().stringForKey("Location")
var Annotation = MKPointAnnotation()
Annotation.coordinate = Location    
它告诉我类型
String?
的值与类型
CLLocationCoordinate2D
的值相同


那么如何将
CLLocationCoordinate2D
坐标转换为
String
类型的值呢?

通过这种方式,您可以将位置存储到
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")
之后,您可以通过以下方式访问它:

//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
更新:

这是您的示例项目

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)"
    }
}
然后在示例代码中使用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")!)

您可以在字典或元组中存储纬度或经度(或两者)。将它们包装成字符串的方法:

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
}

由于某种原因,每当我按下另一个视图控制器上的按钮以保存位置时,So可能会重复,并且会对另一个显示地图视图的视图控制器执行一个序列,而注释不会显示。