【问题标题】:Converting CLLocationCoordinate2D to a String that can be stored将 CLLocationCoordinate2D 转换为可以存储的字符串
【发布时间】:2015-08-28 02:10:02
【问题描述】:

我正在尝试在一个 ViewController 中保存用户的坐标,以便可以使用它来创建可以在另一个 ViewController 中显示的 Annotation。

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

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

在显示注释的地图视图控制器中,我尝试使用代码获取坐标

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

它告诉我String? 类型的值转换为CLLocationCoordinate2D 类型的值。

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

【问题讨论】:

标签: ios swift location cllocation nscoding


【解决方案1】:

这样您就可以将位置存储到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

更新:

HERE 是您的示例项目。

【讨论】:

  • 因此,出于某种原因,每当我按下另一个视图控制器上的按钮以保存位置并且对另一个显示地图视图的视图控制器执行 segue 时,注释未显示。跨度>
【解决方案2】:
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)"
    }
}

然后在您的示例代码中使用:


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

【讨论】:

    【解决方案3】:

    您可以存储纬度或经度(或同时存储在字典或元组中)。将它们包装在 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
    }
    

    【讨论】:

      【解决方案4】:

      使用 sprint 格式:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-09
        • 1970-01-01
        • 1970-01-01
        • 2019-05-23
        • 2021-11-11
        • 2015-01-20
        相关资源
        最近更新 更多