hanumanDev
hanumanDev

Reputation: 6614

How to find current user location to use in Google Places API for iOS

I'm looking to use this Google Places API url to search in an iOS app. Instead of using the designated location I would like to use the user's current location:

NSURL *googlePlacesURL = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=spa&sensor=false&key=AIzaSyAgcp4KtiTYifDSkIqd4-1IPBHCsU0r2_I"];

Would it be a matter of storing the users current location lat/lon and then placing those values in the NSURL?

thanks for any help

Upvotes: 1

Views: 2217

Answers (1)

Ajay Singh Azad
Ajay Singh Azad

Reputation: 74

Your URl shows that you want to list place near by current location.

for that you need current location coordinates, radius, searched place name and key.

Do this:

Step 1: Add CoreLocation framework

Step 2: #import

Step 3: Add CLLocationManagerDelegate delegate

Step 4: Make CLLocationManager *locationManager;

Step 5: Initialise CLLocationManager ( usually in ViewDidLoad)

self.locationManager = [[CLLocationManager alloc] init];// autorelease];
// This is the most important property to set for the manager. It ultimately determines how the manager will
// attempt to acquire location and thus, the amount of power that will be consumed.
locationManager.desiredAccuracy = kCLLocationAccuracyBest ;
// Once configured, the location manager must be "started".
[locationManager startUpdatingLocation];   
locationManager.delegate = self;

Step 6: Write code for CLLocationManager

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    StrCurrentLongitude=[NSString stringWithFormat: @"%f", newLocation.coordinate.longitude]; // string value
    StrCurrentLatitude=[NSString stringWithFormat: @"%f", newLocation.coordinate.latitude];
    appDeleg.newlocation=newLocation;
    [locationManager stopUpdatingLocation]; // string Value
}

Step 7: Use coordinates and fire request :

[NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%@,%@&radius=%@&name=%@&sensor=false&key=YOU-API-KEY",StrCurrentLatitude,StrCurrentLongitude,appDeleg.strRadius,strSelectedCategory]

ENJOY

Upvotes: 3

Related Questions