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 ca开发者_开发技巧n 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
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:
- Store data in MKAnnotation?
- how to store information related of a pin in MKPinAnnotation
- I have 30 annotations, and growing. Looking for a simpler way to code this?
精彩评论