9

I got the current location based on Longitude and latitude values and then I also got Multiple places on google map using Annotation now I want get longitude and latitude values based on Zip code I don't know how to get Longitude and latitude values base on Zip code

BenMorel
  • 34,448
  • 50
  • 182
  • 322

3 Answers3

18

this is the method for call google map api for geocoding, if u pass the Zipcode/Postal code, you get the result

-(void)ViewDidLoad
{ 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@",yourzipcodetext/PostalcodeText.text]]];

[request setHTTPMethod:@"POST"];

NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

NSLog(@"got response==%@", resSrt);

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];

NSString *lataddr=[[[[[dict objectForKey:@"results"] objectAtIndex:0]objectForKey:@"geometry"]objectForKey:@"location"]objectForKey:@"lat"];

NSString *longaddr=[[[[[dict objectForKey:@"results"] objectAtIndex:0]objectForKey:@"geometry"]objectForKey:@"location"]objectForKey:@"lng"];


NSLog(@"The resof latitude=%@", lataddr);

NSLog(@"The resof longitude=%@", longaddr);


}

Chioce-2

CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    [geoCoder geocodeAddressString:@"ZIP CODE HERE" completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        CLLocation *location = placemark.location;
        CLLocationCoordinate2D coordinate = location.coordinate;
        NSLog(@"Latitude %f", coordinate.latitude);
        NSLog(@"Longitude %f", coordinate.longitude);
    }];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • 1
    it is your zip code /Postal code text or string, you pass the value and then u get the response, are you understand – Anbu.Karthik Feb 06 '14 at 06:44
  • thk for replay i will try –  Feb 06 '14 at 06:46
  • NSString *Zipcodetext; and then set Zipcodetext.text but it's error(property text not found on object NSString ) and then How to pass the my zip code number like 534401 –  Feb 06 '14 at 07:01
  • gt create the NSString *zipcode=534401; k and replace the Zipcode to the Zipcodetext.text on the URL, k – Anbu.Karthik Feb 06 '14 at 07:06
  • Thank bro i got output thk bro –  Feb 06 '14 at 07:32
  • is this free API from google or there is any limit of usage or any kinda charges...?? – MPG Jul 24 '14 at 04:14
  • @MPG it is the open source /free api – Anbu.Karthik Jul 24 '14 at 05:09
  • @Anbu.Karthik : I want to fetch location/region based on Lat-Long value. How is it possible? – Jayprakash Dubey Nov 05 '14 at 09:19
  • @JayprakashDubey - can u give a example which type u need i give the answer to u – Anbu.Karthik Nov 05 '14 at 09:27
  • @Anbu.Karthik : Instead on appending postal code in request I want to append Lat-Long (say 19.23424, 34.12123) value and response should return particular region in city (say Andheri in Mumbai). Is this possible? – Jayprakash Dubey Nov 05 '14 at 09:41
  • @JayprakashDubey - it is possible if u pass directly http://maps.google.com/maps/api/geocode/json?sensor=false&address=Andheri,Mumbai u get the answer , – Anbu.Karthik Nov 05 '14 at 10:05
  • @Raja - it is based on response – Anbu.Karthik Apr 28 '17 at 12:00
  • @Anbu.Karthik. Okay. but i'm hitting same address. some time getting lat,lng. Some time getting NULL responce and some time getting HTML responce. how will i handle it. Please help me if you had faced this already HTML response: <![CDATA[http://203.122.12.242:2002/userportal/NSCCONTENT.do?requesturi=http%3a%2f%2fmaps%2egoogle%2ecom%2fmaps%2fapi%2fgeocode%2fjson%3fsensor%3dfalse%26address%3d610104&ip=10%2e0%2e1%2e134&mac=74%3a1b%3ab2%3a29%3a20%3a69&nas=aerovoyce&requestip=maps%2egoogle%2ecom&sc=9f491d49d008fc568408c965a3d3bcf0]]> – ssowri1 Apr 28 '17 at 12:55
  • Currently it doesn't work. Google API now requires an api key. – dpart Oct 09 '18 at 12:49
  • @dpart - thank you verymuch, I will update the answer based on the new one – Anbu.Karthik Oct 09 '18 at 12:58
12

It will be as simple as something like this:

CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    [geoCoder geocodeAddressString:@"ZIP CODE HERE" completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        CLLocation *location = placemark.location;
        CLLocationCoordinate2D coordinate = location.coordinate;
        NSLog(@"Latitude %f", coordinate.latitude);
        NSLog(@"Longitude %f", coordinate.longitude);
    }];
GenieWanted
  • 4,473
  • 4
  • 24
  • 35
  • thk for replay i got Latitude and longitude but it's showing different location plz help me thk in advanced –  Feb 06 '14 at 07:23
2

Apple released good documentation on forward geocoding that should help you.

Volker
  • 4,640
  • 1
  • 23
  • 31