在 swift 的 google 地图上显示坐标
Show co-ordinates on google map in swift
我正在从 json api
中获取坐标
Alamofire.request(Constants.API.url("api/trip/"mykey"/startTrip"), method: .post, parameters: nil, encoding: URLEncoding.default, headers: header).responseJSON { (response:DataResponse<Any>) in
self.progressHUD.dismiss(afterDelay: 0.5)
switch(response.result) {
case .success(_):
guard let json = response.result.value as! [String:Any]? else{ return}
guard let response = json["response"] as! [String:Any]? else{ return}
guard let endCoordinates = response["end_coordinates"] as? [CLLocationDegrees] else { return }
print(endCoordinates)
guard let startCoordinates = response["start_coordinates"] as? [CLLocationDegrees] else { return}
print(startCoordinates)
let markerStart = GMSMarker(position: CLLocationCoordinate2D(latitude: startCoordinates[0] , longitude: startCoordinates[1] ))
print(markerStart)
markerStart.map = self.mapView
let markerEnd = GMSMarker(position: CLLocationCoordinate2D(latitude: endCoordinates[0] , longitude: endCoordinates[1] ))
markerEnd.map = self.mapView
print(markerEnd)
break
case .failure(_):
print("error")
break
}
}
但我无法在 google 地图上显示它。老实说,我不知道如何在 google 地图上显示我的坐标?。有帮助吗?
//在邮递员中获取坐标
您需要将字符串数组坐标转换为 CLLocationCoordinate2D
坐标并创建两个标记并将它们分配给您的 GMSMapView
代码
Alamofire.request(Constants.API.url("api/trip//startTrip"), method: .post, parameters: nil, encoding: URLEncoding.default, headers: header).responseJSON { (response:DataResponse<Any>) in
self.progressHUD.dismiss(afterDelay: 0.5)
switch(response.result) {
case .success(_):
guard let json = response.result.value as? [String:Any] else{ return}
guard let response = json["response"] as? [String:Any] else{ return}
guard let endCoordinates = response["end_coordinates"] as? [CLLocationDegrees] else { return }
print(endCoordinates)
guard let startCoordinates = response["start_coordinates"] as? [CLLocationDegrees] else { return}
print(startCoordinates)
DispatchQueue.main.async {
let markerStart = GMSMarker(position: CLLocationCoordinate2D(latitude: startCoordinates[0], longitude: startCoordinates[1]))
markerStart.map = self.mapView
let markerEnd = GMSMarker(position: CLLocationCoordinate2D(latitude: endCoordinates[0], longitude: endCoordinates[1]))
markerEnd.map = self.mapView
}
case .failure(_):
print("error")
break
}
}
我正在从 json api
中获取坐标Alamofire.request(Constants.API.url("api/trip/"mykey"/startTrip"), method: .post, parameters: nil, encoding: URLEncoding.default, headers: header).responseJSON { (response:DataResponse<Any>) in
self.progressHUD.dismiss(afterDelay: 0.5)
switch(response.result) {
case .success(_):
guard let json = response.result.value as! [String:Any]? else{ return}
guard let response = json["response"] as! [String:Any]? else{ return}
guard let endCoordinates = response["end_coordinates"] as? [CLLocationDegrees] else { return }
print(endCoordinates)
guard let startCoordinates = response["start_coordinates"] as? [CLLocationDegrees] else { return}
print(startCoordinates)
let markerStart = GMSMarker(position: CLLocationCoordinate2D(latitude: startCoordinates[0] , longitude: startCoordinates[1] ))
print(markerStart)
markerStart.map = self.mapView
let markerEnd = GMSMarker(position: CLLocationCoordinate2D(latitude: endCoordinates[0] , longitude: endCoordinates[1] ))
markerEnd.map = self.mapView
print(markerEnd)
break
case .failure(_):
print("error")
break
}
}
但我无法在 google 地图上显示它。老实说,我不知道如何在 google 地图上显示我的坐标?。有帮助吗?
您需要将字符串数组坐标转换为 CLLocationCoordinate2D
坐标并创建两个标记并将它们分配给您的 GMSMapView
代码
Alamofire.request(Constants.API.url("api/trip//startTrip"), method: .post, parameters: nil, encoding: URLEncoding.default, headers: header).responseJSON { (response:DataResponse<Any>) in
self.progressHUD.dismiss(afterDelay: 0.5)
switch(response.result) {
case .success(_):
guard let json = response.result.value as? [String:Any] else{ return}
guard let response = json["response"] as? [String:Any] else{ return}
guard let endCoordinates = response["end_coordinates"] as? [CLLocationDegrees] else { return }
print(endCoordinates)
guard let startCoordinates = response["start_coordinates"] as? [CLLocationDegrees] else { return}
print(startCoordinates)
DispatchQueue.main.async {
let markerStart = GMSMarker(position: CLLocationCoordinate2D(latitude: startCoordinates[0], longitude: startCoordinates[1]))
markerStart.map = self.mapView
let markerEnd = GMSMarker(position: CLLocationCoordinate2D(latitude: endCoordinates[0], longitude: endCoordinates[1]))
markerEnd.map = self.mapView
}
case .failure(_):
print("error")
break
}
}