ViewController.m 文件
#import "ViewController.h"
//引入核心定位
#import <CoreLocation/CoreLocation.h>
//地图
#import <MapKit/MapKit.h>
//引入大头针
#import "Annotation.h"
//NSLocationWhenInUseUsageDescription-->YES(当使用)
//NSLocationAlwaysUseUsageDescription-->YES(一直)
@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
{
//创建管理者
CLLocationManager * locationManager;
//定义地图
MKMapView * mapViewWO;
UITextView * textview;
//创建地理编码对象
CLGeocoder * geoCoder;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self layout];
geoCoder=[CLGeocoder new];
//初始化管理器
locationManager=[CLLocationManager new];
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"定位服务当前可能尚未打开,请设置打开");
return ;
}
//如果没有授权,则请求用户授权
if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined) {
//如果没 请求授权
[locationManager requestWhenInUseAuthorization];
}else if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){
//遵守代理
locationManager.delegate=self;
//设置定位精度
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
//定位频率
locationManager.distanceFilter=100;
//启动设置
[locationManager startUpdatingLocation];
}
}
#pragma mark CoreLocation 代理
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(@"dfsf");
CLLocation * location=[locations firstObject];//取出位置信息
CLLocationCoordinate2D coordinate=location.coordinate;//获取位置坐标
NSLog(@"经度:%f",coordinate.longitude);
NSLog(@"纬度:%f",coordinate.latitude);
NSLog(@"海拔:%f 航向:%f 行走速度:%f",location.altitude,location.course,location.speed);
}
-(void)layout{
textview=[[UITextView alloc]initWithFrame:CGRectMake(10, 20, 280, 30)];
textview.layer.borderWidth=1;
textview.layer.borderColor=[UIColor grayColor].CGColor;
[self.view addSubview:textview];
UIButton * souSuo=[[UIButton alloc]initWithFrame:CGRectMake(300, 20, 60, 30)];
[souSuo setTitle:@"搜索" forState:UIControlStateNormal];
[souSuo setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[souSuo addTarget:self action:@selector(sousuo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:souSuo];
mapViewWO=[[MKMapView alloc]initWithFrame:CGRectMake(0, 50, 375, 667)];
[self.view addSubview:mapViewWO];
mapViewWO.delegate=self;
//设置用户位置追踪
mapViewWO.userTrackingMode=MKUserTrackingModeFollow;
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
NSLog(@"s");
//将地图中心位置移动到某点
//mapView setCenterCoordinate:(CLLocationCoordinate2D)
//地图显示区域
MKCoordinateRegion theRegion;
theRegion.center=userLocation.coordinate;
theRegion.span.latitudeDelta=0.1;
theRegion.span.longitudeDelta=0.1;
[mapViewWO setRegion:theRegion];
}
-(void)getDiMing:(NSString*)diming{
[geoCoder geocodeAddressString:diming completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//取得地标,地标中储存了详细的地址信息,
CLPlacemark *placemark=[placemarks firstObject ];
NSLog(@"%@",placemark);
//剖析地标
//位置
CLLocation * location=placemark.location;
CLLocationCoordinate2D zuobiao=location.coordinate;
NSLog(@"%@",location);
//区域
CLRegion *region=placemark.region;
NSLog(@"%@",region);
//详细地址
NSDictionary * dic=placemark.addressDictionary;
NSLog(@"%@",dic);
NSLog(@"%@",dic[@"Country"]);//国家
NSLog(@"%@",dic[@"Name"]);//省
NSLog(@"%@",dic[@"State"]);
NSLog(@"%@",dic[@"FormattedAddressLines"][0]);//详细地址
//大头针位置
CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(zuobiao.latitude, zuobiao.longitude);
Annotation * annotation=[Annotation new];
annotation.title=dic[@"Name"];
annotation.subtitle=dic[@"FormattedAddressLines"][0];
annotation.coordinate=location1;
[mapViewWO addAnnotation:annotation];
//将地图中心位置移动到某点
//mapView setCenterCoordinate:(CLLocationCoordinate2D)
//地图显示区域
MKCoordinateRegion theRegion;
theRegion.center=location1;
theRegion.span.latitudeDelta=0.1;
theRegion.span.longitudeDelta=0.1;
[mapViewWO setRegion:theRegion];
}];
}
-(void)sousuo{
NSLog(@"sdfsdfsdf");
[self getDiMing:textview.text];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//
// Annotation.h
// Core Location 定位功能
//
// Created by DC017 on 15/12/23.
// Copyright © 2015年 DC017. All rights reserved.
//
@end
_______________________________________________________________
大头针.h文件
#import <Foundation/Foundation.h>
//引入头文件,遵守协议---MKAnnotation
#import <MapKit/MapKit.h>
@interface Annotation : NSObject<MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic,copy)NSString * title;
@property(nonatomic,copy)NSString * subtitle;
@end