使用 Swift 将 NSString 转换为 CLLocationCoordinate2DMake
Convert NSString to CLLocationCoordinate2DMake using Swift
如何将 NSString 转换为 CLLocationCoordinate2DMake。我想在地图上做一个标记。这是我所做的,但出现 "NSString is not convertable to CLLocationDegrees" 错误。
for openComplain in openComplains {
var lat = openComplain["lat"] as NSString
var long = openComplain["lng"] as NSString
NSLog("Lat is %@",lat)
NSLog("long is %@",long)
var complainLoc = CLLocationCoordinate2DMake(lat, long)
var marker : GMSMarker = GMSMarker(position: complainLoc)
marker.title = "Complain 1"
marker.map = self.mapView
}
我试过了
var lat = (openComplain["lat"] as NSString).doubleValue
var long = (openComplain["lng"] as NSString).doubleValue
但在那种情况下,我得到的是空值。请帮忙。
您只需在将字典可选值转换为 Double 之前对其进行解包:
var lat = (openComplain["lat"]! as NSString).doubleValue
var long = (openComplain["lng"]! as NSString).doubleValue
如何将 NSString 转换为 CLLocationCoordinate2DMake。我想在地图上做一个标记。这是我所做的,但出现 "NSString is not convertable to CLLocationDegrees" 错误。
for openComplain in openComplains {
var lat = openComplain["lat"] as NSString
var long = openComplain["lng"] as NSString
NSLog("Lat is %@",lat)
NSLog("long is %@",long)
var complainLoc = CLLocationCoordinate2DMake(lat, long)
var marker : GMSMarker = GMSMarker(position: complainLoc)
marker.title = "Complain 1"
marker.map = self.mapView
}
我试过了
var lat = (openComplain["lat"] as NSString).doubleValue
var long = (openComplain["lng"] as NSString).doubleValue
但在那种情况下,我得到的是空值。请帮忙。
您只需在将字典可选值转换为 Double 之前对其进行解包:
var lat = (openComplain["lat"]! as NSString).doubleValue
var long = (openComplain["lng"]! as NSString).doubleValue