iPhone dev - Can't show user's current location on mapView - iphone

I set the mapView's showUserLocation property to YES, and it does nothing. I was able to use a CLLocationManager to center the map on the user's current location, but there's no pin/dot on the screen. How can I put a pin on the map for the exact location of the user?

This has messed me up several times but its an easy fix
The problem is that your code is:
[self.mapView showUserLocation:YES];
Should be:
[self.mapView setShowsUserLocation:YES]
Also make sure you have set the delegate as well

IN Nib name you can set showuserlocation to mark for Mapview.

Related

iOS MapKit userLocation returning zero on first call, but

I have a curious issue that I can't explain and was wondering if anybody has encountered the same problem.
I have a MKMapView that displays the user's current location, and is written to center the region over that location when loaded. When I load the view, the map defaults to lat zero and long zero (somewhere in Africa). When I leave the view to the previous screen on the stack, and then go back to it everything works properly and I get a mapview centered over Cupertino, California as expected.
Can anyone explain this?
Cheers! :D
mapView.showsUserLocation=YES;
Its Working Device Perfect when ever Location manager unable to Track The Location Then Its Shows Default Location Thats lat zero and long zero so Its Shows In Africa.
Check In Device It Will Work Fine.
UserLocation try to get the coordinate after map view loaded.
before getting the value, user location always is (0,0)
So do not add the setregion method in viewdidload
add it into method fllowing:
func mapView(MKMapView, didUpdate: MKUserLocation)

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;

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.