Putting a CLPlacemark on Map in iOS 5

后端 未结 3 1883
执念已碎
执念已碎 2021-02-14 10:18

In iOS 5, there is a new way to forward geocode address (converting address like 1 Infinite Loop, CA, USA to lat/lang address). More info on this is here.

Has anybody t

3条回答
  •  故里飘歌
    2021-02-14 11:11

    In addition to the selected answer, there is also this way for adding annotations to the mapView for forward geocoding. The CLPlacemark object can be directly cast to MKPlacemark and added to the mapview. Like this:

    MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:placemark];
    [self.mapView addAnnotation:placemark];
    

    Here is a full example of forward geocoding.

     NSString *address = @"1 Infinite Loop, CA, USA";
     CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:address 
                     completionHandler:^(NSArray* placemarks, NSError* error){
                         // Check for returned placemarks
                         if (placemarks && placemarks.count > 0) {
                             CLPlacemark *topResult = [placemarks objectAtIndex:0];
                             // Create a MLPlacemark and add it to the map view
                             MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                             [self.mapView addAnnotation:placemark];
                             [placemark release];
                         }
                         [geocoder release];
                     }];
    

提交回复
热议问题