Can you create an annotation in Mapview from an address?

你。 提交于 2019-12-03 23:12:55

Try like this,

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

             ];

for annotation title and subtitle,

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

You can get lat long from an address using google api or 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;
}  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!