iPhone - Updating Annotation subtitle in mapkit - iphone

I have a custom placemark with title and subtitle. The subtitle is actually displaying the address of the dropped pin using the reverse geocoder.
I have a button which has an action to drop the pin. This action gets the location coordinates of the user, and then calls [geocoder start] which gets the full address with Reverse Geocoder and generates the custom annotation and then calls [mapView addAnnotation:customPlacemark].
My problem is that using this sequence order, when there's no a WiFi connection (only 3G or maybe Edge) the pin takes a lot to drop because it's watigin to get the reverse geocoding info.
So basically I need to drop the pin without a subtitle and from the viewDidAnnotation call the geocoder and inside the reverseGeocoder update the subtitle but I'm not sure how to do that.
I want to display the annotation without the address details and update it when it gets the information from the reverse geocoder.
Any suggestions?
Thanks in advance

MKMapView observes changes its annotations via KVO. Therefore if you update your annotation's properties in a KVO compliant manner, it should Just Work.
For example, when the reverse geocoder returns an address for your annotation, you first announce the title and subtitle properties are about to change:
[self willChangeValueForKey:#"title"];
[self willChangeValueForKey:#"subtitle"];
Note that the above code is assumed to be in the annotation class.
Then update the annotation with information from the geocoder. When you are done:
[self didChangeValueForKey:#"subtitle"];
[self didChangeValueForKey:#"title"];
Note the order changed for didChangeValueForKey: as these need to be nested properly, somewhat like HTML tags.
This also works for the coordinate property, that will cause the pin to move.

I'd place the annotation, keep a reference to it in a property, then when your reverse geocoder calls back use the reference to the annotation and update its properties.

Related

Removing multiple annotations from MapView

In my app I have two functionality when my tableview loads ,in the mapview all the pins are shown corresponding to tableData.NOw I have search bar which gives another set of values which I have to show on my mapView.Everything is working fine but second time previous pins are also shown means prevoius annotations are not deleted .
So please tell me how to remove prevoius annotation so that I can show only relavent pins on mapView
Thanks in advance
Simply use the MKMapView instance method "removeAnnotations:":
-(void)removeAnnotations:(NSArray *)annotations
http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html
If you want to remove all of them you can do somethign like :
[mapView removeAnnotations:mapView.annotations];
P.S: Beware that you might want to iterate through the annotations not to remove the userLocation (blue dot)
Hope this helps,
Vincent
I got my answer my self:-
mapAddAnnotations is my arrayname and map is my mkmapview object.
[map removeAnnotations:self.mapAddAnnotations];

Why is the MKMapView's userLocation property rubbish ... for a while?

I have a Map View defined in IB and it is set to show the user location.
In my app, in -viewDidAppear, I query self.mapView.userLocation.location.coordinate and it comes back with insane values such as:
latitude: 4.8194501961644877e-49
longitude: 2.2993313035571993e-59
However, the next time -viewDidAppear is called (after I've simply moved to another tabbed view and then back to this one) the userLocation property holds exactly the correct values for my current location.
It seems that at the time of my initial call, the userLocation property has not been initialised but despite having read Apple's documentation I can't see any caveats where it says that this property is only valid after doing xxx.
Is there something that has to happen before userLocation is valid to use or should I just use CLLocationManager and ask it instead?
Thanks in advance for any help.
Sadly, Thomas' suggestion didn't help. What I have since discovered is:
If showsUserLocation is NO, then userLocation is never set correctly and -MapView:didUpdateUserLocation: is never called, consequently I never ever get a sensible location value.
So, to get the user's location I have to set showsUserLocation to YES, however that then means that after all my annotations have been added to the view (without including the user's location) I then calculate the required span to encompass them all and display them all at the right zoom level. After I do that though, the view jumps sideways as the Map View then automatically displays the user's location as the blue blob! As it was never included in the annotations to work out the zoom level I can't incorporate it into my calculations. Aaargh!
Note that when showsUserLocation is YES, then -MapView:didUpdateUserLocation: is called, but only after I've calculated all the coordinates of my annotations, not before!
I'm assuming it hasn't finished finding the user location - it has to work this out and it may take a while.
Instead of using it in viewDidLoad use THIS delegate method:
- (void)mapView:(MKMapView *)myMapView didUpdateUserLocation:(MKUserLocation *)userLocation;
You will need to set your mapview delegate to self. :)
Same is often true of Core Location. You'll get the last location lingering it its buffer, sometimes, or a super-broad throw-the-dart-at-the-map kind of location...
Best bet is to check the .horizontalAccuracy property of the location object and toss any that are too vague. It's good practice to just chuck the first one too.
for didUpdateUserLocation to be called you have to have...
mapView.showsUserLocation = TRUE;

MapKit custom annotations being added to map, but are not visible on the map

I have an interface with a mapView and UITableView. Data is loaded from a server, and the annotations are created and added to the map with
[mapView addAnnotation:truck]
the tableview is then populated using the array thats retured from
[mapView annotations]
once this process is completed, i check the number of annotations on the map with [[mapView annotations] count] called whenever i click on a cell in the table and its equal to the number it ought to be, so all the annotations are getting added onto the mapView, but for some reason I cant see any annotations in the simulator.
The images are named just as they are assigned in the custom AnnotationView, the loadAnnotation function is done properly, etc... i dont know what it could be but ive looked at the associate between the image file and wheres its loaded a hundred times to find a discrepancy, but it all looks fine.
One interesting point is that when i print and coordinate value after clicking on the cell (remember this data comes straight from [mapView annotations], it looks good... but for whatever reason the annotation view isnt being displayed.
so i suppose if i could have the answer to one question it would be, what are possible causes for a mapView to contain several annotations, but to not show any on the map?
Thanks
EDITED WITH IMPORTANT ADDITIONAL INFO
There appears to be a disconnect between what is being displayed on my map in the simulator and what im seeing in the mapView object. For example, when i select a row in the tableView, I am calling:
[mapView setRegion:MKCoordinateRegionMake([annotation coordinate], MKCoordinateSpanMake(.01, .01)) animated:YES];
and this has no effect on the map in the simulator. I have checked that the coordinate being passed is actually a valid coordinate, but it doesnt seem to matter because the map is being unresponsive. I thought it could be something wrong with my .xib, but its all connected properly. delegate is the outlet and mapView as a referencing outlet.
Does anyone now understand what may be happening?
This really looks like missing images.
Try replacing your custom annotations with MKPinAnnotations to test, and see if your annotations are visible then.
Have you implemented the following delegate function ?
(MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation;
This line:
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
was creating messing everything up. once i commented it out, the app began working fine.

MKMapView and CLLocationManager

I want to use a MKMapView to display the user current location using the default breathing blue pin and I want to record the user movement at the same time. Is there any way that I could use the GPS manager (not sure if this is a CLLocationManager) the MKMapView uses when we enabled it to show user location?
I know that I can create my own CLLocationManager. But this feels like adding an overhead to my application and I would like the map and my tracking to remain in sync.
I already explored the following ideas without success:
Use the [MKMapView showUserLocation:YES] and add KVO on the userLocation field. This does not work and I am wondering if this is due to the fact that the userLocation field is read only.
Use the [MKMapView showUserLocation:YES], create a MKMapViewDelegate and add the tracking when the annotation view for the user location is requested. This does not work, because the annotation view is apparently requested only once???
Use a CLLocationManager and try to add the blue pin manually. Unfortunately, I did not find the blue pin in the available pin types, so I tried to create a user annotation manually without success.
Does anyone has any idea how I can achieve this and still benefit from the blue pin or is my only solution to use a CLLocationManager and create my own pin?
CLLocationManager uses the same data across all of its instances. MKMapView uses CLLocationManager's data internally. That said the solution to do what you want to do is let MKMapView do its own thing with regards to showUserLocation:. At the same time, create an instance of CLLocationManager and its delegate.
The delegate messages will give you the GPS coordinate location of MKMapView's blue pin. Everything will be in sync with each other.

Problem in moving MKPinAnnotation on iPhone map

I am building an app that tracks user location on map. I can insert a pin easily, but when the location is changed, the app quits.
I have set the default coordinates to 0,0 in viewdidload: method and I have added a pin at that location. I have done this because I want to remove the pin when location is updated and then insert the pin again on new location.
Here is the code which I have written in -(void)locationManager: didUpdateToLocation:fromLocation: method.
[mapview removeAnnotation:myannotation];
CLLocationCoordinate2D currentlocation;
currentlocation.latitude=newLocation.coordinate.latitude;
currentlocation.longitude=newLocation.coordinate.longitude;
myannotation=[[[CSMapAnnotation alloc]initWithCoordinate:currentlocation annotationType:CSMapAnnotationTypeStart title:#"My Location"] autorelease];
[mapview addAnnotation:myannotation];
Here, myannotation is the pin which I want to add,newLocation is the updated location and CSMapAnnotation will return annotation view.
The problem is my app crashes everytime. I was able to get warning message on console before crashing. Here is the message:
"An instance 0x182020 of class CSMapAnnotation is being deallocated while key value observers are still registered with it. Observation info is being leaked, and may even become mistakenly attached to some other object."
So, plz help if anyone has faced the same problem...
A few things to look out for:
MKMapView already has a showsUserLocation attribute that tracks the user's location for you with the pulsing blue-dot thing. If you want to separately set a pin on that location, you can get the userLocation attribute from the mapview.
The crashing bug could be because of the autorelease call. Easiest way to fix it is to make myannotation a property with a retain attribute, then take out the autorelease and instead of myannotation use self.myannotation.
Not really clear what you mean by setting "default coordinates to 0, 0." If these are lat/longs then you're putting a pin in Greenwich, England :-) You don't really need to continually add and remove annotations. You can add them once then adjust their position as needed.
I am thinking it is not liking your autorelease tag for myannotation, when you add the annotation to the map it might not be being retained, and then it just deallocates and crash, thats what i can think of from looking at the code you posted. Hope it helps
I just experienced this same problem and it had something to do with the coordinates of the annotation. I was already ignoring 0,0 but for some reason users were inputting -180,-180 (maybe from a device where we couldnt determine the location?)
anyways - as soon as I started ignoring -180,-180 the problem went away.
hope this helps.