MKMapView带地址

joh*_*hnz 13 iphone mkmapview

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

谢谢

Aar*_*ers 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;
}
Run Code Online (Sandbox Code Playgroud)


Ros*_*nko 5

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

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

NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr] encoding:NSUTF8StringEncoding error:nil];
Run Code Online (Sandbox Code Playgroud)


Cli*_*ize 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];
                 }
             }];
Run Code Online (Sandbox Code Playgroud)