高德地图添加大头针和周边搜索

1.集成高德地图:

在高德官网导入sdk,初始化地图

-(void)initMapView

{

    ///地图需要v4.5.0及以上版本才必须要打开此选项(v4.5.0以下版本,需要手动配置info.plist)

    [AMapServices sharedServices].enableHTTPS = YES;


    ///初始化地图

    _mapView = [[MAMapView alloc]init];

    _mapView.frame = CGRectMake(0, 0, ScreenWidth, _baseView.frame.size.height-45-60);

    _mapView.delegate = self;

    ///把地图添加至view

    [_baseView addSubview:_mapView];

//是否显示路况

        _mapView.showTraffic = YES;

    _mapView.showsLabels = YES;

    _mapView.showsIndoorMapControl = YES;

    [_mapView setZoomLevel:15 animated:YES];

    ///如果您需要进入地图就显示定位小蓝点,则需要下面两行代码

    _mapView.showsUserLocation = YES;

    _mapView.userTrackingMode = MAUserTrackingModeFollow;

    _mapView.logoCenter = CGPointMake(-455, _mapView.scaleOrigin.y);

    _mapView.scaleOrigin= CGPointMake(_mapView.scaleOrigin.x-55, _mapView.scaleOrigin.y);  //设置比例尺位置

    MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];

    r.showsAccuracyRing = YES;///精度圈是否显示,默认YES

    r.showsHeadingIndicator = YES;///是否显示方向指示(MAUserTrackingModeFollowWithHeading模式开启)。默认为YES

    // r.fillColor = [UIColor redColor];///精度圈 填充颜色, 默认 kAccuracyCircleDefaultColor

    // r.strokeColor = [UIColor blueColor];///精度圈 边线颜色, 默认 kAccuracyCircleDefaultColor

    // r.locationDotBgColor = [UIColor greenColor];///定位点背景色,不设置默认白色

    // r.locationDotFillColor = [UIColor grayColor];///定位点蓝色圆点颜色,不设置默认蓝色

    //r.image = [UIImage imageNamed:@"你的图片"]; ///定位图标, 与蓝色原点互斥

    _mapView.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

    [self addLongPressGestureRecognizer];

    [_mapView updateUserLocationRepresentation:r];

   [self removeLogo];

}

//去掉高德的logo

-(void)removeLogo

{

    [self.mapView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        if ([obj isKindOfClass:[UIImageView class]]) {

            UIImageView* logoM = obj;

            logoM.layer.contents = (__bridge id)[UIImage imageNamed:@""].CGImage;

        }

    }];

}

//注意设置地图缩放级别,如果不需要显示定位小蓝点但地图初始显示位置不对(可能表现为很多小格子),需要设置地图中心位置或显示区域

//长按地图某一点,添加大头针

-(void)addLongPressGestureRecognizer

{

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

    [_mapView addGestureRecognizer:addPin];

}

- (void)addPin:(UILongPressGestureRecognizer *)sender

{

    if (sender.state != UIGestureRecognizerStateBegan) {

        return;

    }


    CGPoint point = [sender locationInView:_mapView];

    //    通过地图类的对象  把点转换成经纬度

    CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:sender.view];

    [self setLocationWithCoordinate:coordinate];

}

- (void)setLocationWithCoordinate:(CLLocationCoordinate2D)coordinate{

   NSString *latitudeStr = [NSString stringWithFormat:@"%f",coordinate.latitude];

    NSString *longitudeStr = [NSString stringWithFormat:@"%f",coordinate.longitude];

    NSString* mapCoordinate = [NSStringstringWithFormat:@"%@,%@",latitudeStr,longitudeStr];

    //NSLog(@"%@",_mapCoordinate);

    //反编码 经纬度---->位置信息

    CLLocation *location=[[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];

    CLGeocoder *geocoder=[[CLGeocoder alloc]init];

    [geocoderreverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

        NSString*street;

        NSString*name;

        if(error) {

            NSLog(@"反编码失败:%@",error);

            [self.view makeToast:@"该网点经纬度信息有误,请重新标注!"];

            street  =@"该网点经纬度信息有误,请重新标注";

            name    =  @"未知地点";

        }else{

            //NSLog(@"反编码成功:%@",placemarks);

            CLPlacemark*placemark=[placemarkslastObject];

            //NSLog(@"%@",placemark.addressDictionary[@"FormattedAddressLines"]);

            NSDictionary*addressDic=placemark.addressDictionary;

            street=[addressDicobjectForKey:@"Street"];

            name=[addressDicobjectForKey:@"Name"];

        }


        MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];

        pointAnnotation.coordinate= coordinate;

        pointAnnotation.title= name;

        pointAnnotation.subtitle= street;

        [_mapViewaddAnnotation:pointAnnotation];


//设置该annotation选中状态

        [_mapView selectAnnotation:pointAnnotation animated:YES];

    }];

}

//当移动地图后设置地图回到当前用户位置

-(void)backToLocation

{

    self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;

}

#pragma mark 定位更新回调

-(void)mapView:(MAMapView*)mapView didUpdateUserLocation:(MAUserLocation*)userLocation

updatingLocation:(BOOL)updatingLocation

{

    if(updatingLocation)

    {

        //取出当前位置的坐标

        //        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);

    }

    _currentLocation = [userLocation.location copy];

    [self reGeoCoding];

    if (_firstLocate) {

//当第一次进入该页面,就搜索周边数据

        [self SearchPOI];

    }

}

-(void)SearchPOI{

    //周边搜索

    _POISearchManager = [[AMapSearchAPI alloc] init];

    _POISearchManager.delegate = self;

    _POISearchManager.timeout = 3;

    [self startSearchHandle];

}

-(void)startSearchHandle

{

    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];

    request.location            = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];

    request.keywords            =_keyworkds;//可以是@"地铁"等

    /* 按照距离排序. */

    request.sortrule            =0;

    request.requireExtension    =YES;

    request.radius=10000;

    [self.POISearchManager AMapPOIAroundSearch:request];

}

//周边搜索回调

- (void)onPOISearchDone:(AMapPOISearchBaseRequest*)request response:(AMapPOISearchResponse*)response

{

    [HUD hide];

    self.dataArray = [response.pois mutableCopy];

    if(response.pois.count==0)

    {

        NSString *alert = [NSString stringWithFormat:@"周边暂无%@信息",_keyworkds];

        [self.view showCenterToastWithMessage:alert];

        _addressLabel.text = @"";

        _routeLabel.text = @"";

        [self.tableView reloadData];

        return;

    }

    dispatch_async(dispatch_get_main_queue(), ^{

        AMapPOI *poi = self.dataArray[0];

        _addressLabel.text = poi.name;

        _routeLabel.text = poi.address;

        _clickLocation = [[CLLocation alloc]initWithLatitude:poi.location.latitude longitude:poi.location.longitude];;

        [self createAnnotationView];

        _firstLocate = NO;

        [self.tableView reloadData];

    });

}

//添加大头针

-(void)createAnnotationView

{

    [_mapView removeAnnotations:_mapView.annotations];

    NSMutableArray *array = [[NSMutableArray alloc]init];

    for(inti =0; i

        AMapPOI*poi =self.dataArray[i];

        MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];

        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude);

        pointAnnotation.coordinate= coordinate;

        pointAnnotation.title= poi.name;

        pointAnnotation.subtitle= poi.address;

        [arrayaddObject:pointAnnotation];

       // [_mapView addAnnotation:pointAnnotation];

    }

    [_mapView addAnnotations:array];

}

- (MAAnnotationView*)mapView:(MAMapView*)mapView viewForAnnotation:(id)annotation

{


    if ([annotation isKindOfClass:[MAPointAnnotation class]])

    {

        if ([annotation isKindOfClass:[MAUserLocation class]]) {

            returnnil;

        }

        static NSString *reuseIndetifier = @"annotationReuseIndetifier";

        MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];

        if(annotationView ==nil)

        {

            annotationView = [[MAAnnotationViewalloc]initWithAnnotation:annotation

                                                          reuseIdentifier:reuseIndetifier];

        }

        UIImage*image;

        if(annotation.coordinate.latitude == _clickLocation.coordinate.latitude){

            image = [UIImage imageNamed:@"clickAnnotation"];

        }else  {

            image = [UIImage imageNamed:@"annotationView"];

        }

        annotationView.image= image;

        //设置中心点偏移,使得标注底部中间点成为经纬度对应点

        annotationView.centerOffset=CGPointMake(0, -18);

        returnannotationView;

    }

    return nil;

}

//点击大头针的方法

- (void)mapView:(MAMapView*)mapView didSelectAnnotationView:(MAAnnotationView*)view

{

    _clickLocation = [[CLLocation alloc]initWithLatitude:view.annotation.coordinate.latitude longitude:view.annotation.coordinate.longitude];

    NSArray * array = [NSArray arrayWithArray:_mapView.annotations];

    for(inti=0; i

    {

        if (view.annotation.coordinate.latitude == ((id<MAAnnotation>)array[i]).coordinate.latitude)

        {

            //获取到当前的大头针 你可以执行一些操作

            if ([array[i] isKindOfClass:[MAPointAnnotation class]]) {

                MAPointAnnotation*annotation = array[i];

                _addressLabel.text= annotation.title;

                _routeLabel.text= annotation.subtitle;

            }

        }

    }

//添加大头针前需要先remove一次

    [_mapView removeAnnotations:_mapView.annotations];

    [_mapView addAnnotations:array];

}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,284评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,115评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,614评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,671评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,699评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,562评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,309评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,223评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,668评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,859评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,981评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,705评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,310评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,904评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,023评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,146评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,933评论 2 355

推荐阅读更多精彩内容