我有两个数组,一个是纬度,另一个是经度。我想把它传递给CLLocationCoordinate2DMake。从响应来看,我得到的是纬度和经度,但它们是字符串格式的。我要把它转换成双倍。但是当我传递这个数组时,它会显示一个错误Cannot convert value of type '[Double]' to expected argument type 'CLLocationDegrees' (aka 'Double')。
我试过这个密码,
var latitudeArray = [Double]()
var longitudeArray = [Double]()
latitudeArray = UserDefaults.standard.array(forKey: "latitudeArray") as! [Double]
longitudeArray = UserDefaults.standard.array(forKey: "longitudeArray") as! [Double]
print(latitudeArray)
print(longitudeArray)
let location = CLLocationCoordinate2DMake(latitudeArray, longitudeArray)
mapView.setRegion(MKCoordinateRegionMakeWithDistance(location, 1500, 1500), animated: true)
let pin = MapPin.init(title: name!, locationName: name!, coordinate: location)
mapView.addAnnotation(pin).我怎样才能把数组传递给它呢?我想要这张地图显示我要传递给它的lat和lng的所有位置。
发布于 2018-02-28 15:50:05
您的代码有一些缺陷。首先,CLLocationCoordinate2DMake为每个参数取一个Double,当您试图使用Array of Doubles时,您的代码还不清楚,您的代码是否除了拟合存储到UserDefaults的所有坐标之外,是否还想在地图上显示引脚,所以下面的代码将两者兼而有之。
以下代码将
这就是你要的!
let latitudes = [Double]()
let longitudes = [Double]()
let names = [String]()
let coordinates = zip(latitudes, longitudes).map(CLLocationCoordinate2D.init)
let annotations = zip(coordinates, names)
.map { (coordinate, name) -> MKPointAnnotation in
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = name
return annotation
}
map.addAnnotations(annotations)
map.showAnnotations(annotations, animated: true)来自showAnnotations文档:
设置可见区域,以便映射显示指定的注释。
https://stackoverflow.com/questions/49033222
复制相似问题