How do I compare two MKPlacemarks?

I compare two MKPlacemarks that are each created from a CLPlacemark acquired from CLGeocoder.reverseGeocodeLocation(_:) of the same address, but when I compare those two MKPlacemarks with == they don't equal. Is there a way of determining whether those two placemarks refer to the same location? Are there unique identifers on MKPlacemarks that I could use to determine if those two placemarks are of the same location, such as the name or the title properities of MKPlacemark?

Answered by ShinehahGnolaum in 301912022

Below is the code you asked for.


I get the MKPlacemarks by creating them from the CLPlacemarks I get from CLGeocoder.reverseGeocodeLocation.


I figured out that what are equal are the MKPlacemark.title propery from CLGeocoder. I noticed that the latitude and longitude properties don't equal for MKPLacemarks of the same location. I think it's like you said -- comparing variables of type Double give the result of false.


            geocoder.reverseGeocodeLocation(geoLocation) {
              
                placemarks, error in
              
                guard error == nil else {
                  
                    print("!!!\n", error!)
                  
                    return
                  
                }
              
                guard let placemarks = placemarks, (placemarks.count > 0) else {
                  
                    print("!!! No placemarks were found!")
                  
                    return
                  
                }
              
                let clFirstPlacemark = placemarks[0]
              
                self.originalPlacemark = MKPlacemark(placemark: clFirstPlacemark)
               
            } // geocoder.reverseGeocodeLocation(currentLocation)

I would compare on specific properties equality instead of global ==.


Can you show how you exactly get the 2 CLGeocoder.reverseGeocodeLocation ?


Note: could you log the 2 placemarks you compare, and see what is different ?

Maybe the comparison of locations (which are Double) lead to a false result comparison.

Accepted Answer

Below is the code you asked for.


I get the MKPlacemarks by creating them from the CLPlacemarks I get from CLGeocoder.reverseGeocodeLocation.


I figured out that what are equal are the MKPlacemark.title propery from CLGeocoder. I noticed that the latitude and longitude properties don't equal for MKPLacemarks of the same location. I think it's like you said -- comparing variables of type Double give the result of false.


            geocoder.reverseGeocodeLocation(geoLocation) {
              
                placemarks, error in
              
                guard error == nil else {
                  
                    print("!!!\n", error!)
                  
                    return
                  
                }
              
                guard let placemarks = placemarks, (placemarks.count > 0) else {
                  
                    print("!!! No placemarks were found!")
                  
                    return
                  
                }
              
                let clFirstPlacemark = placemarks[0]
              
                self.originalPlacemark = MKPlacemark(placemark: clFirstPlacemark)
               
            } // geocoder.reverseGeocodeLocation(currentLocation)

If you want to be sure, print the result of comparison of both latitudes.


And don't forget to close the thread when done.

So, that was the problem.


When you say :

I figured out that what are equal are the MKPlacemark.title propery from CLGeocoder. I noticed that the latitude and longitude properties don't equal for MKPLacemarks of the same location.


Could you show the different coordinates you get ?


Discussion hereafter digs into details for comparing Double, using ulpOfOne

h ttps://stackoverflow.com/questions/43911229/comparing-two-identical-double-value-return-false-swift-3

How do I compare two MKPlacemarks?
 
 
Q