让两个变量在同一个locationManager中显示不同的位置

问题描述 投票:0回答:1

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

问题是我正在使用developer.here API进行反向gecoding,我已经在当前位置使用了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 ..
ios swift core-location here-api geocode
1个回答
1
投票

从您的代码中我看到您正在使用Apple MapKit框架并尝试将其与developer.here API连接。

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

选项A:

您可以使用HERE Mobile SDK实现目标。有2个样本:

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

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

选项B:

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

© www.soinside.com 2019 - 2024. All rights reserved.