MKMapView not showing userLocation, even though the location is updated

Discussion in 'iOS Programming' started by tadelv, Aug 5, 2009.

  1. macrumors newbie

    Joined:
    Sep 20, 2008
    #1
    Hello all!

    i am developing a small app that uses the map kit and i would like to display the users location with the blue dot.

    I use the corelocation services to obtain the current location and when the location is found, i present the map view. This means i know when corelocation has aquired a location that is accurate.

    The problem is however, that the mapviews userlocation is nil -> how can this be? at least for a few seconds, then it sometimes displays and sometimes it doesnt.

    does anyone have any idea, how to fix this? perhaps force a location update to mapview?

    thanks!
     
  2. macrumors newbie

    Joined:
    Aug 10, 2009
    #2
    i have the exact same problem... one way to get around it would be use the location manager delegate but I think that's very much an overkill. Can anyone help with this issue?
     
  3. macrumors newbie

    Joined:
    Jan 16, 2008
    #3
    I had the same problem

    My solution was to enable the user location dot after CL had found the location.

    Code:
    - (void)locationUpdate:(CLLocation *)location {
        [mapView setCenterCoordinate:location.coordinate];
        if ([mapView showsUserLocation] == NO) {
            [mapView setShowsUserLocation:YES];
        }
    }
    the "userLocation" property does not enable the blue dot.

    if you enable setUserLocation before CL has a coordinate, the blue dot might not show.

    Without setting any delegates, MKMapView still gets a location if the app is allowed for location updates. It gets its location updates for the blue dot from somewhere else.
     
  4. thread starter macrumors newbie

    Joined:
    Sep 20, 2008
    #4
    Hello,

    thank you for your replies.

    To clear things up a little bit, i obviously was not specific enough.
    I know that the CL has the updated location, i am handling it in another class, not in the viewcontroller.
    So, when the location is updated, i present the map view. i add some annotations and set the showsUserLocation property to yes. The pins show, but the blue dot does not.
    If i however go to the Maps.app first, update location there and return to my application, then the blue dot appears :)
    i find this very confusing, has anyone contacted apple on this issue?
     
  5. macrumors newbie

    Joined:
    Aug 13, 2009
    #5
    The Blue Dot - MKMapView's userLocation annotation

    Hi all,
    I just ran into the same problem you're describing.
    I want to add my own annotation views as well, so I need to implement the mapView:viewForAnnotation: method on the MapView's delegate.
    Also, I wanted to display the user's current position (the animated, blue dot).

    The Blue Dot works by setting:
    Code:
    myMapView.showsUserLocation = YES
    And by implementing the delegate method as follows:

    Code:
    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
    	MKAnnotationView* annotationView;
    	
    	if (annotation == mapView.userLocation)
    	{
    		// We can return nil to let the MapView handle the default annotation view (blue dot):
    		// return nil;
    
    		// Or instead, we can create our own blue dot and even configure it:
    
    		annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"blueDot"];
    		if (annotationView != nil)
    		{
    			annotationView.annotation = annotation;
    		}
    		else
    		{
    			annotationView = [[[NSClassFromString(@"MKUserLocationView") alloc] initWithAnnotation:annotation reuseIdentifier:@"blueDot"] autorelease];
    			
    			// Optionally configure the MKUserLocationView object here
    			// Google MKUserLocationView for the options
    			
    		}
    	}
    	else
    	{
    		// The requested annotation view is for another annotation than the userLocation.
    		// Let's return a normal pin:
    
    		annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"regularPin"];
    		
    		if (annotationView != nil)
    		{
    			annotationView.annotation = annotation;
    		}
    		else
    		{
    			annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"regularPin"] autorelease];
    		}
    	}
    	return annotationView;
    }
    Now both my pins appear, and the blue dot appears as soon as a location fix is found.
     
  6. thread starter macrumors newbie

    Joined:
    Sep 20, 2008
    #6
    Hello!

    i tried out your sample code - the blue dot appears! :)

    however, as soon as i start moving the map around, i get the following error:
    Code:
    *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <NSCFArray: 0x2fafc0> was mutated while being enumerated.'
    It seems as if the dequeued "blueDot" is always nil... i'll look closer into it, but thanks! one step closer now :)
     
  7. thread starter macrumors newbie

    Joined:
    Sep 20, 2008
    #7
    Ok, i managed to get the blueDot to appear everytime is show the mapView :)

    Code:
    - (void)resetUserLocation;
    this one is undocumented however, but i think this is the way to go. maybe find another method that does something similar, because with this one, you get a approx. 1sec delay with the blue dot appearing.

    anyways, thank you all for helping me out!
     
  8. macrumors newbie

    Joined:
    Oct 8, 2009
    #8
    Hi tadelv, Could you explain how you fixed the crash when zooming the map? I am having the same problem thanks!
     
  9. macrumors newbie

    dschiefer

    Joined:
    Jan 11, 2010
    Location:
    UK
    #9
    Same Problem

    I have exactly the same issue, when I move around the map or zoom out for a short time, it crashes...has anyone found a solution for this?
     
  10. macrumors newbie

    dschiefer

    Joined:
    Jan 11, 2010
    Location:
    UK
    #10
    I have now found a solution myself, nothing was wrong with the code above, but I inserted an object that was already released, therefore causing the BAD_ACCESS error to occur and my app to crash. It only occurred when scrolling because the MKMapView was tracking my position and does so only every few seconds (think its 5?). therefore, after 5 seconds my app crashed...
     

Share This Page