如何从地址字符串中获取lat和long坐标

Cha*_*lis 8 mapkit cllocation cllocationcoordinate2d

我有一个MKMapView,有一个UISearchBar在上面,我希望用户能够键入一个地址,并找到该地址并在其上放置一个图钉.我不知道的是如何将地址字符串转换为经度和纬度,所以我可以创建一个CLLocation对象.有谁知道我怎么做到这一点?

arj*_*lad 16

您可以在这个问题中找到答案.

iOS - MKMapView通过使用地址而不是lat/long来放置注释 用户罗马.

NSString *location = @"some address, state, and zip";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:location 
             completionHandler:^(NSArray* placemarks, NSError* error){
                 if (placemarks && placemarks.count > 0) {
                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                     MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

                     MKCoordinateRegion region = self.mapView.region;
                     region.center = placemark.region.center;
                     region.span.longitudeDelta /= 8.0;
                     region.span.latitudeDelta /= 8.0;

                     [self.mapView setRegion:region animated:YES];
                     [self.mapView addAnnotation:placemark];
                 }
             }
         ];
Run Code Online (Sandbox Code Playgroud)

非常简单的解决方案.但仅适用于iOS5.1或更高版本.