美文网首页
MapView 地图参考

MapView 地图参考

作者: 羊子_ | 来源:发表于2017-10-25 20:28 被阅读0次

    #import "ViewController.h"

    #import "MapKit/MapKit.h"

    @interface ViewController ()<MKMapViewDelgate>

    {UITextField *latitudeTF;

    UITextField *longtitudeTF;

    }

    @property(nonatomic,strong)MKMapView *mapview;

    @property(nonatomic,strong)CLGeocoder *geocoder;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

    [super viewDidLoad];

    // 初始化

    self.geocoder = [[CLGeocoder alloc]init];

    // 初始化地图

    self.mapview = [[MKMapView alloc]initWithFrame:self.view.frame];

    // 设置属性

    // 设置标准地图

    self.mapview.mapType = MKMapTypeStandard;

    // 设置地图可滚动

    self.mapview.scrollEnabled = YES;

    // 展示用户当前显示位置

    self.mapview.showsUserLocation = YES;

    // 设置代理

    self.mapview.delegate = self;

    // 设置允许缩放

    self.mapview.zoomEnabled = YES;

    // 添加到视图上

    [self.view addSubview:_mapview];

    // 设置地图显示的经纬度为39.5427 经度为116.2317

    [self locateToLatitude:39.5427 longtitude:116.2317];

    // 设置长按手势

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

    [self.mapview addGestureRecognizer:longPress];

    // 创建纬度Label

    UILabel *latitudeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 20, 40, 30)];

    latitudeLabel.text = @"纬度";

    // 添加到视图上

    [self.mapview addSubview:latitudeLabel];

    // 创建纬度文本框

    latitudeTF = [[UITextField alloc]initWithFrame:CGRectMake(45, 20, 100, 30)];

    latitudeTF.text = @"23.12672";

    latitudeTF.borderStyle = UITextBorderStyleRoundedRect;

    // 数字键盘

    latitudeTF.keyboardType = UIKeyboardTypeNumberPad;

    // 添加到视图上

    [self.mapview addSubview:latitudeTF];

    // 创建经度Label

    UILabel *longtitudeLabel = [[UILabel alloc]initWithFrame:CGRectMake(145, 20, 40, 30)];

    longtitudeLabel.text = @"经度";

    // 添加到视图上

    [self.mapview addSubview:longtitudeLabel];

    // 创建经度文本框

    longtitudeTF = [[UITextField alloc]initWithFrame:CGRectMake(185, 20, 100, 30)];

    longtitudeTF.text = @"113.395";

    longtitudeTF.borderStyle = UITextBorderStyleRoundedRect;

    // 数字键盘

    longtitudeTF.keyboardType = UIKeyboardTypeNumberPad;

    // 添加到视图上

    [self.mapview addSubview:longtitudeTF];

    // 创建按钮

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame =CGRectMake(285, 20, 40, 30);

    [btn setTitle:@"GO" forState:UIControlStateNormal];

    // 添加点击事件

    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

    [self.mapview addSubview:btn];

    CGFloat width = self.view.frame.size.width;

    NSLog(@"%f",width);

    }

    // 前往另一个地图界面

    -(void)click

    {

    [latitudeTF resignFirstResponder];

    [longtitudeTF resignFirstResponder];

    //经度

    NSString *latitudeStr = latitudeTF.text;

    //纬度

    NSString *longtitudeStr = longtitudeTF.text;

    //如果用户输入的经度、纬度为空

    if (latitudeStr != nil && latitudeStr.length > 0

    && longtitudeStr!= nil && longtitudeStr.length > 0)

    {

    //设置经度、纬度

    [self locateToLatitude:latitudeStr.floatValue longtitude:longtitudeStr.floatValue];

    }

    }

    -(void)locateToLatitude:(CGFloat)latitude longtitude:(CGFloat)longitude{    //设置地图中的的经度、纬度    CLLocationCoordinate2D center = {latitude,longitude};    //设置地图显示的范围    MKCoordinateSpan span;    //地图显示范围越小,细节越清楚;    span.latitudeDelta = 0.01;    span.longitudeDelta = 0.01;    //创建MKCoordinateRegion对象,该对象代表地图的显示中心和显示范围    MKCoordinateRegion region = {center,span};    //设置当前地图的显示中心和显示范围    [self.mapview setRegion:region animated:YES];    // 设置一个固定的锚点    MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];    // 设置显示的标题    annotation.title = @"一个新的地点";    annotation.subtitle  = @"我喜欢的地方";    // 添加位置    CLLocationCoordinate2D coordinate = {latitude,longitude};    annotation.coordinate = coordinate;    // 添加锚点到视图上    [self.mapview addAnnotation:annotation];}// 设置长按手势的触发方法-(void)longPress:(UILongPressGestureRecognizer *)press{    // 定义坐标值    CGPoint cp = [press locationInView:self.mapview];    // 根据坐标获取经纬度    CLLocationCoordinate2D coordinate = [self.mapview convertPoint:cp toCoordinateFromView:self.mapview];    // 将经纬度包装成CLLocation对象    CLLocation *location = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];    // 根据经纬度反向解析地址    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {                if (placemarks.count > 0) {            CLPlacemark *placemark = placemarks[0];            // 获取地址信息对应的地址详情            NSArray *addrArr = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];            NSMutableString *addreStr = [[NSMutableString alloc]init];            for (int i = 0; i)annotation

    {

    static NSString *annoID = @"FKAnno";

    // 设置可重用的锚点控件

    MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoID];

    if (!annoView) {

    annoView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annoID];

    }

    // 为锚点添加图片

    annoView.image = [UIImage imageNamed:@"pos.gif"];

    // 设置该控件是否显示气泡

    annoView.canShowCallout = YES;

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    [btn addTarget:self action:@selector(goclick) forControlEvents:UIControlEventTouchUpInside];

    annoView.rightCalloutAccessoryView = btn;

    // 返回annoView

    return annoView;

    }

    -(void)goclick

    {

    NSLog(@"您点击了锚点信息");

    }

    相关文章

      网友评论

          本文标题:MapView 地图参考

          本文链接:https://www.haomeiwen.com/subject/gvlluxtx.html