MKMapitem's Placemark.name is nil when creating MKMapItem from MKPlacemark

655 views Asked by At
MKPlacemark *placemark1 = [[MKPlacemark alloc] initWithPlacemark:mapItem1.placemark];

MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark1];

NSLog(@"placemark1.name is - %@", placemark1.name)

placemark1.name is - Starbucks

NSLog(@"mapItem1's placemark.name is - %@", mapItem1.placemark.name) 

mapItem1's placemark.name is - Starbucks

NSLog(@"item's placemark.name is - %@", item.placemark.name) 

item's placemark.name is - nil

mapItem1 has the proper value. mapItem1.placemark.name is @"Startbucks".

When I implement like the above, item.placemark.name is nil. But placemark1.name is @"Sartbucks".

When I made MKMapItem by another MKPlacemark, it also place mark.name in this MKMapItem is nil.

I don't know why these result has returned.

I think the same value of original placemark has to be returned.

2

There are 2 answers

0
Alex Blair On

I'm assuming that mapItem1 is of type MKMapItem? If this is true, you don't need to initialize a new MKPlacemark object(*placemark1), nor would you need to store placemark1 in a new MKMapItem object(*item).

Since you're calling placemark on mapItem1 in your code (mapItem1.placemark) I think I'm right by assuming it is of type MKMapItem.

To sum it up you don't need these lines of code at all:

MKPlacemark *placemark1 = [[MKPlacemark alloc] initWithPlacemark:mapItem1.placemark];

MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark1];

Your MKMapItem *mapItem1 will work as is. Does that make sense? You're kind of reinventing the wheel here. Just use mapItem1.placemark wherever it is you need to use it. I've been working on a project that uses mapkit, so if you have any questions I'd be happy to answer them.

0
Hanyu.Chen On

I notice if you assign the name of MapItem, the placemark would show the name.

extension MKPlacemark {
    var toMapItem: MKMapItem {
        let item = MKMapItem(placemark: self)
        item.name = name
        return item
    }
}