iOS程序员之高德地图SDK

来源:互联网 发布:图像处理十大经典算法 编辑:程序博客网 时间:2024/04/29 18:10

高德SDK

最近项目中需要定位客户位置,要满足地址搜索,长按地图添加,同时大头针还要能移动的需求,这里整理下,希望帮助有需要的人

1.添加地图

    // 地图    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, lineView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-lineView.bottom-kViewMargin)];    _mapView.showsCompass = NO; // 隐藏指南针    _mapView.showsScale = NO; // 隐藏比例尺    _mapView.delegate = self;    [_mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES]; // 追踪用户位置    [self.view addSubview:_mapView];

2.搜索按钮点击事件

// 搜索按钮事件- (void)actionSearchAddress:(UIButton *)button{    [self.view endEditing:YES]; // 结束编辑回收键盘    [[CustomActivityIndicator sharedActivityIndicator] startSynchAnimating];        [_mapView removeAnnotation:_annotation]; // 移除大头针        //构造AMapGeocodeSearchRequest对象,address为必选项,city为可选项    AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];    geo.address = _searchContentTF.text;        //发起正向地理编码    [_mapSearch AMapGeocodeSearch: geo];}

3.高德地图代理方法(3.0以上的高德SDK包含点击,长按等地图手势的代理方法)

#pragma mark - MAMapViewDelegate// 大头针样式- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation{    if ([annotation isKindOfClass:[ReGeocodeAnnotation class]])    {        static NSString *pointReuseIndentifier = @"SearchAdressAnnotationView";        MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];        if (annotationView == nil)        {            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];        }        annotationView.canShowCallout= YES;       //设置气泡可以弹出,默认为NO        annotationView.animatesDrop = YES;        //设置标注动画显示,默认为NO        annotationView.draggable = YES;        //设置标注可以拖动,默认为NO        annotationView.pinColor = MAPinAnnotationColorPurple;                // 添加点到数值中,便于改变时移除//        if (!_annotationsArray) {//            _annotationsArray = [NSMutableArray array];//        }//        [_annotationsArray addObject:annotation];//        _annotation = annotation;                return annotationView;    }    return nil;}// 单击地图- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate{    [self.view endEditing:YES];}// 拖拽大头针触发方法- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState fromOldState:(MAAnnotationViewDragState)oldState{    if (newState == MAAnnotationViewDragStateEnding)    {        _isSearchFromDragging = YES;                CLLocationCoordinate2D coordinate = view.annotation.coordinate;        self.annotation = view.annotation;                [self searchReGeocodeWithCoordinate:coordinate];    }}// 长按触发方法- (void)mapView:(MAMapView *)mapView didLongPressedAtCoordinate:(CLLocationCoordinate2D)coordinate{    [self.mapView removeAnnotation:_annotation];    _isSearchFromDragging = NO;    [self searchReGeocodeWithCoordinate:coordinate];}

4.长按和移动大头针时调用逆地理编码方法

// 逆地址编码搜索- (void)searchReGeocodeWithCoordinate:(CLLocationCoordinate2D)coordinate{    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];        regeo.location = [AMapGeoPoint locationWithLatitude:coordinate.latitude longitude:coordinate.longitude];    regeo.requireExtension            = YES;        [_mapSearch AMapReGoecodeSearch:regeo];}

5.高德search的代理方法

#pragma mark - AMapSearchDelegate//实现正向地理编码的回调函数- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response{    if(response.geocodes.count == 0)    {        return;    } else {                // 通过AMapGeocodeSearchResponse对象处理搜索结果, 只要一个位置        AMapGeocode *p = response.geocodes[0];//        NSLog(@"%f,,%f",p.location.latitude, p.location.longitude);        ReGeocodeAnnotation *pointAnnotation = [[ReGeocodeAnnotation alloc] init];        pointAnnotation.coordinate = CLLocationCoordinate2DMake(p.location.latitude, p.location.longitude);        pointAnnotation.title = _searchContentTF.text;        [_mapView addAnnotation:pointAnnotation];                _searchContentTF.text = pointAnnotation.title;        // 保存点        _annotation = pointAnnotation;        [self.mapView selectAnnotation:self.annotation animated:YES];        // 改变地图的中心点为大头针的中心点        MACoordinateRegion theRegion = {{0.0, 0.0 }, { 0.0, 0.0 }};        theRegion.center = CLLocationCoordinate2DMake(p.location.latitude, p.location.longitude);        theRegion.span.longitudeDelta = 0.05f;        theRegion.span.latitudeDelta = 0.05f;        [_mapView setRegion:theRegion animated:YES];    }        [[CustomActivityIndicator sharedActivityIndicator] stopSynchAnimating];}// 逆地理编码回调.- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response{    if (response.regeocode != nil && _isSearchFromDragging == NO)    {        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(request.location.latitude, request.location.longitude);        ReGeocodeAnnotation *reGeocodeAnnotation = [[ReGeocodeAnnotation alloc] initWithCoordinate:coordinate reGeocode:response.regeocode];                [self.mapView addAnnotation:reGeocodeAnnotation];        _annotation = reGeocodeAnnotation;        _searchContentTF.text = reGeocodeAnnotation.title;        [self.mapView selectAnnotation:reGeocodeAnnotation animated:YES];    }    else /* from drag search, update address */    {        [self.annotation setAMapReGeocode:response.regeocode];        _searchContentTF.text = [NSString stringWithFormat:@"%@%@%@%@", response.regeocode.addressComponent.province?: @"", response.regeocode.addressComponent.city ?: @"", response.regeocode.addressComponent.district?: @"", response.regeocode.addressComponent.township?: @""];        [self.mapView selectAnnotation:self.annotation animated:YES];    }}

6.ReGeocodeAnnotation是自定义的类,代码如下:

ReGeocodeAnnotation.h

#import <MAMapKit/MAMapKit.h>#import <AMapSearchKit/AMapCommonObj.h>@interface ReGeocodeAnnotation : NSObject <MAAnnotation>- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate reGeocode:(AMapReGeocode *)reGeocode;@property (nonatomic, readonly, strong) AMapReGeocode *reGeocode;@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;@property (nonatomic, copy) NSString *title;@property (nonatomic, copy) NSString *subtitle;- (void)setAMapReGeocode:(AMapReGeocode *)reGerocode;@end
ReGeocodeAnnotation.m

#import "ReGeocodeAnnotation.h"@interface ReGeocodeAnnotation ()@property (nonatomic, readwrite, strong) AMapReGeocode *reGeocode;@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;@end@implementation ReGeocodeAnnotation@synthesize reGeocode  = _reGeocode;@synthesize coordinate = _coordinate;- (void)setAMapReGeocode:(AMapReGeocode *)reGerocode{    self.reGeocode = reGerocode;        [self updateContent];}- (void)updateContent{    /* 包含 省, 市, 区以及乡镇.  */    self.title = [NSString stringWithFormat:@"%@%@%@%@",                  self.reGeocode.addressComponent.province?: @"",                  self.reGeocode.addressComponent.city ?: @"",                  self.reGeocode.addressComponent.district?: @"",                  self.reGeocode.addressComponent.township?: @""];    /* 包含 社区,建筑. *///    self.subtitle = [NSString stringWithFormat:@"%@%@",//                     self.reGeocode.addressComponent.neighborhood?: @"",//                     self.reGeocode.addressComponent.building?: @""];}#pragma mark - Life Cycle- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate reGeocode:(AMapReGeocode *)reGeocode{    if (self = [super init])    {        self.coordinate = coordinate;        self.reGeocode  = reGeocode;        [self updateContent];    }        return self;}@end

有时可能会需要修改地图显示范围,方法如下:

- (void)figureMapRegion{    if (_allPointArray.count == 1) { // 一个点        MACoordinateRegion theRegion = {{0.0, 0.0 }, { 0.0, 0.0 }};        theRegion.center = CLLocationCoordinate2DMake([_allPointArray[0][@"latitude"] doubleValue], [_allPointArray[0][@"longitude"] doubleValue]);        theRegion.span.longitudeDelta = 0.05f;        theRegion.span.latitudeDelta = 0.05f;        [_mapView setRegion:theRegion animated:YES];    }        if (_allPointArray.count > 1) { // 多个点        //构造多边形数据对象        CLLocationCoordinate2D polylineCoords;        double maxLat = 0;        double maxLon = 0;        double minLat = 0;        double minLon = 0;               for (int i=0; i<_allPointArray.count; i++) {            NSDictionary *dic = _allPointArray[i];            polylineCoords.latitude = [dic[@"latitude"] doubleValue];            polylineCoords.longitude = [dic[@"longitude"] doubleValue];                        if (i==0) {                maxLat=[dic[@"latitude"] doubleValue];                minLat=[dic[@"latitude"] doubleValue];                maxLon=[dic[@"longitude"] doubleValue];                minLon=[dic[@"longitude"] doubleValue];            } else {                if (maxLat<[dic[@"latitude"] doubleValue]) {                    maxLat=[dic[@"latitude"] doubleValue];                }                if (minLat>[dic[@"latitude"] doubleValue]) {                    minLat=[dic[@"latitude"] doubleValue];                }                if (maxLon<[dic[@"longitude"] doubleValue]) {                    maxLon=[dic[@"longitude"] doubleValue];                }                if (minLon>[dic[@"longitude"] doubleValue]) {                    minLon=[dic[@"longitude"] doubleValue];                }            }        }                // 设置地图范围及中心        [self setMapRegionAndCenterWithMaxLatitude:maxLat minLatitude:minLat maxLongitude:maxLon minLongitude:minLon];    }    }// 设置地图的范围- (void)setMapRegionAndCenterWithMaxLatitude:(double)maxLat minLatitude:(double)minLat maxLongitude:(double)maxLon minLongitude:(double)minLon{    CLLocationCoordinate2D center = CLLocationCoordinate2DMake((maxLat+minLat)/2, (maxLon+minLon)/2);    MACoordinateSpan span = MACoordinateSpanMake((maxLat-minLat)*1.5, (maxLon-minLon)*1.5);    MACoordinateRegion regin = MACoordinateRegionMake(center, span);    [_mapView setRegion:regin animated:YES];}






1 0
原创粉丝点击