如何从地址字符串获取经纬度坐标

问题描述:

我有一个MKMapView,它的顶部有一个UISearchBar,我希望用户能够键入一个地址,并找到该地址并在其上放一个针脚.我不知道如何将地址字符串转换为经度和纬度,因此我可以创建一个CLLocation对象.有人知道我该怎么做吗?

I have a MKMapView that has a UISearchBar on the top, and I want the user to be able to type a address, and to find that address and drop a pin on it. What I don't know is how to turn the address string into longitude and latitude, so I can make a CLLocation object. Does anyone know how I can do this?

您可能会在此问题中找到答案.

You may find your answer in this question.

iOS-通过使用MKMapView位置注释地址,而不是经度/长由用户Romes.

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

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

A Very simple solution. But only applicable on iOS5.1 or later.