Bobi
Bobi

Reputation: 477

How get user location using iOS geolocation framework

I am looking to make an "If" statement, whereby I'm using the user's geolocation to create an outcome. I do not know how to do this.

For example:

If (USERLOCATION = CANADA) {

//then do something here

}

Else {
//do something else
}

Upvotes: 0

Views: 1109

Answers (2)

jbouaziz
jbouaziz

Reputation: 1493

If you have the user location, you can use this code and match the country name with your. Although there might be a localization limitation.

- (void)reverseGeocode:(CLLocation *)location {
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

        if (!error) {
            CLPlacemark *placemark = [placemarks lastObject];
            NSLog(@"Country: %@", placemark.country);

        } else
            NSLog(@"Error %@", error.description);
    }];
}


Then use [placemark.country caseInsensitiveCompare:@"canada"] == NSOrderedSame to match with the country you're looking for.

Upvotes: 2

user189804
user189804

Reputation:

You'll need to find a third-party API that can give an answer about whether a latitude/longitude is in Canada, or you will need to define a bounding box that means "Canada" and test whether your location is inside it.

Defining "Canada" by coordinates might be a little bit tricky because it isn't a square though there are significant parts that are nice straight lines. This answer could help you define the box by supplying a list of bounding coordinates.

Upvotes: 0

Related Questions