Bhupesh Kumar
Bhupesh Kumar

Reputation: 419

How to fetch user current location and some other coordinate are same or not?

I want to match user current location with other location whether they are same or not.

My logic:

            point.coordinate = coordinate
            let userLat = mapView.userLocation?.coordinate.latitude
            let userLong = mapView.userLocation?.coordinate.longitude
            if point.coordinate.latitude == userLat && point.coordinate.longitude == userLong
            {
            point.title = "You are here"

            }
            else
            {
            point.title = "\(coordinate.latitude), \(coordinate.longitude)"
            }

But the probelm is that, mapView.userLocation?.coordinate.latitude is not equal to point.coordinate.latitude . However I am setting the same value for userLocation in simulator and other cooridinate.

Value I am setting for User Location is: Lat : 28.6 Long : 77.35

Value I am setting for other coordinate is: Lat : 28.6 Long : 77.35

Value I am getting for User Location is: Lat: Optional - Some : 3.4028234663852886e+38

Long:▿ Optional - Some : 3.4028234663852886e+38

Value I am getting for other coordinate is: Lat : 28.6 Long : 77.35

And if there is any better logic to find out this thing , then please tell me.

Upvotes: 0

Views: 163

Answers (1)

Anton Novoselov
Anton Novoselov

Reputation: 769

Is your point object - object of CLLocation subclass? If not, create CLLocation from your point.coordinate and compare as below. Try this compare CLLocation objects, instead of coordinates, using this approach:

let distanceThreshold: CLLocationDistance = 2.0 // in meters
if point.distanceFromLocation(mapView.userLocation!) < distanceThreshold {
point.title = "You are here"
} else {
point.title = "\(coordinate.latitude), \(coordinate.longitude)"
}

or you can set threshold to 0

Upvotes: 2

Related Questions