Putting a CLPlacemark on Map in iOS 5
Asked Answered
S

3

10

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 tried to put the forward geocoded CLPlacemark object on a MKMapView? I have a CLPlacemark object after geocoding but have no clue how to put it on a map.

I would appreciate any type of help. Google is of no help so far.

Spirograph answered 27/11, 2011 at 3:49 Comment(0)
S
24

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];
                 }];
Spirograph answered 27/11, 2011 at 21:12 Comment(0)
M
9

A CLPlacemark doesn't implement the MKAnnotation protocol so you still have to create your own annotation class or you can use MKPointAnnotation. The coordinates of the placemark are in its location property.

For example:

MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = placemark.location.coordinate;
pa.title = ABCreateStringWithAddressDictionary(placemark.addressDictionary, YES);
[mapView addAnnotation:pa];
[pa release];  //remove if using ARC

You can set the title to anything you want from the placemark but one possibility, as shown in the example, is to use the Address Book UI framework to generate an address string from the address dictionary provided by the placemark.

Mott answered 27/11, 2011 at 4:35 Comment(4)
Thank you! That was superbly helpful!Spirograph
I also found another way of doing this via video from this year (2011) WWDC. Just a fyi.Spirograph
I've been seeing weird tiny little memory leaks when using ABCreateStringWithAddressDictionary...2 leaks each occurance at 192bytes total. Has anyone else been having this issue?Logistician
@Logistician Running Analyze in Xcode cites ABCreateStringWithAddressDictionary as being a source of a potential leak, so there's definitely something weird going on with it.Exercise
R
0

in swift 3.0

 let pm = placemarks! as [CLPlacemark] // this is Clpmacemark
 let placemark = MKPlacemark.init(placemark: pm)
 self.mapview.addAnnotation(placemark)
Riven answered 8/9, 2017 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.