0

Hee all,

At this moment i'm working on reading locations from a (valid) XML feed.

<locations>
    <location>
        <title>Supercoop</title>
        <category>supermarkt</category>
        <adres>Something</adres>
        <latitude>52.8982342</latitude>
        <longitude>5.2449449</longitude>
    </location>

    <location>
        <title>Thuis</title>
        <category>home</category>
        <adres>Something</adres>
        <latitude>52.223482</latitude>
        <longitude>5.248282</longitude>
    </location>
</locations>

"No real data in this example"

I use TouchXML for the parsing and that works fine.

My question is: what is the preferred way of processing this information to a MKAnnotation.

I thought about making an object, for example: MyLocation and store it there, but than i have to walk trough lots of MyLocation objects.

Secondly i want that object / annotation be accessible when i use the "calloutAccessoryControlTapped" method so i can access more information than is stored in the annotation when it goes to a detailview for example.

I hope and think you guys can help me out! Thanks!

Rolf

Rolf Koenders
  • 439
  • 3
  • 8

1 Answers1

0

Not sure what you mean by "have to walk through lots of MyLocation objects" but one way or the other, you have to create an object that conforms to the MKAnnotation protocol and add it to the map.

You can either immediately call addAnnotation as you parse each location or, probably better, add the annotations to an NSMutableArray and then call addAnnotations to add them to the map in one shot.

If you don't want to create your own class and you are using iOS 4+, you can use the pre-defined MKPointAnnotation class. That gives you the properties title, subtitle, and coordinate so you'd have to squeeze your title, category, and adres in there somehow.

It's not hard to create your own class that implements MKAnnotation and add your custom properties. This is better than trying to cram your properties into MKPointAnnotation. However, do not use the example provided in the MapCallouts sample app (it implies you have to create a separate class for each unique coordinate). Instead, create a settable coordinate property.

In the calloutAccessoryControlTapped method, the annotation object is accessible using view.annotation and then you can cast it to your custom class and access the properties easily.

For some sample code, try these other answers:

Community
  • 1
  • 1
  • Alright thanks. I have created an MyLocation object. And in my loop i create an object and add that to the map. I have the following method in my object: "- (id)initWithTitle:(NSString *)Title subTitle:(NSString *)subTitle categorie:(NSString *)cat coordinate:(CLLocationCoordinate2D)coord" But i want to store more data in that object for later usage outside of the loop. And the annotation needs to have a different image wich i locate by the categorie added to the object. – Rolf Koenders Jul 21 '11 at 15:03
  • For the image i can use this delegate method: "- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation" i guess? But than i need an if for every kind of annotation? – Rolf Koenders Jul 21 '11 at 15:14
  • It depends on what the logic is for your images. You can either add an image property to your annotation object and set it when you create the annotation and just set annView.image to it in viewForAnnotation or you can determine what image to use in viewForAnnotation using conditionals based on other annotation properties like title, category, id#, etc. –  Jul 21 '11 at 15:19
  • I use this to create an location object and add it to the map. http://cl.ly/2j3f2k2Q1A1T0E3E0h2k i can't set an image there i guess? in the viewForAnnotation is it going to be to much if this than that image.. – Rolf Koenders Jul 21 '11 at 15:25
  • I added an int property to my object with an imgId. id 1 = home, 2 = supermarket etc.. But where is the best way of adding this image? in the viewForAnnotation? – Rolf Koenders Jul 21 '11 at 15:40
  • It really depends on whether your images can be determined at annotation creation time or if they're dynamic (they change after annotation is added to map). If dynamic, you have to set in viewForAnnotation. Otherwise, you can set in either place. In viewForAnnotation, still make sure you handle and account for view re-use properly (set image and annotation properties if view is being re-used). –  Jul 21 '11 at 15:40
  • If your images have to be downloaded (they're not resources in your project), then maybe the MyLocation object can start an asynchronous download on creation and set image property in MyLocation when done and in viewForAnnotation it can just use MyLocation.image. If it's still nil when viewForAnnotation gets called, then maybe it could use a default image showing "loading...". –  Jul 21 '11 at 15:42
  • They are not dynamic, dont have to be downloaden, they dont change, its just an image for a specific location. But i dont get your "you can set in either place" i cant set the imageProperty of an annotation when i do AddAnnotation as far as i know? – Rolf Koenders Jul 21 '11 at 15:43
  • You can add a UIImage *image property to your MyLocation class and set it when you create a MyLocation. Then in viewForAnnotation, you can do `annView.image = ((MyLocation *)annotation).image;`. –  Jul 21 '11 at 15:46
  • Or probably simpler in your case is to just put all the logic in viewForAnnotation. Use a switch statement based on the imgId. –  Jul 21 '11 at 15:50