//
//
//
//
//
on 15/8/22.
//
年
congWang
. All rights reserved.
//
#import
"RootViewController.h"
#import
#import
"KCAnnotinate.h"
#import
//
实现地理编码和反编码
,
以及定位功能
@interface
RootViewController
()<</span>MKMapViewDelegate, CLLocationManagerDelegate>
@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 configureAnnotationWithLocationCoordinate2D:self.coofdinate];
self.mapView.delegate = self;
//实现地理编码和反编码
[self geoByLocationOrAddress];
//实现定位
[self configureLocationManager];
}
#pragma mark - 添加地图 -
- (void)configureMapview
{
self.mapView= [[MKMapView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.coofdinate = CLLocationCoordinate2DMake(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)configureAnnotationWithLocationCoordinate2D:(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 dequeueReusableAnnotationViewWithIdentifier: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 * buttonToChangeMapViewType = [UIButton buttonWithType:UIButtonTypeCustom];
buttonToChangeMapViewType.backgroundColor = [UIColor colorWithRed:31 / 255.0 green:31 / 255.0 blue:31 / 255.0 alpha:0.5];
[buttonToChangeMapViewType setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[buttonToChangeMapViewType setTitle:@"平面地图" forState:UIControlStateNormal];
buttonToChangeMapViewType.frame = CGRectMake(375 / 3, 667 - 40, 375 / 3, 40);
[buttonToChangeMapViewType addTarget: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 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: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 whiteColor] forState:UIControlStateNormal];
[buttonToZhengZhou setTitle:@"郑州" forState:UIControlStateNormal];
buttonToZhengZhou.frame = CGRectMake(0, 667 - 40, 375 / 3, 40);
[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.75797500, 113.66541200); //经度
self.region = MKCoordinateRegionMake(_coofdinate, _span);
self.mapView.region = self.region;
//由于大头针是复用的,如果离开郑州,会丢失大头针,需要重新添加
[self configureAnnotationWithLocationCoordinate2D:self.coofdinate];
}
#pragma mark - 地理编码和反编码 -
- (void)geoByLocationOrAddress{
//地理编码
[self getCoordinateByAddress:@"中国湖北省十堰市郧西县六郎乡"];
//地理反编码
CLLocation * location = [[CLLocation alloc] initWithLatitude:33 longitude:110];
[self getAddressByCoordinateWithLocation: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)getAddressByCoordinateWithLocation:(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] == kCLAuthorizationStatusNotDetermined) {
[self.locationManager requestAlwaysAuthorization]; //为了验证,填写一直定位
}
}
_locationManager.delegate = self;
//定位精准度
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//设置定位精准度
CLLocationDistance distance = 10.0;
_locationManager.distanceFilter = distance;
//启动跟踪定位
//
}
#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 alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[self getAddressByCoordinateWithLocation:location];
}
- (
void
)didReceiveMemoryWarning {
super
didReceiveMemoryWarning
];
// Dispose of any resources that can be recreated.
}
//
//
//
//
// 华子 on 15/8/22.
// 年 华子. 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
@end
ios高德地图地理编码 ios默认地图修改 高德
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
ios高德地图视频教程 高德地图 ios
高德地图iOS 定位 SDK V2.6.7 2020-08-281、适配iOS14定位权限;新增“模糊定位”权限下的兼容策略;2、修复bug,提升性能和稳定性。高德地图iOS 定位 SDK V2.6
ios高德地图视频教程 ios 高德获取定位 iOS 高德地图 字段 -
java怎么实现 自动识别地址的邮编
java编码处理:(java在内存中使用的是uniocode编码)一、编码基本知识:1、iso8859-1 单字节编码 范围0-255 优点:和计算机最基础的表示单位一致2、GBK/GB2312 双字节编码 &
java怎么实现 自动识别地址的邮编 Java Tomcat JSP 网络协议