开发者

Placing a blue dot (user location) Only using CLLocationManager

I've read through countless posts here on stack and apple's docs and can't find anything to solve this problem.

The issue is if you set mapView.showsUserLocation = YES, then MapKit will start making it's own GPS queries to your phone.

From apple docs:

Setting this property to YES causes the map view to use the Core Location framework to find the cu开发者_Python百科rrent location. As long as this property is YES, the map view continues to track the user’s location and update it periodically.

If you also want to use CLLocationManager, then when you make a call to [mylocationmanager startUpdatingLocation], then you are making a second GPS query on your phone.

Now you have 2 separate processes asking for GPS location.

Not a problem on the simulator, but if you try it on a real phone it will take a very very long time to get the GPS location. It is also inconsistent 10seconds - 1 minute, whereas if you turn off mapView.showsUserLocation it takes 2-3 seconds very consistently.

In general it seems like a very bad practice to use both.

For flexibility and control, I'd rather use CLLocationManager, but if you don't set mapView.showsUserLocation = YES, then you don't get the blue dot!

I tried the usual overwrite annotation methods: eg:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    if ([annotation isKindOfClass:MKUserLocation.class]) {
        //it's the built-in user location annotation, return nil to get default blue dot...
        return nil;
    }

    //handle your custom annotations...
}

But it doesn't work, most probably because there is never a call to actually place a user annotation on the map.

So does anyone have a solution to only use CLLocationManager to place the user's location on the map?


Just overriding the viewForAnnotation method isn't enough, you first have to add you annotations to the map by calling

[mapView addAnnotation:annotationObject];

Your annotationObject can be an instance of any class that implements the MKAnnotation protocol. You can find the details in the MapKit Guide in the Annotating apps section.


If you need to show just blue dot around your location (accuracy), you can do it like this:

MKCircle *accuracyCircle;

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// when you want update your position and accuracy
[self.mapView removeOverlay:accuracyCircle];
accuracyCircle = [MKCircle circleWithCenterCoordinate:newLocation.coordinate
                                                    radius:newLocation.horizontalAccuracy];
[self.mapView addOverlay:accuracyCircle];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
if([overlay isKindOfClass:[MKCircle class]])
    {
        MKCircleRenderer * circleRenderer = [[MKCircleRenderer alloc] initWithOverlay:overlay];
        circleRenderer.fillColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.2];
        return circleRenderer;
    }
    return nil;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜