convert NSArray to MKPlacemark

115 Views Asked by At

I have a forward geocode block. like this:

[_geoCoder geocodeAddressString:searchString completionHandler:^(NSArray *placemarks, NSError *error) {....}];

As is, the geocode data stores in the NSArray * placemarks. Now I would like to do some annotation to the map, The addAnnotation method requires a MKPlacemark, so how do I convert the place mark in NSArrayinto a MKPlacemark? Thanks.

1

There are 1 best solutions below

0
AudioBubble On

Here's what you want to do: Iterate through the parameters array - meaning, fetch each item from the array. As we're fetching the items, we want to create MKPlacemark objects using the data the items contain.

The Objective-C language gives us a special tool that allows us to iterate through the array - the "forin" loop:

for (CLPlacemark *placemark in placemarks)
{
// insert code here
}

Now, we want to create an MKPlacemark object from "placemark": (Note: An MKPlacemark object is a CLPlacemark object)

MKPlacemark *mkPlacemark = [MKPlacemark initWithCoordinate:(CLLocationCoordinate2D)coordinate 
                 addressDictionary:(NSDictionary<NSString *,id> *)addressDictionary;]