#import <MapKit/MKMapView.h>
<MKMapViewDelegate,CLLocationManagerDelegate>
{
CLLocationDegrees lati;
CLLocationDegrees longti;
NSMutableArray *chooseArray;
NSMutableArray *tittleArray;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//初始化地图视图
MKMapView* mapview = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
//地图的代理方法
mapview.delegate = self;
//是否显示当前的位置
mapview.showsUserLocation = YES;
//地图的类型, iOS开发中自带的地图
//使用第三方的地图可以查找周边环境的餐馆,学校之类的
/*
MKMapTypeStandard 标准地图
MKMapTypeSatellite 卫星地图
MKMapTypeHybrid 混合地图
*/
mapview.mapType = MKMapTypeStandard;
//河南南阳的经纬度,初始化的坐标
CLLocationCoordinate2D coor2d = {33.00,112.52};
//CLLocationCoordinate2D coor2d = {37.7,112.4};
//显示范围,数值越大,范围就越大
MKCoordinateSpan span = {5,5};
MKCoordinateRegion region = {coor2d,span};
//是否允许缩放,一般都会让缩放的
//mapview.zoomEnabled = NO;
//mapview.scrollEnabled = NO;
//地图初始化时显示的区域
[mapview setRegion:region];
[self.view addSubview:mapview];
locationmanager = [[CLLocationManager alloc]init];
//设置精度
/*
kCLLocationAccuracyBest
kCLLocationAccuracyNearestTenMeters
kCLLocationAccuracyHundredMeters
kCLLocationAccuracyHundredMeters
kCLLocationAccuracyKilometer
kCLLocationAccuracyThreeKilometers
*/
//设置定位的精度
[locationmanager setDesiredAccuracy:kCLLocationAccuracyBest];
//实现协议
locationmanager.delegate = self;
NSLog(@"开始定位");
//开始定位
[locationmanager startUpdatingLocation];
}
#pragma mark locationManager delegate
//实现定位 6.0 过期的做法
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"hello");
//打印出精度和纬度
CLLocationCoordinate2D coordinate = newLocation.coordinate;
NSLog(@"输出当前的精度和纬度");
NSLog(@"精度:%f 纬度:%f",coordinate.latitude,coordinate.longitude);
//停止定位
[locationmanager stopUpdatingLocation];
//计算两个位置的距离
float distance = [newLocation distanceFromLocation:oldLocation];
NSLog(@" 距离 %f",distance);
//====位置的反编码 5.0 之后的
/*
*/
geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray*placemarks,NSError* error)
{
//NSLog(@"hffjv");
for (CLPlacemark* place in placemarks) {
//NSLog(@"hffjv");
NSLog(@"name %@",place.name); //位置
NSLog(@"thoroughfare %@",place.thoroughfare);//街道
//子街道
NSLog(@"subthoroughfare %@",place.subAdministrativeArea);
//市
NSLog(@"loclitity %@",place.locality);
//区
NSLog(@"subLocality %@",place.subLocality);
//国家
NSLog(@"country %@",place.country);
NSLog(@"hffjv");
}
}];
}
// 6.0 之后新增的位置调用方法
/*-(void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
for (CLLocation* location in locations) {
NSLog(@"%@",location);
}
//停止定位
// [manager stopUpdatingLocation];
}
*/
#pragma mark MKMapViewDelegate的代理方法
//返回标注视图(大头针视图)
/*- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <mkannotation>)annotation
{
}*/
//更新当前位置调用
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
}
//选中注释图标
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
}
//地图的显示区域改变了调用
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
}