1.导入
self.locationManager = [[CLLocationManager alloc]init];
//开启定位
[self.locationManager startUpdatingLocation];
//定位授权
[self.locationManager requestWhenInUseAuthorization];
//绑定代理
self.locationManager.delegate = self ;
//位置更新后走这个代理方法
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation * location = locations.firstObject;
//经度
self.outputLongitude.text =[ NSString stringWithFormat:@"%f",location.coordinate.longitude ];
//经度
self.outputLatitude.text = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
//地理编码 反地理编码
CLGeocoder * geo = [[CLGeocoder alloc]init];
//反地理编码(把经纬度转换为位置信息)
[geo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark * mark = placemarks.firstObject;
self.outputLocation.text= mark.name ;
NSLog(@"%@",mark.name);
}];
}
//根据经纬度 转换 地理信息
- (IBAction)geocoderConvertAction:(id)sender {
CLLocation * location = [[CLLocation alloc]initWithLatitude:[self.inputlatitude.text doubleValue] longitude:[self.inputLongitude.text doubleValue]];
CLGeocoder * geo = [[CLGeocoder alloc]init];
[geo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark * mark = placemarks.firstObject;
self.displayLocation.text= mark.name ;
NSLog(@"%@",mark.name);
}];
}
- (IBAction)locationConvertAction:(id)sender {
CLGeocoder * geo = [[CLGeocoder alloc]init];
//修饰弱引用(只用不持有)
__weak ViewController * weakSelf = self;
[geo geocodeAddressString:self.inputLocation.text completionHandler:
^( NSArray * placemarks, NSError *error){
CLPlacemark * mark = placemarks.firstObject;
self.displaylongitude.text =[NSString stringWithFormat:@"%f", mark.location.coordinate.longitude];
self.displaylatitude.text = [NSString stringWithFormat:@"%f",mark.location.coordinate.latitude];
}];
}
self.mapView = [[MKMapView alloc]initWitFrame:self.view.frame];
[self.view addSubview:self.mapView];
//展示用户位置
self.mapView.showsUserLocation = YES;
//设置跟踪模式
self.mapView.userTrackingMode = MKUserTrackingModeFollowingWithHeading;
//设置代理
self.mapView.delegate = self;
CLLocation * location = [[CLLocation alloc ]initWithLatitude:40 longitude:116.5];
[self setAnnotationWithLocation: location];
//设置地图格式
self.mapView.mapType = MKMapTypeHybrid;
//执行代理中的方法
- (void)mapView:(MKMapView*)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLGeocoder * geo = [[GLGeocoder alloc]init];
[geo reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray * placemarks,NSError * error){
CLPLacemark * mark = placemarks.firstObject;
//显示地点名称
self.mapView.userLocation.title= mark.name;
//添加子标题
self.mapView.userLocation.subtitle = mark.addressDictionary[@"City"];}];
}
//根据传过来的location显示大头针
- (void)setAnnotationWithLocation:(CLLocation *)location
{
self.annotation = [[MKPointAnnotation alloc]init];
self.annotation.coordinate = location.coordinate;
//添加到地图上
[self.mapView addAnnotation:self.annotation];
CLGeocoder * geo = [[CLGeocoder alloc] init];
[geo reverseGeocodeLocation:location completionHandler:^(NSArray * placemarks,NSError * error){
CLPlacemark * mark = placemarks.firstObject;
self.annotation.title= mark.name;
//大头针的坐标最好不要和location的坐标设置一样,测试时无法同时显示
}];
}
PS:在使用storyboard时,再添加MapKit View之前,在设置界面General、Linked FrameWorks and libraries 中添加MapKit.frameWork;不然运行程序会崩溃。