要得到当前的位置,只需要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];}
那么定位当前位置就完成,代码量非常的少,也非常的简单