如何在iPhone中从地址中找到纬度和经度

时间:2011-07-06 11:57:25

标签: iphone mapkit

我正在制作一个我正在获取地址的应用程序,我需要从该地址中找到纬度和经度并在地图中显示该位置

- (void)viewDidLoad 
{
    [super viewDidLoad];

    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    mapView.showsUserLocation = YES;
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 

    geoCoder.delegate = self;
    [geoCoder start];

    double latitude = 0, longitude = 0;

    region.center.latitude = latitude;   
    region.center.longitude = longitude;  
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [mapView setRegion:region animated:YES]; 

    [mapView setDelegate:self];

    DisplayMap *ann = [[DisplayMap alloc] init]; 
    ann.title = @"BarRestaurant";
    ann.subtitle = @"..."; 
    ann.coordinate = region.center; 
    [mapView addAnnotation:ann];
}

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {

    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                          initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinView.pinColor = MKPinAnnotationColorRed; 
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    } 
    else 
    {
        [mapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
    appRestaurantAppDelegate *delegate = [[UIApplication sharedApplication] delegate];

    double latitude = 0, longitude = 0;

    address = delegate.streetName;

    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;
}

然而我无法找到纬度和经度,请帮助我在代理中我做错了我从网络服务器获取地址

2 个答案:

答案 0 :(得分:6)

试试这段代码,

- (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;
}

另请参阅此link

答案 1 :(得分:1)

在ios 5.0之后,谷歌地图和谷歌地图api不应该在原生地图视图中使用。它将违反谷歌和苹果政策。

但是我们可以从apple提供的mapkit框架本身找到位置。

请参阅以下链接。 https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html

使用&#34; CLGeocoder &#34;非常简单喜欢:

geocoder = [[CLGeocoder alloc] init];    

[geocoder geocodeAddressString:@"1 Infinite Loop"
 completionHandler:^(NSArray* placemarks, NSError* error){
     for (CLPlacemark* aPlacemark in placemarks)
     {
         // Process the placemark.
     }
}];