how to show current location on MKMapView

后端 未结 3 852
眼角桃花
眼角桃花 2021-02-13 21:47

I am using MKMapView on my application. i need to show current location on simulator.
is it possible. to show current location on simulator.

3条回答
  •  爱一瞬间的悲伤
    2021-02-13 22:21

     self.mapView.delegate =  self;
     self.mapView.showsUserLocation = YES;
    

    This will show the current location in MkMapview.

    If you are using Interface Builder,In the Attributes Inspector , we have an option Behaviour which has an option Show User Location, on checking that option will also do the same.

    If you are not able to see in simulator,

    1. Open the application in the simulator.
    2. From the Menu Bar select Debug - > Location - > (If "None" option is selected ,change it to "Custom Location") and set location .

    With CLLocationManager also we can get the current location, Import Corelocation FrameWork to the project

    In .h file

    #import 
    @property (nonatomic, strong) CLLocationManager *locationManager;
    @property (nonatomic, strong) CLLocation* currentLocation;
    

    In .m file

    if ([CLLocationManager locationServicesEnabled] )
        {
            if (self.locationManager == nil )
            {
                self.locationManager = [[CLLocationManager alloc] init];
                self.locationManager.delegate = self;
                self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
                self.locationManager.distanceFilter = kDistanceFilter; //kCLDistanceFilterNone// kDistanceFilter;
            }
    
            [self.locationManager startUpdatingLocation];
        }
    

    The delegate function :

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        self.currentLocation = [locations lastObject];
       // here we get the current location
    }
    

    Hope this answer may help you.

提交回复
热议问题