Multiple Annotations ( MKAnnotationView ) in the same location Coordinates - iphone

Hi I am trying to implement Annotations grouping and animation if the user touch.
I did look a the different cluster Library solution but this wont work for my because I have multiple Annotations in the same Coordinates.
So this is what I want to do
1- I identify where there are multiple Annotations in the same place (Coordinates)?
how can implement this ? find the annotations in the same coordinates and make a new group annotation ?
2- change the colour of that Annotation (to let the user know)
I can do this on this method
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation;
3- if the user touch the annotations in that group , annotations will move in a circle around their location.
So far I have all my annotation on the map some have a very dark shadow.
I want to focus in the first task now - grouping
I am not asking for code example I can do that I just need help figuring out what methods to use to implement the task .
Thanks for your help.

I ended up implementing this code from StormID
Handling MKMapView Annotation Pins on the Same Coordinate
I have to make some changes but it works great.

To get rid of the dark shadow, which is caused by stacked annotation views, I would change your mapView:viewForAnnotation: to recognize when the passed annotation has the same coordinates as another and only return a view (of a different color) for one of those annotations.
Then, for your animation, upon touch of the grouped annotation, I would set mapView.scrollEnabled = mapView.zoomEnabled = NO temporarily for simplicity's sake, hide the group annotation, and create an overlay with animations of the individual annotations yourself. Upon another touch, animate them back to the center, remove them and the overlay, and unhide the grouped annotation and re-enable the map.

Related

How to place a view next to an MKRoute in a MapKit app

I have an app that is using MapKit's MKRoute. I want to place a UIView on the route on the map, similar to how Apple annotates routes in Maps with an estimated travel time popover:
http://www.siprog2.com/pjx/fichiers/mkroute.jpg
You can use MKAnnotation to annotate a specific coordinate point on your route. Your MKMapView's delegate will then be asked to return an MKAnnotationView if your annotation is in the currently visible area of the map.
Apple has sample code for adding custom annotation views to a map (it's not using a route, but all you need to change is annotate a point on your route).

How to designed a correct MVC on iOS?

I have a project where I want to display overlays to locate people on a map. I have difficulties to follow the MVC pattern since I've never practice it.
In a database, I'll save a position (in map coordinates) and the image to identify one person (the image of the overlay).
If 2 or more are at the same position, they are grouped under another "group" overlay. When an overlay is touched, I want to display different information, depending of its type ("group" or "single").
Here is my problem : I obviously need to subclass an Overlay superclass, which have the properties position and image, to redefined the overlayTouched: method. But this code his typically a View code of the MVC pattern, but the overlay is a Model object since it will be saved, so i'm confused.
How should I designed this ?
You should not subclass Overlay to add touch events to it, instead create a new UIView, YourNewView
This YourNewView will "has a" instance of Overlay, and it will delegate all its method that are required from the model to Overlay
For example. if you have a touchesEnded in your YourNewView and you want it to save the overlay to the DB, you would do
//in touchesEnded
[self.overlay saveToDb];
And so on, all the calls will be forwarded to the Overlay Class

Change Custom annotation Label without removing it from Map in iPhone

I am diplaying a Custom annotation in the center of the map. Which continues need to update its label how far is it from the center of the map.
P.S. Center of the map means the centre of the visible region in the map.
So by default my annotation is in the center of the map with label 0 then when I move the map it must update itself.
Currently on each move I am removing the annotation and adding the annotation with updated text. But its not the correct way..
Kindly help.... Thanks in advance....
I'd add a UIView with the label on it on the view that contains map right after adding map view. The size of this new view should be the same to size of the map. The tricky part is handling touches on it and then transferring them to the map view. You can dig into this tutorial. It is not map view but web view, nevertheless principles are the same.
If you choose that way you don't have to add this label as an annotation.

zoom in, zoom out on mkmapview shows the hidden mkannotations

i have a strange problem on my mapview.
I have filtering option on my map to show and hide some annotations.
using
[[self.gmapViewController.gMap viewForAnnotation:annot] setHidden:YES]; or
[[self.gmapViewController.gMap viewForAnnotation:annot] setHidden:NO];
and i center the map to a particular location when i filter the mkmpaview.
but the issue is ... when i try to zoom in or zoom out, the hidden annotations are getting displayed on the map.
how to avoid it doing that.
Could any one ..let me know the solution please...
Why cant you remove the annotations and then again add annotations where you want instead of hidding and showing.
You can't rely on specific annotation views to continue to represent the same annotation as the map view's boundaries change; it reuses them, much as a UITableView reuses its cells. What you need to do is change some property of annot itself as well as changing the hidden state of the annotation view that currently represents it, then check for that property and set the appropriate hidden state in your MKMapViewDelegate -mapView:viewForAnnotation: method as well.

MKAnnotationView in both hovering and pinned states

I'm trying to add a pin (MKAnnotation and MKAnnotationView) to my MKMapView and allow the user to drag it around.
I'd also like to make the dragging of the pin animated and interactive like the iPhone's Map App.
My question is how do I change the state of the MKAnnoationView so that it's hovering over the map (so the pin isn't actually inside the map)?
I'm not 100% sure how to do this.
At present, my colleague as found an hovering image that he swaps with the default MKAnnotationView, but that means I can't easily animate between the two.
Not sure what you exactly want to do but I have used Apple's example in the iPhone App Programming Guide (Handling Events in an Annotation View) to implement the draggable pin.
It has a partial code but tha may be enough for you to figure it out.
Basically, you must subclass the MKAnnnotation and MKPinAnnotationView and in your CustomAnnotationView class you have to implement delegate methods to handle touch events, as shown in the Apple example.
There was a bit of filling out or modification needed because the code snippet was not complete, but I have reproduced the behaviour of the pin on the Apple's iPhone Map app exactly (except that I did not implement the right accessory button).
In it, the pin feels like it is hovering. So, I suspect that you have no need for the hovering image you have mentioned.
I also presume that by providing a BOOL property, you could make the pin draggable or "fixed" programmatically.
Does this help?