MapView unable to convert Lat/Long to Double

I am trying to set the mapView based on Lat Long but I belive they need to be set as Double and not String?!


if let articlesFromJson = json["articles"] as? [[String:AnyObject]] {
                    for articleFromJSON in articlesFromJson{
                        if let title = articleFromJSON["CompanyName"] as? String, let desc = articleFromJSON["SICDescriptions"] as? String, let latitude = articleFromJSON["lat"] as? String, let longitude = articleFromJSON["lon"] as? String {
                           
                            DispatchQueue.main.async {
                                self.mainTitle.text = title
                                self.subMainTitle.text = desc
                                let lat:CLLocationDegrees = latitude
                                let lon:CLLocationDegrees = longitude
                                let deltaLatitude: CLLocationDegrees = 0.01
                                let deltaLongitude: CLLocationDegrees = 0.01
                                let span: MKCoordinateSpan = MKCoordinateSpanMake(deltaLatitude, deltaLongitude)
                                let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, lon)
                                let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
                                self.mapView.setRegion(region, animated: true)
                            }
                        }
                       
                    }

Replies

You can convert a string to a double like so:

let s = "1.23"
let d = Double(s)
print(d)
// Optional(1.23)

Note that

d
is an optional because the conversion might fail; based on the code you posted it looks like you already know how to deal with optionals.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"