2

I get the results of a MKLocalSearch and it includes something like...

{
    address =     {
        formattedAddressLine =         (
            "Marton Road",
            Middlesbrough,
            TS1,
            England
        );
        structuredAddress =         {
            administrativeArea = England;
            areaOfInterest =             (
                "Great Britain"
            );
            country = "United Kingdom";
            countryCode = GB;
            fullThoroughfare = "Marton Road";
            geoId =             (
            );
            locality = Middlesbrough;
            postCode = TS1;
            subAdministrativeArea = Middlesbrough;
            thoroughfare = "Marton Road";
        };
    };
    addressGeocodeAccuracy = 0;
    business =     (
                {
            UID = 9301704419119613323;
            URL = "http://www.cineworld.co.uk";
            attribution =             (
                                {
                    attributionURLs =                     (
                        "yelp5.3:///biz/cineworld-middlesbrough",
                        "yelp4:///biz/cineworld-middlesbrough",
                        "yelp:///biz/cineworld-middlesbrough",
                        "http://yelp.com/biz/cineworld-middlesbrough"
                    );
                    sourceIdentifier = "com.yelp";
                    sourceVersion = 1;
                }
            );
            canBeCorrectedByBusinessOwner = 1;
            name = Cineworld;
            source =             (
                                {
                    "source_id" = "b2LOPag6ha6845__dgXehw";
                    "source_name" = yelp;
                },
                                {
                    "source_id" = 6670;
                    "source_name" = tribune;
                },
                                {
                    "source_id" = 2000000103009680;
                    "source_name" = "acxiom_intl";
                },
                                {
                    "source_id" = "cineworld-middlesbrough";
                    "source_name" = "yelp_alias";
                }
            );
            "star_rating" =             (
                0
            );
            telephone = "+448712002000";
        }
    );
    center =     {
        lat = "54.57633773904653";
        lng = "-1.228197113614671";
    };
    inputLanguage = en;
    localSearchProviderID = 9902;
    mapRegion =     {
        eastLng = "-1.224891596539819";
        northLat = "54.57545000290778";
        southLat = "54.5738619816233";
        westLng = "-1.227631256834202";
    };
    name = Cineworld;
    type = 57;
}

Now when I add it to my map with...

id <MKAnnotation> annotation = mapItem.placemark;

[self.mapView addAnnotation:annotation];

It adds a pin that, when I tap on it shows "Marton Road" but I'd like it to show "Cineworld".

I'm finding it really hard to find any info on getting stuff out of the MKMapItem and into the MKPlacemark though.

If I try to use mapItem.name in the place mark then they all show "United States".

Any idea how I can get some more useful information out of this?

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • This is likely an annotation reuse issue. Can you show the code you use in - (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id )annotation {}? – Mike Aug 27 '14 at 17:04
  • I'm not using that. Literally the two lines above are all I use to put the pins on the map. – Fogmeister Aug 27 '14 at 19:17
  • I would look into that more then, like UITableViewCells, map view's reuse annotations and any appeared duplication/incorrect information is usually caused by reuse problems. – Mike Aug 27 '14 at 19:18
  • On second thought, you're only using one annotation? Do an NSLog on the mapItem object before you add the annotation and share what the result is. Are you sure the above information you showed is what you're actually getting? – Mike Aug 27 '14 at 19:20
  • 2
    The MKPlacemark returns the address for its `title` property and the MKPlacemark class doesn't let you set the `title` yourself. What you can do is create an MKPointAnnotation (which has a settable `title` property) and set its title to whatever you want like mapItem.name. See http://stackoverflow.com/questions/21617414/create-a-title-for-annotation-from-mklocalsearch. –  Aug 27 '14 at 19:26
  • @Mike the code in the question is an NSLog of the MKMapItem. Thanks – Fogmeister Aug 27 '14 at 19:27
  • @Anna thanks. I'll give that a try in the morning :-) – Fogmeister Aug 27 '14 at 19:28
  • @Anna, I agree this is likely the issue. Nice catch. – Mike Aug 27 '14 at 19:29
  • @Anna awesome! Just tried it and it works perfectly :D Thanks. If you want to add it as an answer I'll accept :) – Fogmeister Aug 28 '14 at 06:58

1 Answers1

2

The MKPlacemark returns the address for its title property and the MKPlacemark class doesn't let you set the title yourself.

What you can do is create an MKPointAnnotation (which has a settable title property) and set its title to whatever you want like mapItem.name.

For example:

MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.title = mapItem.name;
pa.coordinate = mapItem.placemark.coordinate;
[self.mapView addAnnotation:pa];


Note:
You don't have to use the MKPointAnnotation class (it's just the most convenient available).
You can also use a custom class that conforms to MKAnnotation and that has a settable title property (or some class that returns the MKMapItem's name as its title).

Also note that if you want to be able to access the related MKMapItem or MKPlacemark in the annotation you add after-the-fact (eg. in the map view delegate methods), you'll need to use a custom class instead of MKPointAnnotation where you add a "sourceMapItem" or "sourcePlacemark" property that you can set when creating the annotation.

This way, you can set the title as needed but still access all the original MKMapItem or MKPlacemark values from which the annotation object was created (you couldn't do this easily if you use just MKPointAnnotation since it doesn't keep a reference to the source map item or placemark).