/// Required Framework
1. CoreLocation and 2. MapKit.
Import this two file where you use bellow method
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
/// This bellow code work in < 5.0
CLGeocoder* gc = [[CLGeocoder alloc] init];
[gc geocodeAddressString:@"Set Your Location Name" completionHandler:^(NSArray *placemarks, NSError *error)
{
if ([placemarks count]>0)
{
// get the first one
CLPlacemark* mark = (CLPlacemark*)[placemarks objectAtIndex:0];
double lat = mark.location.coordinate.latitude;
double lng = mark.location.coordinate.longitude;
CLLocationCoordinate2D objCo;
objCo.latitude = lat;
objCo.longitude = lng;
MKCoordinateRegion region;
region.center = objCo;
region.span.latitudeDelta = 0.03;
region.span.longitudeDelta = 0.03;
[objMapView setRegion:region animated:YES];// set region on your mapView
MyAnnotation *ann = [[MyAnnotation alloc] init];// i just create this Annonation object for get and set location detail
ann.title = strLocation;
ann.subtitle = strDetailDesc;
ann.anntag=11111;
ann.coordinate = objCo;
[objMapView addAnnotation:ann];
// MKCoordinateRegion viewregion = MKCoordinateRegionMakeWithDistance(objCo, 0.03 , 0.03);
// [self.objMapView setRegion:viewregion animated:YES];
}
}];
/// This is working in any ios
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address{
double latitude = 0, longitude = 0;
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
return center;
}
=> In this above method if your location not found then you get lat-long 0.0000
you can use above method it like bellow...
CLLocationCoordinate2D objLocaton =[appDelegate geoCodeUsingAddress:@"ahmedabad"];
NSLog(@"Location:%f && %f",objLocaton.latitude, objLocaton.longitude);
No comments:
Post a Comment