[x-Code7新功能之四]Core Location和MapKit框架练习
来源:互联网 发布:linux 黑客工具 编辑:程序博客网 时间:2024/05/19 13:18
#import <UIKit/UIKit.h>#import <CoreLocation/CoreLocation.h>#import <MapKit/MapKit.h>@interface MyAnnotication : NSObject<MKAnnotation>@property (nonatomic) CLLocationCoordinate2D coordinate;@property (nonatomic, copy) NSString *title;@property (nonatomic, copy) NSString *subtitle;@end@interface ViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate>@end
#import "ViewController.h"@implementation MyAnnotication@end@interface ViewController ()@end@implementation ViewController{ CLLocationManager* _Manager; CLGeocoder* _Geocoder; CLLocation* _MyLocation; MKMapView* _MapView;}- (void)viewDidLoad { [super viewDidLoad]; _Manager = [[CLLocationManager alloc] init]; _MapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds]; _MapView.delegate = self; _MapView.userTrackingMode = MKUserTrackingModeFollow; [self.view addSubview:_MapView]; if (![CLLocationManager locationServicesEnabled]) { NSLog(@"定位功能还没有打开"); return; } CLAuthorizationStatus state = [CLLocationManager authorizationStatus]; if (state ==kCLAuthorizationStatusNotDetermined){ [_Manager requestWhenInUseAuthorization]; } else if (state == kCLAuthorizationStatusAuthorizedWhenInUse){ _Manager.delegate = self; _Manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; CLLocationDistance distance = 10.0; _Manager.distanceFilter = distance; [_Manager startUpdatingLocation]; } _Geocoder = [[CLGeocoder alloc] init]; /*地理编码测试*/// [_Geocoder geocodeAddressString:@"昌平区沙河镇" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {// [placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {// CLLocation* Location = obj.location;// CLRegion* Region = obj.region;// NSData* data = [NSJSONSerialization dataWithJSONObject:obj.addressDictionary options:NSJSONWritingPrettyPrinted error:nil];// NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];// NSLog(@"位置:%@,区域:%@,详细信息:%@",Location,Region,str);// }];// }];}-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ _MyLocation = [locations firstObject]; CLLocationCoordinate2D FirstCoordinate = [_MyLocation coordinate]; NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",FirstCoordinate.longitude,FirstCoordinate.latitude,_MyLocation.altitude,_MyLocation.course,_MyLocation.speed); /*地理反编码测试~*/ [_Geocoder reverseGeocodeLocation:_MyLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { CLPlacemark *placemark=[placemarks firstObject]; NSData* data = [NSJSONSerialization dataWithJSONObject:placemark.addressDictionary options:NSJSONWritingPrettyPrinted error:nil]; NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; MyAnnotication *annotation1=[[MyAnnotication alloc]init]; annotation1.title=[placemark.addressDictionary valueForKey:@"Name"]; annotation1.subtitle=[placemark.addressDictionary valueForKey:@"SubLocality"]; annotation1.coordinate=FirstCoordinate; [_MapView addAnnotation:annotation1]; /*定位一次后,关闭*/ [_Manager stopUpdatingLocation]; NSLog(@"详细信息:%@",str); }];}-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ static NSString* ViewIdentifer = @"ViewIdentifer"; if ([annotation isKindOfClass:[MyAnnotication class]]) { MKAnnotationView* AnnotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:ViewIdentifer]; if (!AnnotationView) { AnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ViewIdentifer]; AnnotationView.canShowCallout = YES;/*允许用户点击*/ AnnotationView.calloutOffset = CGPointMake(0, 1); AnnotationView.leftCalloutAccessoryView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"J.jpg"]]; /*如果不设置这个值,什么都看不到~*/ AnnotationView.image = [UIImage imageNamed:@"J.jpg"]; } return AnnotationView; } else{ return nil; } }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
定位效果还挺理想,不错啊。
想利用这个写个移动签到的小程序。。。
参考:http://www.cnblogs.com/kenshincui/p/4125570.html
0 0
- [x-Code7新功能之四]Core Location和MapKit框架练习
- [x-Code7新功能之二]UIAlertController的练习
- [x-Code7新功能之三]PHPhotoLibrary的练习
- Core Location,MapKit 和Geofengcing
- IOS学习之Core Location定位和MapKit视图使用
- Core Location定位和MapKit视图使用
- IOS学习之十二:Core Location定位和MapKit视图使用
- IOS学习之十二:Core Location定位和MapKit视图使用
- Core Location和MapKit的一些简单使用
- [X-Code7新功能之一]OC的泛型支持
- Cocoa框架-Core Location
- ios-Core Location框架
- 自学Swift-斯坦福笔记整理(十四) Core Location and MapKit
- x-code7遇到问题
- Core Location之指南针
- Core Location框架基本概念理解
- MapKit框架
- IOS开发之Core Location
- android自定义loding view(上)
- JavaScript入门
- struts上传和下载
- There are inconsistent line endings in the "XXX.cs" script
- Class.isAssignableFrom()和instanceOf关键字
- [x-Code7新功能之四]Core Location和MapKit框架练习
- 刷新例子
- TransR/CTransR论文剩余部分:元组分类,文本关系抽取,结论
- hdoj 5665 Lucky 【水题】
- Python Paste Deployment
- 数据结构—排序
- blog 迁移
- JAVA--浅谈线程
- Maven教程:Maven Pom.xml文件详细解释