版权声明:本文为博主原创文章,未经博主允许不得转载。
因为有个项目要在地图中显示位置,所以用到了MapKit。
记录下来,以免以后忘记。
#import "LWViewController.h"
#import <MapKit/MapKit.h>
@interface LWViewController ()<MKMapViewDelegate>
//设置纬度文本框
@property (strong, nonatomic) IBOutlet UITextField *LatitudeTF;
//设置经度文本框
@property (strong, nonatomic) IBOutlet UITextField *longitudeTF;
//设置地图视图
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic,strong) CLGeocoder *geocoder;
@end
@implementation LWViewController
- (void)viewDidLoad {
[super viewDidLoad];
//模块一
//创建提示框视图
UIAlertView *alerV = [[UIAlertView alloc] initWithTitle:nil message:@"是否希望显示当前的位置" delegate:nil cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
//显示提示框
[alerV show];
//设置显示当前位置
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
//设置中心位置和范围
[self locateToLatitude:39.905 longitude:116.405];
//创建解析地址对象
self.geocoder = [[CLGeocoder alloc] init];
}
//显示当前位置中心和范围
-(void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude
{ //设置中心位置
CLLocationCoordinate2D cencer = {latitude,longitude};
//设置显示范围
MKCoordinateSpan span;
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
//创建MKCoordinateRegion对象
MKCoordinateRegion region = {cencer,span};
//在地图设置
[self.mapView setRegion:region];
//添加锚点
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
//添加标题
annotation.title = @"北京八维";
annotation.subtitle = @"北京海淀区软件园南站";
CLLocationCoordinate2D coor = {longitude,latitude};
annotation.coordinate = coor;
//加入到地图中
[self.mapView addAnnotation:annotation];
}
//自定义锚点代理方法
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{ static NSString *ka = @"me";
MKAnnotationView *annotionV = [mapView dequeueReusableAnnotationViewWithIdentifier:ka];
if(annotionV == nil)
{
annotionV = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ka];
}
annotionV.image = [UIImage imageNamed:@"地图.png"];
annotionV.canShowCallout = YES;
return annotionV;
}
//根据经、纬度显示当前位置
- (IBAction)clickGo:(id)sender {
if(self.LatitudeTF.text.length>0&&self.longitudeTF.text.length>0){
CLLocation *location = [[CLLocation alloc] initWithLatitude:[self.LatitudeTF.text floatValue] longitude:[self.longitudeTF.text floatValue]];
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placemark = placemarks[0];
NSLog(@"==%@==",[placemark.addressDictionary objectForKey:@"FormattedAddressLines"][0]);
[self locateToLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
NSLog(@"纬度:%g,经度:%g",location.coordinate.latitude,location.coordinate.longitude);
//119.021 33.5975
}];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#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