Greg
Greg

Reputation: 34798

CLGeocoder reverseGeocodeLocation is returning placemark with different lat/long? (playground example attached)

Why does CLGeocoder reverseGeocodeLocation return a placemark with different lat/long when it looks up address?

Background: In terms of a user "long pressing" on the map to drop a pin, but my code reverse Geocoding this to be able to put an area name on the Pin, but then the PIN dropped is in a different location (i.e. not a good user experience). So I'm looking for a way to utilise the lat/long the user actually selected, but then just look up the location name.

Example: With code below I see:

Code:

import UIKit
import CoreLocation
import PlaygroundSupport

// Initial Test Co-ordinate
let locInput = CLLocation(latitude: -28.778021889561444, longitude: 152.97857401126666)
print("Input: \(locInput.coordinate.latitude), \(locInput.coordinate.longitude)")

// Initiate Reverse Geocoding
let geocoder: CLGeocoder = CLGeocoder()
print("About to call reverse geocoding function")
geocoder.reverseGeocodeLocation(locInput) { placemarks, error in
    guard let placemarks = placemarks else {fatalError("No Placemarks Provided \(error?.localizedDescription)")}
    for p in placemarks {
        print("OUTPUT:  \(p.location!.coordinate.latitude), \(p.location!.coordinate.longitude)")
    }
}

// Enable Asynch
PlaygroundPage.current.needsIndefiniteExecution = true

Upvotes: 5

Views: 587

Answers (1)

Ricowere
Ricowere

Reputation: 707

It's a correct behaviour. The placemark will returns it's associated location. Which obviously could differ from the input one. When you provide a location to the geocoder, this one will "infer" the placemark you want to get.

Suppose this:

 a: Associated location with the empire state building.
 b: Location you provided from an long press for instance.

    -------
    |     |
    |  a  | --> CLPlacemark: Empire state building, location: a
    |    b|
    ------- 

You were right in your assumptions. :)

Upvotes: 2

Related Questions