将从网站接收的数据放在地图视图中

时间:2018-07-14 23:24:04

标签: ios swift3

使用getURL()函数,我成功从网站接收了数据,我想将纬度和经度放入mapView中,但是发现一个错误,表明CLL类型的位置与字符串不兼容, 我想至少放置自己的位置,然后在地图视图中看到它,但是我在地图上找不到任何东西。 你能帮我吗?

/* func getURL(){
 var request = URLRequest(url: URL(string: "http://gpsforanimal.herokuapp.com/positions.json")!)
 request.httpMethod = "GET"
 let session = URLSession.shared
 session.dataTask(with: request) {data, response, error in
 if error != nil {
 print(error!.localizedDescription)
 }
 guard let data = data else { return }
 do {
 //Decode retrived data with JSONDecoder and assing type of Article object
 let realData = try JSONDecoder().decode([gps].self, from: data)
 //print(realData)
 if let data = realData.last {
 print(data.latitude!)

 print(data.longitude!)
 print(data.created_at)
 print(data.updated_at)
 }
 } catch let jsonError {
 print(jsonError)
 }
 }.resume()
 }*/
/* struct gps : Codable{
 var id = Int()
 var longitude  : String?
 var latitude : String?
 var created_at = String()
 var updated_at = String()
 var url = String()

 init(){
 self.id = Int()
 self.longitude = String()
 self.latitude  = String()
 self.created_at = String()
 self.updated_at = String()
 self.url = String()
 }
 } */


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    map.showsPointsOfInterest = true

    // map.showsUserLocation = true
    manager.requestAlwaysAuthorization()
    manager.requestWhenInUseAuthorization()

    //user location stuff
    if CLLocationManager.locationServicesEnabled() {
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.startUpdatingLocation()
    }

    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
    let location = CLLocationCoordinate2D(latitude: 34.719913, longitude: 10.718982)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
    map.setRegion(region, animated: true)
    self.map.showsUserLocation = true

    func locationManager(_manager: CLLocationManager, didUpdateLocations locations:[CLLocation])
    {///
    }
}

0 个答案:

没有答案