//
// ViewController.m
// MapDemo
//
// Created by 森屿猫 on 2018/10/11.
// Copyright © 2018 森屿猫. All rights reserved.
//
#import “ViewController.h”
#import “MapViewcontroller.h”
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSArray *_provinceArr;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//给一个初始值
_provinceArr = @[@“河北石家庄”,@“北京”,@“山西太原”,@“山东济南”,@“陕西西安”];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _provinceArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = _provinceArr[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//得到选中cell的对应文本
NSString *pro = _provinceArr[indexPath.row];
MapViewcontroller *mapVC = [[MapViewcontroller alloc]init];
mapVC.passProvince = pro;
[self.navigationController pushViewController:mapVC animated:YES];
}
@end
//
// MapViewcontroller.h
// MapDemo
//
// Created by 森屿猫 on 2018/10/11.
// Copyright © 2018 森屿猫. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MapViewcontroller : UIViewController
@property(nonatomic,copy)NSString *passProvince;
@end
NS_ASSUME_NONNULL_END
//
// MapViewcontroller.m
// MapDemo
//
// Created by 森屿猫 on 2018/10/11.
// Copyright © 2018 森屿猫. All rights reserved.
//
#import “MapViewcontroller.h”
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewcontroller ()<CLLocationManagerDelegate,MKMapViewDelegate>
@property(nonatomic,strong)MKMapView *mapview;
@property(nonatomic,strong)CLLocationManager *locManager;//地理位置管理者
@end
@implementation MapViewcontroller
-
(void)viewDidLoad {
[super viewDidLoad];//获取当前位置
self.navigationItem.title = self.passProvince;
self.mapview = [[MKMapView alloc]initWithFrame:self.view.frame];
self.mapview.mapType = MKMapTypeHybrid;//设置平面+卫星的混合样式
self.mapview.mapType = MKMapTypeStandard;
[self.view addSubview:self.mapview];//获取当前位置的按钮
UIButton *currentbtn = [UIButton buttonWithType:UIButtonTypeCustom];
currentbtn.frame = CGRectMake(10,self.view.frame.size.height - 80,40,40);
[currentbtn setTitle:@“定位” forState:UIControlStateNormal];
[currentbtn setBackgroundColor:[UIColor blueColor]];
[currentbtn addTarget:self action:@selector(currentBtnDidPress:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:currentbtn];//实例化位置管理对象
self.locManager = [[CLLocationManager alloc]init];
self.locManager.delegate = self;//设置代理//添加右侧按钮 用于获取省会位置
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@“省会定位” style:UIBarButtonItemStylePlain target:self action:@selector(getProvinceLocation:)];//申请用户授权
[self.locManager requestWhenInUseAuthorization];
}
//获取h省会位置
-(void)getProvinceLocation:(id)sender{
CLGeocoder *g = [[CLGeocoder alloc]init];
//地址解析,将这样一个地址字符串转换为位置的经纬度
[g geocodeAddressString:self.passProvince completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//随便获取一个位置 展现在地图上
CLPlacemark *place = [placemarks lastObject];
//获取位置
CLLocation *loc = place.location;
//获取经纬度
CLLocationCoordinate2D coor = loc.coordinate;
//定义矛点 添加到地图上
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
//
annotation.coordinate = coor;
//
dispatch_async(dispatch_get_main_queue(), ^{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coor, 800, 800);
//显示区域变化
[self.mapview setRegion:region animated:YES];
[self.mapview addAnnotation:annotation];
});
}];
}
//获取按钮触发
-(void)currentBtnDidPress:(id)sender{
//开始获取位置信息 调用此方法后 协议中的方法才会执行
[self.locManager startUpdatingLocation];
}
#pragma mark ---- CLLocationManagerDelegat ----
//获取当前位置信息后触发回调方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
//获取当前位置
CLLocation *curLoc = [locations lastObject];
//获取经纬度
CLLocationCoordinate2D curCoor = curLoc.coordinate;
NSLog(@"经度:%g,纬度:%g",curCoor.longitude,curCoor.latitude);
manager.delegate = nil;
//让地图区域变小
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(curCoor, 300, 300);
[self.mapview setRegion:region animated:YES];
//地址解析
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:curLoc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//获取其中一个
CLPlacemark *place = [placemarks lastObject];
//做一个大头针 弄在当前位置
MKPointAnnotation *point = [[MKPointAnnotation alloc]init];
point.title = [NSString stringWithFormat:@"我的位置:"];
point.coordinate = curCoor;//大头针位置
[self.mapview addAnnotation:point];
}];
}
//用于设置矛点试图的方法
-
(nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation;{
//做一个静态字符串,作为静态父用标志
static NSString *iden = @“pin”;
//congmapview可复用的矛点中找一块可复用的内存
MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:iden forAnnotation:annotation];//如果可复用内存为空,实例化新内存,并给他添加复用标志
if (pin == nil) {
pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:iden];
}
//设置掉落效果
pin.animatesDrop = YES;
//设置泡泡效果
pin.canShowCallout = YES;
return pin;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end