Blue dot placement (user location) Only with CLLocationManager

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

The problem is that you set mapView.showsUserLocation = YES, then MapKit will start creating your own GPS requests on your phone.

From apple documents:

Setting this property to YES causes the map to be used to use the main location of the frame to find the current location. While this property is YES, the map display continues to track user locations and updates periodically.

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

You now have 2 separate processes requesting a GPS location.

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

In general, it seems very bad practice to use both.

For flexibility and control, I would prefer to use CLLocationManager, but if you do not set mapView.showsUserLocation = YES, then you will not get a blue dot!

I tried the usual rewriting annotation methods: for example:

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

    //handle your custom annotations...
}

But this does not work, most likely because in fact there is never a call to place the user's annotation on the map.

- CLLocationManager ?

+3
2

viewForAnnotation , ,

[mapView addAnnotation:annotationObject];

annotationObject , MKAnnotation. MapKit " ".

0

(), :

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

Source: https://habr.com/ru/post/1794535/


All Articles