ios - 在同一个 locationManager 中有两个变量显示不同的位置

标签 ios swift core-location here-api geocode

我正在尝试开发一个维护两个位置的 iOS 应用程序。一个是当前位置,另一个是将由 mapkit View 的中心位置确定的新位置。

问题是我正在使用 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 连接。

我建议您不要使用不同的数据提供者。最好选择其中之一,以避免可能的数据不兼容。

选项 A:

您可以使用 HERE Mobile SDK 来实现您的目标。 有 2 个示例:

1) Geocoder - 展示如何使用地理编码和反向地理编码

2) Positioning - 显示如何获得当前位置

选项 B:

继续使用 MapKit 并开始使用 CLGeocoder 类 (More details about reverse geocoding using CLGeocoder) 的 Apple 地理编码方法 reverseGeocodeLocation:completionHandler

关于ios - 在同一个 locationManager 中有两个变量显示不同的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55540183/

相关文章:

ios - 当我尝试在函数前添加 private 时,Xcode 报错 "attribute private can only be used in a non local scope"

ios - CLLocationManager 不在 iOS 10.2 [Swift] 中调用 didUpdateLocation()

ios - 隐藏应用程序时停止定位

ios - Swift - 如何让分段控件锁定在屏幕顶部

swift - 取出后如何将uiview放回stackview?

ios - 如何使用 swift 作为 postman 请求测试来使用行 http 正文发出发布请求?

swift - "Compiler error: Invalid library file"在 Debug > Location 中模拟位置 - CoreLocation 和 MapKit

ios - 横向 UICollectionView 单行布局

ios - NSInvalidArgumentException 原因 : 'Storyboard doesn' t contain a view controller with identifier 'CenterViewController' '

android - 在 Google SDK 内部开发与外部开发