你可以从一个地址在Mapview中创建一个注释吗?

时间:2012-11-26 00:11:59

标签: iphone ios objective-c xcode mkmapviewdelegate

我今天几次“搜索”了这个问题而没有运气。我想知道是否可以从地址而不是使用lat / Long方法在Mapview中创建注释。我目前的代码如下。

//1. Create a coordinate for use with the annotation
    CLLocationCoordinate2D Sharon;
    Sharon.latitude = 38.952223;
    Sharon.longitude = -77.193646;

    Annotation *myAnnotation = [Annotation alloc];
    myAnnotation.coordinate = Sharon;
    myAnnotation.title = @"Sharon Lodge #327";
    myAnnotation.subtitle = @"McLean, VA";

2 个答案:

答案 0 :(得分:1)

试试这个,

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

             ];

用于注释标题和副标题,

CLPlacemark *topResult = [placemarks objectAtIndex:0];

// Create an MLPlacemark

MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

// Create an editable PointAnnotation, using placemark's coordinates, and set your own title/subtitle
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = @"Sample Location";
point.subtitle = @"Sample Subtitle";


// Set your region using placemark (not point)          
MKCoordinateRegion region = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;

// Add point (not placemark) to the mapView                                              
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:point];

// Select the PointAnnotation programatically
[self.mapView selectAnnotation:point animated:NO];

答案 1 :(得分:1)

您可以使用 google api CLGeocoder

从地址获取长途

google api

Link 1 Link 2

CLGeocoder

-(id)getAnnotationAryFromAddress
{

        CLGeocoder *geocoder2 = [[CLGeocoder alloc] init];

    NSString *addressString = "string contain address, city, state and zip";

    [geocoder2 geocodeAddressString:addressString
                  completionHandler:^(NSArray *placemarks, NSError *error) {

                      if (error) {
                          //NSLog(@"Geocode failed with error: %@", error);
                          return;
                      }

                      if (placemarks && placemarks.count > 0)
                      {                          
                          CLPlacemark *placemark = [placemarks objectAtIndex:0];

                          CLLocation *location = placemark.location;

                          RegionAnnotation *annotation = [[RegionAnnotation alloc] init];
                          CLLocationCoordinate2D theCoordinate1;
                          theCoordinate1 = location.coordinate;

                          annotation.title = @"Title";

                          annotation.subtitle = @"SubTitle";

                          annotation.tag = i;

                          annotation.coordinate = theCoordinate1;

                          [self.mapView addAnnotation:annotation];
                      }
                  }];
    }


    return self;
}