#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "LanouViewController.h"
#import "MyAnnotation.h"
@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>{
//定位管理类
CLLocationManager *manager;
//用户位置
CLLocation *userLocation1;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建地图
MKMapView *mapView=[[MKMapView alloc]initWithFrame:self.view.frame];
//设置地图的属性
// mapView.userLocation=@"经纬度";
mapView.showsUserLocation=YES;
//添加到视图上
[self.view addSubview:mapView];
//地图的类型
mapView.mapType=MKMapTypeStandard;
//判断系统
if ([[[UIDevice currentDevice] systemVersion]floatValue] >= 8.0) {
//允许定位,需要在infor.plist里面添加字段,并且和此处保持一致
//ios8.0以后添加plist文件
//使用CLLocationManager,并且声明为全局变量
manager=[[CLLocationManager alloc]init];
//设置权限为使用时进行定位
[manager requestWhenInUseAuthorization];
//设置跟踪模式 为一直跟踪
[mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
//设置地图的代理
mapView.delegate=self;
//设置地图管理类的代理
manager.delegate=self;
//距离筛选器,设置最小的位置更新提示距离
manager.desiredAccuracy=1000;
[manager startUpdatingLocation];
}
#pragma mark --MKMapViewDelegate
#pragma mark --用户位置发生了变化
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
userLocation.title=@"北京";
userLocation.subtitle=@"清河";
//插一根大头针
MKPointAnnotation *point=[[MKPointAnnotation alloc]init];
CLLocationCoordinate2D theLocation=userLocation.coordinate;
point.coordinate=theLocation;
[mapView addAnnotation:point];
CLLocationCoordinate2D secondCoor=CLLocationCoordinate2DMake(39.00, 116.00);
//自定义的大头针
MyAnnotation *myAnnotation=[[MyAnnotation alloc]initWithTitle:@"金五星" subTitle:@"蓝欧科技" coordinate:secondCoor];
[mapView addAnnotation:myAnnotation];
}
#pragma mark --往地图上添加标记,例如大头针
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
//创建重用标示符
static NSString *reuseIdentifier=@"asd";
//根据重用标示符查找是否有创建好的可重用的
MKPinAnnotationView *pinAnnotation=(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
//判断是否有可重用的
if (pinAnnotation==nil) {
//如果没有就去创建
pinAnnotation=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
}
//左视图imageView
UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 70, 50)];
imgView.image=[UIImage imageNamed:@"111.png"];
[pinAnnotation setLeftCalloutAccessoryView:imgView];
//右视图
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(0, 0, 70, 50);
[button setTitle:@"蓝欧" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[pinAnnotation setRightCalloutAccessoryView:button];
//显示视图
pinAnnotation.canShowCallout=YES;
return pinAnnotation;
}
#pragma mark --点击气泡的时候
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
NSLog(@"点击气泡的时候...");
}
#pragma mark --点击可编辑区域的时候---
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
//获取控制器
LanouViewController *lan=[[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"LanouViewController"];
[self.navigationController pushViewController:lan animated:YES];
}
#pragma mark --CLLocationManagerDelegate
#pragma mark --位置发生变化,旧位置到新位置
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
NSLog(@"从一个旧的位置到一个新的位置");
}
#pragma mark --位置发生了变化
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0){
NSLog(@"location=%@",locations[0]);
//1.获取经纬度
CLLocation *location=[locations lastObject];
//纬度
CLLocationDegrees lati=location.coordinate.latitude;
//经度
CLLocationDegrees longti=location.coordinate.longitude;
userLocation1=[[CLLocation alloc]initWithLatitude:lati longitude:longti];
//地理逆编码
CLGeocoder *goconder=[[CLGeocoder alloc]init];
[goconder reverseGeocodeLocation:userLocation1 completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if(!error){
CLPlacemark *mark=placemarks[0];
NSLog(@"位置信息是:%@",mark);
NSDictionary *dict=mark.addressDictionary;
NSLog(@"%@",dict);
NSString *city=dict[@"city"];
NSLog(@"%@",city);
NSString *name=mark.name;
NSLog(@"===========%@",name);
NSString *thruFare=mark.thoroughfare;
NSLog(@"===========%@",thruFare);
NSString *subThru=mark.subThoroughfare;
NSLog(@"============%@",subThru);
NSString *locality=mark.locality;
NSLog(@"=============%@",locality);
NSString *admin=mark.administrativeArea;
NSLog(@"=============%@",admin);
NSString *postal=mark.postalCode;
NSLog(@"==============%@",postal);
}
NSLog(@"编码完成");
}];
}