zoom in, zoom out on mkmapview shows the hidden mkannotations - iphone

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.

Related

Multiple Annotations ( MKAnnotationView ) in the same location Coordinates

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.

Custom MKAnnotationView - How to capture touches and NOT dismiss the callout?

I have a custom MKAnnotationView subclass. It is showing the view exactly as I want it to. In that view, I have a button. I want to capture events on the button to perform an action. This works just fine.
However, I do NOT want the callout to be dismissed or disappear. Basically, touching the button in the callout will start playing a sound, but I want to leave the annotation up so the user can press stop if they want to, without having to touch the map pin again to bring the annotation back up. In another instance, I want the button touch to animate more details in the callout, so I definitely don't want to dismiss the callout at that point.
How can I keep the callout from disappearing whenever the user selects the callout or a button inside the callout?
This may not be the best solution, but it definitely works. First off, I tried a number of things, like observing for context and such, but I never got past crashing, and it seemed cumbersome. So, this is what I did:
I first specified what the controlling factor was for keeping an alert viewable. In my case, I created a custom annotation view, and whenever the user clicks a button on that custom view, I want it to stay visible, and maybe even change the content. So, I set a delegate on that custom view so that my map can know when something changes. In my map view controller, I catch that message and set a class member variable to true to signify that I want the annotation view to stay.
NOTE: This will happen before the selection messages occur.
Now, in my didDeselectAnnotation method, I check the boolean value. If I want to keep it visible, I opt to NOT remove my annotation, I reset the boolean value, and I re-select the annotation manually, setting animation to NO. This lets the annotation view "stay" visible--maybe a cheat, but the user can't see the difference. Whenever that boolean value says that deselection is ok, I simply remove the annotation and all is well.
So, the workflow is this:
Touch pin
CustomAnnotationView is displayed
User clicks a button on CustomAnnotationView, which notifies the delegate (mapView) that the
action occurred
Set the class boolean value to know that you want to keep the annotation around
mapView then calls didDeselectAnnotation method
In didDeselectAnnotation method, use conditional to decide if you should remove the annotation, or keep it around by not removing the annotation and manually re-selecting it without animation.
I hope this helps others. It took me a while to figure this out, so I hope it saves you time.
If you find a better solution, by all means, please post it here!
you can suppress callout closing by using hittest, check this following post.
Detect tap on title of callout

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.

iPhone SDK: Adding text to a MKMapView

I just want to add some text to a MKMapView (dynamically in code). It seems like one way to go would be subclassing MKShape and defining a custom annotation (i.e. one without a pin or any other graphics) but I'm uncertain how to do it. Also, it seems like it might be possible to layer another control on top of the MKMapView that would allow me to add text.
Why not just add a UILabel as a subview of the map?

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?