MKMapView带地址

时间:2010-08-01 18:41:27

标签: iphone mkmapview

有没有办法让MKMapView放置一个具有给定地址的引脚?不使用坐标

由于

5 个答案:

答案 0 :(得分:21)

这是我在应用程序中使用的一段代码

-(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr {
    NSString *urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                           [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr]];
    NSArray *items = [locationStr componentsSeparatedByString:@","];

    double lat = 0.0;
    double lon = 0.0;

    if([items count] >= 4 && [[items objectAtIndex:0] isEqualToString:@"200"]) {
        lat = [[items objectAtIndex:2] doubleValue];
        lon = [[items objectAtIndex:3] doubleValue];
    }
    else {
        NSLog(@"Address, %@ not found: Error %@",addressStr, [items objectAtIndex:0]);
    }
    CLLocationCoordinate2D location;
    location.latitude = lat;
    location.longitude = lon;

    return location;
}

答案 1 :(得分:5)

  

这是我在应用程序中使用的一段代码

[NSString stringWithContentsOfURL:]目前已弃用。你必须使用:

NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr] encoding:NSUTF8StringEncoding error:nil];

答案 2 :(得分:5)

由于此结果位于您进行Google搜索时的第一页,因此我认为提供比Google地理编码(限制版)更新鲜的解决方案更为出色。使用Apple地理编码器会更好:

NSString *location = @"your address";

CLGeocoder *geocoder = [CLGeocoder new];
[geocoder geocodeAddressString:location
             completionHandler:^(NSArray* placemarks, NSError* error){
                 if (error) {
                     NSLog(@"%@", error);
                 } else if ([placemarks count]) {
                     CLPlacemark *topResult = [placemarks firstObject];
                     MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];


                     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(placemark.coordinate, 5000, 5000);

                     [self.mapView setRegion:region animated:YES];
                     [self.mapView addAnnotation:placemark];
                 }
             }];

答案 3 :(得分:2)

由于Google向其地理编码API (upgrade notes)发布了新版本,因此@Aaron Saunders提供的Google地图网址不再有效。此外,响应现在为jsonxml,因此我在接受的响应中进行了一些更改以解决此问题。

- (CLLocationCoordinate2D)locationFromAddressString:(NSString*)addressString {

    if (!addressString.length) {
        ALog(@"provided an empty 'addressStr'");
        return CLLocationCoordinate2DMake(0.0, 0.0);
    }

    NSString *urlStr = [NSString stringWithFormat:GOOGLE_MAPS_GEOCODING_URL_STR,
                    [addressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSError *dataError;
    NSError *jsonError;
    NSData *responseData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]
                                             options:NSDataReadingMappedAlways
                                               error:&dataError];
    NSDictionary *responseBody = [NSJSONSerialization JSONObjectWithData:responseData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&jsonError];

    if (!dataError && !jsonError && [responseBody[@"status"] isEqualToString:@"OK"]) {
        NSDictionary *coords = responseBody[@"results"][0][@"geometry"][@"location"];
        return CLLocationCoordinate2DMake([coords[@"lat"] doubleValue],
                                      [coords[@"lng"] doubleValue]);
    }
    else {
        ALog(@"Address [%@] not found. \nResponse [%@]. \nError [%@]",addressString, responseBody, jsonError);
        return CLLocationCoordinate2DMake(0.0, 0.0);
    }
}

答案 4 :(得分:0)

您需要使用地理编码服务将地址转换为坐标。我认为谷歌提供了一个,以及其他一些服务。