pingpong
pingpong

Reputation: 1237

how can i get the location of a user from the iphone?

this is not a coding question, its just the concept im trying to achieve(alogirthm) if you like.

basically what i want to do is check if a user is in a particular place i.e the british meseum!! and then sending them a message saying welcome to the british meseum.

i only have three location in my DB

i know that i need to use real time checking! so i was thinking that i get the users location, check to see if it matches any three values, and then send message. can you suggest anything better? thanks

Upvotes: 1

Views: 116

Answers (2)

Robert
Robert

Reputation: 38213

Yea, that sounds like it would work. Just be sure to check the location matches within radius (the location will never be exactly the same) i.e.

if ( dist ( userCoordinate - museumCoordinate) < THRESHOLD ) ...

(use the distanceFromLocation: method To calculate this)

Also, to get the real time checking, you might want to use the delegate callback of the CLLocationManager to receive updates when the users location changes.

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

     // The location has changed to check if they are in one of your places
 }

Upvotes: 1

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

What you want to is called geocoding. Typically is done by sending a request to a service which actually does the conversion for you. Google and some other companies offer this service.

Below can be used as code reference

-(CLLocationCoordinate2D) addressLocation:(NSString *)input {

    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                           [input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
    NSArray *listItems = [locationString componentsSeparatedByString:@","];

    double latitude = 0.0;
    double longitude = 0.0;

    if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
        latitude = [[listItems objectAtIndex:2] doubleValue];
        longitude = [[listItems objectAtIndex:3] doubleValue];
    }
    else {
        //Show error
    }
    CLLocationCoordinate2D location;
    location.latitude = latitude;
    location.longitude = longitude;
    return location;
}

Upvotes: 1

Related Questions