Get latitude and longitude based on zip using Geocoder class in IOS

前端 未结 3 2090
温柔的废话
温柔的废话 2020-12-16 05:24

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

相关标签:
3条回答
  • 2020-12-16 05:47

    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);
        }];
    
    0 讨论(0)
  • 2020-12-16 05:54

    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);
        }];
    
    0 讨论(0)
  • 2020-12-16 05:56

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

    0 讨论(0)
提交回复
热议问题