Search code examples
iosswiftcore-locationhere-apigeocode

Having two variables displaying different locations in the same locationManager


I'm trying to develop an iOS app that maintains two locations. One is the current location and the other is the new location that would be determined by where the center of the mapkit view is.

The thing is I'm doing reverse gecoding with developer.here API and I'm already using the locationManager updateLocation function on the current location and then updating it using a GET REQUEST to their server.

My question is how can I maintain both at once? Because right now only the current location is being updated.

The locationManager func:

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)
    }

The new location code :

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 ..

Solution

  • From your code I see that you are using Apple MapKit framework and trying to connect it with developer.here API.

    I recommend you to not use different data providers. It's better to select one of them to avoid possible data incompatibility.

    Option A:

    You can use HERE Mobile SDK for achieving your goal. There are 2 samples:

    1) Geocoder - shows how to use geocoding and reverse geocoding

    2) Positioning - shows how to get current position

    Option B:

    Keep working with MapKit and start use of Apple geocoding method reverseGeocodeLocation:completionHandler of CLGeocoder class (More details about reverse geocoding using CLGeocoder).