要得到当前的位置,只需要2步就能完成

      1:判断设备是否支持定位功能,然后创建MKMapView

if ([CLLocationManager locationServicesEnabled]) {
        myMapView =[[MKMapView alloc] init];
        myMapView.delegate=self;
        myMapView.showsUserLocation=YES;
    }

       2:实现MKMapViewDelegate协议

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    //得到经纬度
    CLLocation *newLocation=userLocation.location;

    NSLog(@"locaiton---%f---%f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
    
    //反地理编码
    CLGeocoder *clGeoCoder=[[CLGeocoder alloc] init];
    CLGeocodeCompletionHandler handle=^(NSArray *placemarks,NSError *error){
        for (CLPlacemark *placeMark in placemarks) {
            
            //获取地理位置名称
            NSDictionary *addressDic=placeMark.addressDictionary;
            NSString *state=[addressDic objectForKey:@"State"];
            NSString *city=[addressDic objectForKey:@"City"];
            NSString *subLocality=[addressDic objectForKey:@"SubLocality"];
            NSString *street=[addressDic objectForKey:@"Street"];
            
            NSLog(@"%@%@%@",state,subLocality,street);
        }
    };
//执行
    [clGeoCoder reverseGeocodeLocation:newLocation completionHandler:handle];
}

那么定位当前位置就完成,代码量非常的少,也非常的简单

相关文章:

  • 2021-11-21
  • 2021-12-22
  • 2021-07-27
  • 2022-02-09
  • 2022-02-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
相关资源
相似解决方案