iOS 高德地图

//
//  RootViewController.m
//  MapView
//
//  Created by  on 15/8/22.
//  Copyright (c) 2015   congWang . All rights reserved.
//

#import  "RootViewController.h"
#import
 
#import
  "KCAnnotinate.h"
#import
    // 实现地理编码和反编码 ,  以及定位功能


@interface  RootViewController  ()<</span>MKMapViewDelegateCLLocationManagerDelegate>

@property (nonatomicstrong)MKMapView * mapView; //mapView设置全局属性

@property (nonatomicstrong)NSArray * arrayEnum; //用于改变按钮上面的显示

@property (nonatomicstrong)CLLocationManager * locationManager; //定位管理对象

@property (nonatomicassign)CLLocationCoordinate2D coofdinate; //实现定位后重新对地理坐标进行赋值

@property (nonatomicassign)MKCoordinateSpan span;

@property (nonatomicassign)MKCoordinateRegion region;

@property (nonatomicstrong)KCAnnotinate * annotation; //大头针,用于全局

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    //初始化改变按钮显示的数组
    self.arrayEnum @[@"平面地图"@"卫星无字"@"卫星有字"];

   
    //配置地图
    [self configureMapview];
   
    //添加按钮
    [self configureButton];
   
   
    //添加大头针
    [self configureAnnotationWithLocationCoordinate2D:self.coofdinate];
   
    self.mapView.delegate self;
   
    //实现地理编码和反编码
    [self geoByLocationOrAddress];
   
   
    //实现定位
    
   
    [self configureLocationManager];
}

#pragma mark - 添加地图 -
- (void)configureMapview
{
   
    self.mapView= [[MKMapView allocinitWithFrame:[[UIScreen mainScreenbounds]];
   
    self.coofdinate CLLocationCoordinate2DMake(34.75797500113.66541200); //经度
    self.span MKCoordinateSpanMake(0.050.05); //缩放比例
   
    self.region MKCoordinateRegionMake(_coofdinate_span);
   
    _mapView.mapType MKMapTypeStandard;
   
    _mapView.region _region;
   
   
    [self.view addSubview:_mapView];
   
}

//添加大头针
- (void)configureAnnotationWithLocationCoordinate2D:(CLLocationCoordinate2D)coordinate
{
   
    self.annotation = [[KCAnnotinate allocinit];
    _annotation.coordinate = coordinate;
    _annotation.title @"hauzi";
    _annotation.subtitle @"test";
    _annotation.image = [UIImage imageNamed:@"foot"];
    [self.mapView addAnnotations:@[_annotation]];
   
}

#pragma mark - 地图代理方法-
//大头针复用 类似于cellForRow
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<</span>MKAnnotation>)annotation{
   
    static NSString * indentifier = @"annotationView";
   
   
    MKAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:indentifier];
    if (!annotationView) {
        annotationView = [[MKAnnotationView allocinitWithAnnotation:annotation reuseIdentifier:indentifier];
       
        annotationView.canShowCallout YES;
       
       
    }
   
    annotationView.annotation = annotation;
    annotationView.image = ((KCAnnotinate *)annotation).image;
   
    return annotationView;
}


#pragma mark - 添加按钮, 用于改变地图显示类型 -

- (void)configureButton{
   
    //添加按钮,用于改变地图显示样式
    UIButton * buttonToChangeMapViewType = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonToChangeMapViewType.backgroundColor = [UIColor colorWithRed:31 255.0 green:31 255.0 blue:31 255.0 alpha:0.5];
    [buttonToChangeMapViewTypsetTitleColor:[UIColor whiteColorforState:UIControlStateNormal];
    [buttonToChangeMapViewTypsetTitle:@"平面地图" forState:UIControlStateNormal];
    buttonToChangeMapViewType.frame CGRectMake(375 3667 40375 340);
    [buttonToChangeMapViewTypaddTarget:self action:@selector(changeMapViewTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonToChangeMapViewType];
    //添加效果
    [self configureButtonEffectWithButton:buttonToChangeMapViewType];
   
   
    // 添加按钮, 用于实现开始定位
    UIButton * buttonToEnsureLocation = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonToEnsureLocation.backgroundColor = [UIColor colorWithRed:31 255.0 green:31 255.0 blue:31 255.0 alpha:0.5];
    [buttonToEnsureLocation setTitleColor:[UIColor whiteColorforState:UIControlStateNormal];
    [buttonToEnsureLocation setTitle:@"点我定位" forState:UIControlStateNormal];
    buttonToEnsureLocation.frame CGRectMake(375 375 3667 40375 340);
    [buttonToEnsureLocation addTarget:self action:@selector(ensureLocationAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonToEnsureLocation];
    [self configureButtonEffectWithButton:buttonToEnsureLocation];
   
    // 添加按钮, 定位到郑州
    UIButton * buttonToZhengZhou = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonToZhengZhou.backgroundColor = [UIColor colorWithRed:31 255.0 green:31 255.0 blue:31 255.0 alpha:0.5];
    [buttonToZhengZhou setTitleColor:[UIColor whiteColorforState:UIControlStateNormal];
    [buttonToZhengZhou setTitle:@"郑州" forState:UIControlStateNormal];
    buttonToZhengZhou.frame CGRectMake(0667 40375 340);
    [buttonToZhengZhou addTarget:self action:@selector(toZhengZhouAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonToZhengZhou];
    [self configureButtonEffectWithButton:buttonToZhengZhou];
   
}

// 为按钮添加效果
- (void)configureButtonEffectWithButton:(UIButton *)button{
   
    button.layer.cornerRadius 1;
    button.layer.masksToBounds YES;
    button.layer.borderColor = [UIColor blackColor].CGColor;
    button.layer.borderWidth 1;
    button.showsTouchWhenHighlighted YES;
   
}

//改变地图样式
- (void)changeMapViewTypeAction:(UIButton *)button{
   
    static int temp = 0;
    temp ++;
    if (temp > 2) {
        temp = 0;
    }
    [button setTitle:self.arrayEnum[temp] forState:UIControlStateNormal];
    self.mapView.mapType = temp;
   
}

//实现定位
- (void)ensureLocationAction:(UIButton *)button{
   
    if ([button.titleLabel.text isEqualToString:@"点我定位"]) {
        //开启定位
        [self.locationManager startUpdatingLocation];
        [button setTitle:@"结束定位" forState:UIControlStateNormal];
       
    }else{
       
        //结束定位
        [self.locationManager stopUpdatingLocation];
        [button setTitle:@"点我定位" forState:UIControlStateNormal];
       
    }
   
   
}

// 到郑州
- (void)toZhengZhouAction:(UIButton *)button{

    self.coofdinate CLLocationCoordinate2DMake(34.75797500113.66541200); //经度
   
    self.region MKCoordinateRegionMake(_coofdinate_span);
    self.mapView.region self.region;
   
    //由于大头针是复用的,如果离开郑州,会丢失大头针,需要重新添加
    [self configureAnnotationWithLocationCoordinate2D:self.coofdinate];

}

#pragma mark - 地理编码和反编码 -
- (void)geoByLocationOrAddress{
   
   
    //地理编码
    [self getCoordinateByAddress:@"中国湖北省十堰市郧西县六郎乡"];
   
    //地理反编码
    CLLocation * location = [[CLLocation allocinitWithLatitude:33 longitude:110];
   
    [self getAddressByCoordinateWithLocation:location];
   
}

//地理编码 (从地址得到经纬度信息)
- (void)getCoordinateByAddress:(NSString *)AddressOfStr{
   
    //初始化一个地理编码对象
    CLGeocoder * geoCoder = [[CLGeocoder allocinit];
   
    // 实现地理编码
    [geoCoder geocodeAddressString:AddressOfStr completionHandler:^(NSArray *placemarks, NSError *error) {
       
        //NSLog(@"%d, %@", __LINE__, [placemarks firstObject]);
        CLPlacemark * getPlaceMarks = [placemarks firstObject];
       
        //位置
        CLLocation * location = getPlaceMarks.location;
       
        NSLog(@"location: %@ \n\n\n\n\n\n", location);
       
    }]; 
   
}

//地理反编码(经纬度转化为地址)
- (void)getAddressByCoordinateWithLocation:(CLLocation *)location{
   
    CLGeocoder * geocoder = [[CLGeocoder allocinit];
    //实现反编码
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
      
        //NSLog(@"%d, %@", __LINE__, [placemarks firstObject]);
        CLPlacemark * getPlacemark = [placemarks firstObject];
        //NSLog(@"详细信息:%@", getPlacemark.addressDictionary);
       
        //给大头针赋值
        self.annotation.title = getPlacemark.country;
        self.annotation.subtitle = getPlacemark.name;
       
        for (NSString * key in getPlacemark.addressDictionary) {
           
            if ([getPlacemark.addressDictionary[key] isKindOfClass:[NSArray class]]) {
                NSLog(@"%@: %@", key, [getPlacemark.addressDictionary[key] firstObject]);
            }else{
                NSLog(@"%@: %@", key, getPlacemark.addressDictionary[key]);
            }
        }
       
    }];
   
   
   
}



#pragma mark - 实现定位管理 -
- (void)configureLocationManager{
   
    self.locationManager = [[CLLocationManager allocinit];
   
    // [CLLocationManager locationServicesEnabled] 默认值为no, 就是没有打开定位服务
   
    if ([CLLocationManager locationServicesEnabled]) {
        //查看打开后的状态, 如果没有定位, 那么就实现定位
        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
            [self.locationManager requestAlwaysAuthorization]; //为了验证,填写一直定位
        }
    }
   
    _locationManager.delegate self;
   
    //定位精准度
    _locationManager.desiredAccuracy  kCLLocationAccuracyBest;
   
    //设置定位精准度
    CLLocationDistance distance = 10.0;
   
    _locationManager.distanceFilter = distance;
   
    //启动跟踪定位
//    [_locationManager startUpdatingLocation];
   
}



#pragma mark - 实现跟踪定位协议的方法 -
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
   
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
   
    //取出位置
    CLLocation * lastLocation = [locations firstObject];
   
//    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(34, 113);
   
    CLLocationCoordinate2D coordinate= lastLocation.coordinate;
   
   
    //NSLog(@"经度:%f, 纬度:%f, 海拔:%f, 航向:%f, 速度:%f", coordinate.longitude, coordinate.latitude, lastLocation.altitude, lastLocation.course, lastLocation.speed);
   
    self.region MKCoordinateRegionMake(coordinate, self.span);
    self.mapView.region self.region;
   
    //添加大头针
    self.annotation.image nil;
    [self configureAnnotationWithLocationCoordinate2D:coordinate];
    _annotation.image = [UIImage imageNamed:@"foot"];
   
    //调用地理反编码,给大头针赋值
    CLLocation * location = [[CLLocation allocinitWithLatitude:coordinate.latitude longitude:coordinate.longitude];
   
    [self getAddressByCoordinateWithLocation:location];
   
}


- ( void )didReceiveMemoryWarning {
    [
super  didReceiveMemoryWarning ];
   
  // Dispose of any resources that can be recreated.
}



//
//  KCAnnotinate.h
//  MapView
//
//  Created by 华子 on 15/8/22.
//  Copyright (c) 2015 华子. All rights reserved.
//

#import 
#import
 

@interface KCAnnotinate : NSObject<</span>MKAnnotation>

@property (nonatomicCLLocationCoordinate2D coordinate;



// Title and subtitle for use by selection UI.
@property (nonatomiccopyNSString *title;
@property (nonatomiccopyNSString *subtitle;

@property (nonatomicstrong)UIImage * image; //自定义大头针图片显示


@end


@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值