How to set accuracy and distance filter when using MKMapView

前端 未结 3 1079
说谎
说谎 2020-12-07 10:29

When i use setShowsUserLocation with MKMapView to track user location, how do I set the accuracy and distance filter? I am not talking about

3条回答
  •  醉话见心
    2020-12-07 10:57

    For displaying map here is an example code.

    First import MKMapKit and CoreLocation framework in your .h file.

    #import 
     #import 
    

    Add MKMapKit and CoreLocation Delegate in .h file

    @interface MapViewController : UIViewController 
    
    
    CGPoint gameMapCenter = CGPointMake([[UIScreen mainScreen] bounds].size.width / 2, [[UIScreen mainScreen] bounds].size.height / 2);
        gameMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 640, 620)];
        [gameMapView setCenter:gameMapCenter];
        [gameMapView setMapType:MKMapTypeStandard];
        [gameMapView setDelegate:self];
        [self.view addSubview:gameMapView];
        [gameMapView setShowsUserLocation:YES];
    

    Use CLLocationManager for fetching user location.

    Declare an instance of CLLocationManager

    CLLocationManager *locationManager;
    

    In ViewDidLoad

    locationManager = [[CLLocationManager alloc] init];
    
    [locationManager setDelegate:self];
    
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    
    [locationManger startUpdatingLocation];
    

    startUpdatingLocation Method Implementation:

    (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
    {
          //Your Stuff
    }
    

提交回复
热议问题