IOS开发 使用地图 MapKit

因为有个项目要在地图中显示位置,所以用到了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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值