tags:

views:

1475

answers:

2

hello all

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: 

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

Daniel
A: 

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.

Ramin
thanx Ramin and Daniel...I did what you guys said and now its working happily...! I simply didnt know about MKUserLocation class.I added 3 lines of code and it solved all issues.MKUserLocation *ulocation=[[MKUserLocation alloc]init];[mapview setShowsUserLocation:TRUE];[mapview addAnnotation:ulocation]; Thanx to you guys and this great website..
Napster