PDA

View Full Version : sorting data into NSMutableDictionary?




MACloop
Aug 2, 2010, 06:21 AM
Hello,
I have some objects with coordinates and those should be used to create annotations to be set on a map. The problem is that some of the objects have the same coordinate values, ie they are located on the same address.
I only want one pin for each address on the map, but I want the information for all objects associated with this coordinate, to be shown when the user clicks on the pin.

The starting point is that I have an array with all the objects and somehow I have to seperate and group them properly. The I have to create one annotation for every coordinate.

I think it might be good to use a NSMutableDictionary in this case but I am not sure how to structure this? Any ideas?

Thanks in advance!
MACloop



seepel
Aug 2, 2010, 05:14 PM
it's a bit simple, but I would just make arrays for each coordinate and put the objects in the arrays.

MACloop
Aug 3, 2010, 02:36 AM
it's a bit simple, but I would just make arrays for each coordinate and put the objects in the arrays.

Well, the simple soultion is often the best one ;-) How would you fill those arrays? I mean, without being for-looping for a very long time?

Perhaps you could give me an example how to go through two arrays. One with coordinates and one with objects each having a coordinate. If we have, say 5 coordinates and 50 objects. How must I devide those 5 coords into 5 arrays and then devide the array with objects into the "proper" coordinate array...

Thanks a lot for your help!
MACloop

robbieduncan
Aug 3, 2010, 03:31 AM
Let's assume the coordinates are 2-tuple of doubles. Then we can easily create a key for the object associated with the coordinate by turning these into a string. To ensure that two relatively close objects map to the same key we will limit the decimal places on the coordinates.


NSString *key = [NSString stringWithFormat:@"%5f-%5f",latitude,longitude]; // Note that you may need to tune the number of decimal places


Once we have our key simply store an NSMutableArray for each key in the NSMutableDictionary and add the objects to those arrays.

MACloop
Aug 3, 2010, 04:08 AM
Let's assume the coordinates are 2-tuple of doubles. Then we can easily create a key for the object associated with the coordinate by turning these into a string. To ensure that two relatively close objects map to the same key we will limit the decimal places on the coordinates.


NSString *key = [NSString stringWithFormat:@"%5f-%5f",latitude,longitude]; // Note that you may need to tune the number of decimal places


Once we have our key simply store an NSMutableArray for each key in the NSMutableDictionary and add the objects to those arrays.

Thanks for the answer!
I have implemeted somthing like that already and so far so good.
I create a dict and add all the coords as keys and an empty array as object for each key. Then I iterate the array with all the objects and also (this is perhaps not the fastest solution..?) iterate the dictionary in order to compare the key with the object's coordinate. I use the coords as string in both cases, ie the key is a sting and I compare the object coordinate like you suggested.

So now all the information is sorted ok, I think. The next step will be to make a custom annotationview. I want to show all object-names associated with one coordinate, when the pin on the map is clicked. As far as I see, the framework does not support very much altering for an annotation callout... do you have any good advice here? I found this and think it is interesting. I would like to add a button for each object associated with a specific coordinate. When the user clicks this button, I intend to show a detailed view for this object...

Again - thanks alot for the help!
MACloop