0

In my application I want the exact lattitude and longitude of given address using forward geocoding in IOS in Objective-C.

I had used forward geocoding in my application but it is not giving the exact address. this is my code of forward geocoding

-(void)detectlocation:(NSString*)address
{
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
    [geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error)
     {
         if(!error)
         {
             CLPlacemark *placemark = [placemarks objectAtIndex:0];

             NSLog(@"%f",placemark.location.coordinate.latitude);
             NSLog(@"%f",placemark.location.coordinate.longitude);
             self.latitude4=placemark.location.coordinate.latitude;
             self.longitude4=placemark.location.coordinate.longitude;
             NSLog(@"%@",[NSString stringWithFormat:@"%@",[placemark description]]);
         }
    }];
}

Thanks In Advance

Himanth
  • 2,381
  • 3
  • 28
  • 41

2 Answers2

0

You can access placemark properties for a more accurate location.

This is swift, but the same is for objective-c

Placemark Options

Pedro Pinho
  • 652
  • 3
  • 6
  • your comment is not relevant for the answer. In the screenshot it has what he can use and description, so it's better than code in this case. – Pedro Pinho Jul 18 '17 at 08:22
  • No, it's relevant. Because images can die (dead link), while it's more difficult for text. Also, it's easier for research. – Larme Jul 18 '17 at 08:24
0

Declare and initiate your CLLocationMager* inside the viewDidLoad method.

I recommend to use:

@property YourCustomLocation* foundedLocation;

in order to "save" data of the place that has been found.

Then try this inside your method:

[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
            
            //if 1+ "places" have been found:
            if ([placemarks count] > 0) {
                
                //save the first place that has been found
                CLPlacemark *placemark = [placemarks objectAtIndex:0];
                //save location from the placemark
                CLLocation *location = placemark.location;
                //save coordinates from location
                CLLocationCoordinate2D coordinate = location.coordinate;
//Do more stuffs...like:
self->_foundedLocation = [[YouCustomLocation alloc]initLocationWithLatitude:coordinate.latitude Longitude:coordinate.longitude];}
                    }];

Remember that forward-geocoding works with blocks, so unless you use __block directive near the variable you won't be able to "save the location" on a variable declared outside the block.

DaniBatta
  • 1
  • 2