iOS应用之地图
2015-09-28 14:10
435 查看
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;不然运行程序会崩溃。
相关文章推荐
- ios 里如何判断当前应用的定位服务是否可用
- iOS8 【xcode6中添加pch全局引用文件】
- 关于真机测试证书的申请
- ios去掉navigationController和tabBarController里的边框黑线
- iostat分析磁盘的IO状况
- iOS9适配
- iOS蓝牙4.0
- IOS开发—对当前屏幕闲置时间计时
- iOS上如何让按钮文本左对齐问题
- Nagios
- 实现iOS全局禁止横屏,但kxmovie全屏横屏播放视频
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- ios模拟器不响应电脑键盘解决方法
- iOS - 验证输入的是否是正确的身份证号码和手机号码
- iOS Xcode错误: linker command failed with exit code 1 (use -v to see invocation) 可能错误与处理方法
- 企业级分发-ios9新特性
- iOS开发详解之几种基础动画篇
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS/OS X开发:各种工具快到碗里来
- IOS 项目的框架搭建