//
// 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>MKMapViewDelegate, CLLocationManagerDelegat e>
@property (nonatomic, strong)MKMapView * mapView; //mapView设置全局属性
@property (nonatomic, strong)NSArray * arrayEnum; //用于改变按钮上面的显示
@property (nonatomic, strong)CLLocationManager * locationManager; //定位管理对象
@property (nonatomic, assign)CLLocationCoordinate2D coofdinate; //实现定位后重新对地理坐标进行赋值
@property (nonatomic, assign)MKCoordinateSpan span;
@property (nonatomic, assign)MKCoordinateRegion region;
@property (nonatomic, strong)KCAnnotinate * annotation; //大头针,用于全局
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//初始化改变按钮显示的数组
self.arrayEnum = @[@"平面地图", @"卫星无字", @"卫星有字"];
//配置地图
[self configureMapview];
//添加按钮
[self configureButton];
//添加大头针
[self configureAnnotationWithL ocationCoordinate2D:self.coofdinate];
self.mapView.delegate = self;
//实现地理编码和反编码
[self geoByLocationOrAddress];
//实现定位
[self configureLocationManager ];
}
#pragma mark - 添加地图 -
- (void)configureMapview
{
self.mapView= [[MKMapView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.coofdinate = CLLocationCoordinate2DMa ke(34.75797500, 113.66541200); //经度
self.span = MKCoordinateSpanMake(0.05, 0.05); //缩放比例
self.region = MKCoordinateRegionMake(_coofdinate, _span);
_mapView.mapType = MKMapTypeStandard;
_mapView.region = _region;
[self.view addSubview:_mapView];
}
//添加大头针
- (void)configureAnnotationWithL ocationCoordinate2D:(CLLocationCoordinate2D)coordinate
{
self.annotation = [[KCAnnotinate alloc] init];
_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 dequeueReusableAnnotatio nViewWithIdentifier:indentifier];
if (!annotationView) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:indentifier];
annotationView.canShowCallout = YES;
}
annotationView.annotation = annotation;
annotationView.image = ((KCAnnotinate *)annotation).image;
return annotationView;
}
#pragma mark - 添加按钮, 用于改变地图显示类型 -
- (void)configureButton{
//添加按钮,用于改变地图显示样式
UIButton * buttonToChangeMapViewTyp e = [UIButton buttonWithType:UIButtonTypeCustom];
buttonToChangeMapViewTyp e.backgroundColor = [UIColor colorWithRed:31 / 255.0 green:31 / 255.0 blue:31 / 255.0 alpha:0.5];
[buttonToChangeMapViewTyp e setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[buttonToChangeMapViewTyp e setTitle:@"平面地图" forState:UIControlStateNormal];
buttonToChangeMapViewTyp e.frame = CGRectMake(375 / 3, 667 - 40, 375 / 3, 40);
[buttonToChangeMapViewTyp e addTarget:self action:@selector(changeMapViewTypeAction:) forControlEvents:UIControlEventTouchUpIns ide];
[self.view addSubview:buttonToChangeMapViewTyp e];
//添加效果
[self configureButtonEffectWit hButton:buttonToChangeMapViewTyp e];
// 添加按钮, 用于实现开始定位
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 whiteColor] forState:UIControlStateNormal];
[buttonToEnsureLocation setTitle:@"点我定位" forState:UIControlStateNormal];
buttonToEnsureLocation.frame = CGRectMake(375 - 375 / 3, 667 - 40, 375 / 3, 40);
[buttonToEnsureLocation addTarget:self action:@selector(ensureLocationAction:) forControlEvents:UIControlEventTouchUpIns ide];
[self.view addSubview:buttonToEnsureLocation];
[self configureButtonEffectWit hButton: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 whiteColor] forState:UIControlStateNormal];
[buttonToZhengZhou setTitle:@"郑州" forState:UIControlStateNormal];
buttonToZhengZhou.frame = CGRectMake(0, 667 - 40, 375 / 3, 40);
[buttonToZhengZhou addTarget:self action:@selector(toZhengZhouAction:) forControlEvents:UIControlEventTouchUpIns ide];
[self.view addSubview:buttonToZhengZhou];
[self configureButtonEffectWit hButton:buttonToZhengZhou];
}
// 为按钮添加效果
- (void)configureButtonEffectWit hButton:(UIButton *)button{
button.layer.cornerRadius = 1;
button.layer.masksToBounds = YES;
button.layer.borderColor = [UIColor blackColor].CGColor;
button.layer.borderWidth = 1;
button.showsTouchWhenHighlighte d = 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 = CLLocationCoordinate2DMa ke(34.75797500, 113.66541200); //经度
self.region = MKCoordinateRegionMake(_coofdinate, _span);
self.mapView.region = self.region;
//由于大头针是复用的,如果离开郑州,会丢失大头针,需要重新添加
[self configureAnnotationWithL ocationCoordinate2D:self.coofdinate];
}
#pragma mark - 地理编码和反编码 -
- (void)geoByLocationOrAddress{
//地理编码
[self getCoordinateByAddress:@"中国湖北省十堰市郧西县六郎乡"];
//地理反编码
CLLocation * location = [[CLLocation alloc] initWithLatitude:33 longitude:110];
[self getAddressByCoordinateWi thLocation:location];
}
//地理编码 (从地址得到经纬度信息)
- (void)getCoordinateByAddress:(NSString *)AddressOfStr{
//初始化一个地理编码对象
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
// 实现地理编码
[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)getAddressByCoordinateWi thLocation:(CLLocation *)location{
CLGeocoder * geocoder = [[CLGeocoder alloc] init];
//实现反编码
[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 alloc] init];
// [CLLocationManager locationServicesEnabled] 默认值为no, 就是没有打开定位服务
if ([CLLocationManager locationServicesEnabled]) {
//查看打开后的状态, 如果没有定位, 那么就实现定位
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNo tDetermined) {
[self.locationManager requestAlwaysAuthorizati on]; //为了验证,填写一直定位
}
}
_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 = CLLocationCoordinate2DMa ke(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 configureAnnotationWithL ocationCoordinate2D:coordinate];
_annotation.image = [UIImage imageNamed:@"foot"];
//调用地理反编码,给大头针赋值
CLLocation * location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[self getAddressByCoordinateWi thLocation:location];
}
#import
#import
#import
#import
@interface
@property
@property
@property
@property
@property
@property
@property
@end
@implementation
- (void)viewDidLoad {
}
#pragma mark -
- (void)configureMapview
{
}
//添加大头针
- (void)configureAnnotationWithL
{
}
#pragma mark -
//大头针复用
- (MKAnnotationView
}
#pragma mark -
- (void)configureButton{
}
//
- (void)configureButtonEffectWit
}
//改变地图样式
- (void)changeMapViewTypeAction:(UIButton
}
//实现定位
- (void)ensureLocationAction:(UIButton
}
//
- (void)toZhengZhouAction:(UIButton
}
#pragma mark -
- (void)geoByLocationOrAddress{
}
//地理编码
- (void)getCoordinateByAddress:(NSString
}
//地理反编码(经纬度转化为地址)
- (void)getAddressByCoordinateWi
}
#pragma mark -
- (void)configureLocationManager
//
}
#pragma mark -
- (void)locationManager:(CLLocationManager
//
}
- (
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 (nonatomic) CLLocationCoordinate2D coordinate;
// Title and subtitle for use by selection UI.
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, strong)UIImage * image; //自定义大头针图片显示
@end
//
//
//
//
//
//
#import
#import
@interface
@property
// Title and subtitle for use by selection UI.
@property
@property
@property
@end
@end