我正在尝试开发一个可维护两个位置的iOS应用。一个是当前位置,另一个是新位置,该位置将由mapkit视图的中心位置确定。
问题是我正在与developer.org进行反向地理编码,并且已经在当前位置上使用locationManager updateLocation函数,然后使用GET REQUEST将其更新到其服务器。
我的问题是如何同时维护两者?因为现在只更新当前位置。
locationManager功能:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locationList = locations[0] //holds the users locations in a list starting from the first location
let span:MKCoordinateSpan = MKCoordinateSpan.init(latitudeDelta: 0.01, longitudeDelta: 0.01) //determines how zoomed in we'll be on the user using the map
let currentLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(locationList.coordinate.latitude, locationList.coordinate.longitude)
// let previousLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(locationList.coordinate.latitude, locationList.coordinate.longitude) // for pin
let region: MKCoordinateRegion = MKCoordinateRegion.init(center: currentLocation, span: span)
mapView.setRegion(region, animated: true)
self.mapView.showsUserLocation = true //will show the users location as a blue dot
let center = getCenterLocation(for: mapView) // gets latitude and longitude coordinates
//code ...
do {
let result = try JSONDecoder().decode(Places.self, from: data)
DispatchQueue.main.async {
addrs = (result.response?.view?[0].result?[0].location?.address?.label)!
self.locationLabel.text = ("Your Current Location Is :" + addrs)
//print(addrs)
}
} catch let jsonError {
print("Error doing a JSONSerilization", jsonError)
}
新的位置代码:
let sessionForPin = URLSession.shared //creates a new url session
sessionForPin.dataTask(with: requestForPin) { (pin_data, pin_response, pin_error) in
// let response = response as? HTTPURLResponse,error == nil
// guard let data = data else {return}
guard let pin_data = pin_data,
let pin_response = pin_response as? HTTPURLResponse,
pin_error == nil else { // check for fundamental networking error
print("error", pin_error ?? "Unknown error")
return
}
guard (200 ... 299) ~= pin_response.statusCode else {
// check for http errors
print("statusCode should be 2xx, but is \(pin_response.statusCode)") //checks that we got a good response if not then it prints
print("response = \(pin_response)")
return
}
do {
let result = try JSONDecoder().decode(Places.self, from: pin_data)
DispatchQueue.main.async {
pin_addrs = (result.response?.view?[0].result?[0].location?.address?.label)!
self.newLocButton.setTitle( pin_addrs, for: .normal)
}
//code ..
答案 0 :(得分:1)
从您的代码中,我看到您正在使用Apple MapKit框架,并尝试将其与developer.here API连接起来。
我建议您不要使用其他数据提供程序。最好选择其中之一以避免可能的数据不兼容。
选项A:
您可以使用HERE Mobile SDK实现您的目标。 有2个样本:
1)Geocoder-显示如何使用地理编码和反向地理编码
2)Positioning-显示如何获取当前位置
选项B:
继续使用MapKit,并开始使用reverseGeocodeLocation:completionHandler
类(More details about reverse geocoding using CLGeocoder)的Apple地理编码方法CLGeocoder
。