美文网首页iOS OC
iOS 高德地图以及定位

iOS 高德地图以及定位

作者: 流年小书 | 来源:发表于2018-06-28 14:05 被阅读13次

    AppDelegate中

    导入头文件
    #import <AMapFoundationKit/AMapFoundationKit.h>
    #import <AMapLocationKit/AMapLocationKit.h>
    
    填入申请好的apikey
    [AMapServices sharedServices].apiKey = @"a823a5d1e3b7fwdnnfwenfweoi";
    

    初始化高德地图

       //地图初始化
        self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight)];
        self.mapView.backgroundColor = [UIColor whiteColor];
        self.mapView.delegate = self;
        //设置定位精度
        //    self.mapView.desiredAccuracy = kCLLocationAccuracyBest;
        //设置定位距离
        //    self.mapView.distanceFilter = 5.0f;
        self.mapView.zoomEnabled = YES;
        //普通样式
        self.mapView.mapType = MAMapTypeStandard;
        //地图跟着位置移动
        [self.mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];
        //设置成NO表示关闭指南针;YES表示显示指南针
        self.mapView.showsCompass= NO;
        //设置指南针位置
        self.mapView.compassOrigin= CGPointMake(_mapView.compassOrigin.x, 22);
        //设置成NO表示不显示比例尺;YES表示显示比例尺
        self.mapView.showsScale= NO;
        //设置比例尺位置
        self.mapView.scaleOrigin= CGPointMake(_mapView.scaleOrigin.x, 22);
        //缩放等级
        [self.mapView setZoomLevel:16 animated:YES];
        //开启定位
        self.mapView.showsUserLocation = YES;
        [cell.contentView addSubview:self.mapView];
        [self.mapView  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(cell.contentView);
            make.left.equalTo(cell.contentView);
            make.right.equalTo(cell.contentView);
            make.bottom.equalTo(cell.commitButton.mas_top).with.offset(-30);
        }];
    

    根据输入的地址获取经纬度,并让地图定位其位置

    地理编码和反地理编码
     CLGeocoder *geocoder = [[CLGeocoder alloc] init];
     [geocoder geocodeAddressString:self.addtress completionHandler:^(NSArray *placemarks, NSError *error){
            if ([placemarks count] > 0) {
                CLPlacemark *placemark = [placemarks objectAtIndex:0];
                CLLocationCoordinate2D coordinate = placemark.location.coordinate;
                
                NSString *strCoordinate = [NSString stringWithFormat:@"经度:%3.5f 纬度:%3.5f:",coordinate.latitude,coordinate.longitude ];
                self.coordinate = coordinate;
                //根据经纬度获取地图位置
                MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
                annotation.coordinate = self.coordinate;
                annotation.title = @"你好";
                [self.mapView addAnnotation:annotation];
                [self.mapView setRegion:MACoordinateRegionMakeWithDistance(self.coordinate, 5000, 5000)];
            }
    }];
    

    相关文章

      网友评论

        本文标题:iOS 高德地图以及定位

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