LoginSignup
2
2

More than 5 years have passed since last update.

地图与定位

Last updated at Posted at 2015-07-30
    //导入头文件
    #import <MapKit/MapKit.h>
    //1.建立mapView为属性
    @property (nonatomic, strong) MKMapView *mapView;

    //2.viewDidLoad方法里初始化地图视图
    - (void) viewDidLoad {
        [super viewDidLoad];
        //初始化地图视图
        self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];

        //创建坐标, 第一个参数纬度,第二个参数经度
        CLLocationCoordinate2D cc2d = CLLocationCoordinate2DMake(30.666621, 104.0);
        //创建比例尺大小.第一个参数是纬度比例尺,第二个参数是经度比例尺。
        MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
        //创建要显示的区域(位置和大小).第一个参数是坐标,第二个是比例尺。
        MKCoordinateRegion region = MKCoordinateRegionMake(cc2d, span);
        //让地图视图显示这个区域
        [self.mapView setRegion:region];

        //把地图视图加载到view上
        [self.tableView addSubview: self.mapView];

        //位置追踪
        //导入头文件
        #import <CoreLocation/CoreLocation.h>
        //创建一个位置管理器属性
        @property (nonatomic, strong) CLLocationManager *locManager;
        //初始化管理器
        self.locManager = [[CLLocationManager alloc] init];
        //设置地图刷新距离。多少米刷新一次 10m
        [self.locManager setDistanceFilter:10.0];
        //申请总是使用定位的用户授权
        [self.locManager requestAlwaysAutorization];
#ps.申请使用时允许定位授权是 requestWhenInUseAuthorization.
        //刷新地图位置
        [self.locManager startUpdatingLocation];
        //这里需要回调一个协议里的方法,所以需要添加协议
        <CLLocationManagerDelegate>
        //
        [self.locManager setDelegate:self];

    }
//回调方法的实现:
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    //获取数组里的第一个位置就是现在的位置
    CLLocation *location = [locations firstObject];
    //在工程plist添加如下内容:<key>NSLocationAlwaysUsageDescription</key>
    <true/>
    //把显示区域设置为当前需要显示的区域
    [self.mapView setRegion:MKCoordinateRegionMake(location.coordinate, MKCoordinateSpan(0.01, 0.01))];
}

    //地图上设置大头针
    //先给地图视图添加一个长按手势操作,当长按后添加一个大头针
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self selector:@selector(longPress:)];
    //把手势操作添加到地图视图上
    [self.mapView addGestureRecognizer:longPress];

    //设置长按回调方法
- (void) longPress:(UILongPressGestureRecognizer *)longPress {
    //获取用户点击地点的视图位置
    CGPoint point = [longPress locationInView:self.mapView];
    //将视图位置转化为地图坐标
    CLLocationCoordinate2D c2d = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    //加个判定,当长按手势开始时才添加大头针
    if (longPress.state == UIGestureRecognizerStateBegan) {
    //初始化一个大头针
        MKPointAnnotation *anno = [[MKPointAnnotation alloc] init];
        //设置大头针位置
        [anno setCoordinate:c2d];
        //设置大头针标题
        [anno setTitle:@"主标题"];
        [anno setSubTitle:@"副标题"];
        //将大头针加载到地图上
        [self.mapView addAnnotation:anno];
    }

}
    //设置大头针定制的回调方法
    //这个回调方法需要协议MKMapViewDelegate,所以添加协议<MKMapViewDelegate>
    //[self.mapView setDelegate:self];
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>) annotation {
    //先从复用池去取大头针,没有再创建,相似于tableViewCell
    MKPinAnnotationView *pin = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN"];
    if (!pin) {
        pin = [[MKPinAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN"];
    }
    //大头针定制
    //大头针下落出现动画
    [pin setAnimatesDrop:YES];
    //大头针气泡显示信息
    [pin setCanShowCallout:YES];
    //自定义气泡的左右视图
    //左视图
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"93690_1419384339dZGB.jpg"]];
    [imageView setFrame:CGRectMake(0, 0, 50, 50)];
    [pin setLeftCalloutAccessoryView:imageView];
    //返回pin
    return pin;
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2