在同一个 locationManager 中有两个变量显示不同的位置
Having two variables displaying different locations in the same locationManager
我正在尝试开发一个维护两个位置的 iOS 应用程序。一个是当前位置,另一个是新位置,将由 mapkit 视图的中心位置确定。
我正在使用 developer.here API 进行反向地理编码,并且我已经在当前位置上使用了 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 ..
从您的代码中我看到您正在使用 Apple MapKit 框架并尝试将其与 developer.here API 连接。
我建议您不要使用不同的数据提供程序。最好 select 其中之一,以避免可能的数据不兼容。
选项A:
您可以使用 HERE Mobile SDK 来实现您的目标。
有 2 个示例:
1) Geocoder - 显示如何使用地理编码和反向地理编码
2) Positioning - 显示如何获得当前位置
选项B:
继续使用 MapKit 并开始使用 CLGeocoder
class (More details about reverse geocoding using CLGeocoder) 的 Apple 地理编码方法 reverseGeocodeLocation:completionHandler
。
我正在尝试开发一个维护两个位置的 iOS 应用程序。一个是当前位置,另一个是新位置,将由 mapkit 视图的中心位置确定。
我正在使用 developer.here API 进行反向地理编码,并且我已经在当前位置上使用了 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 ..
从您的代码中我看到您正在使用 Apple MapKit 框架并尝试将其与 developer.here API 连接。
我建议您不要使用不同的数据提供程序。最好 select 其中之一,以避免可能的数据不兼容。
选项A:
您可以使用 HERE Mobile SDK 来实现您的目标。 有 2 个示例:
1) Geocoder - 显示如何使用地理编码和反向地理编码
2) Positioning - 显示如何获得当前位置
选项B:
继续使用 MapKit 并开始使用 CLGeocoder
class (More details about reverse geocoding using CLGeocoder) 的 Apple 地理编码方法 reverseGeocodeLocation:completionHandler
。