高德地圖maker點聚合以及周邊推薦

gif5新文件.gif

當移動地圖后屏幕內(nèi)沒有maker時,出現(xiàn)指引的懸浮按鈕,效果還是很酷炫的。

實現(xiàn)原理就是計算出屏幕中心和maker形成的夾角a,然后根據(jù)屏幕寬高比形成的夾角b和a作比較,把maker分為上下左右四組,再分別選出四組里離當前屏幕中心最近的點,即最多在屏幕的上下左右各懸浮一個指引按鈕

根據(jù)形成的夾角a將maker分為上下左右四組

    NSMutableArray *topArr   = [NSMutableArray array];
    NSMutableArray *leftArr  = [NSMutableArray array];
    NSMutableArray *botArr   = [NSMutableArray array];
    NSMutableArray *rightArr = [NSMutableArray array];
    
    for (CustomInTurnAnnotationModel *model in dataSourceAnnotations)
    {
        CGPoint p = [mapView convertCoordinate:model.coordinate toPointToView:view];
        float angle       = atan2(p.y - SCREEN_HEIGHT/2.f,p.x - SCREEN_WIDTH/2.f) * (180.0 / M_PI);//場所和屏幕中心形成的角度
        if (angle < 0.f) {
            angle += 360;
        }
        float angleScreen = atan2(-SCREEN_HEIGHT/2.f,-SCREEN_WIDTH/2.f) * (180.0 / M_PI) + 180;//屏幕中心和原點形成的角度
        if (angle >= 180 + angleScreen && angle < 360 - angleScreen)
        {//上
            [topArr addObject:model];
        }else if (angle < 180 + angleScreen && angle >= 180 - angleScreen)
        {//左
            [leftArr addObject:model];
        }else if (angle < 180 - angleScreen && angle >= angleScreen)
        {//下
            [botArr addObject:model];
        }else
        {//右
            [rightArr addObject:model];
        }
    }

計算出四組maker,離當前屏幕中心的距離

 NSMutableArray *disTop = [NSMutableArray array];//頂部場所和屏幕中心距離
    for (int i = 0; i < topArr.count; i++)
    {
        CustomInTurnAnnotationModel *model = [topArr objectAtIndex:i];
        CLLocationCoordinate2D convertPoint = [mapView convertPoint:CGPointMake(SCREEN_WIDTH/2.f, SCREEN_HEIGHT/2.f) toCoordinateFromView:view];
        double dis = [self distancePoint1:model.coordinate Point2:convertPoint];
        NSString *diss = [NSString stringWithFormat:@"%.2f",dis];
        [disTop addObject:diss];
    }
    
    NSMutableArray *disLeft = [NSMutableArray array];//左部場所和屏幕中心距離
    for (int i = 0; i < leftArr.count; i++)
    {
        CustomInTurnAnnotationModel *model = [leftArr objectAtIndex:i];
        CLLocationCoordinate2D convertPoint = [mapView convertPoint:CGPointMake(SCREEN_WIDTH/2.f, SCREEN_HEIGHT/2.f) toCoordinateFromView:view];
        double dis = [self distancePoint1:model.coordinate Point2:convertPoint];
        NSString *diss = [NSString stringWithFormat:@"%.2f",dis];
        [disLeft addObject:diss];
    }
    
    NSMutableArray *disBot = [NSMutableArray array];//底部場所和屏幕中心距離
    for (int i = 0; i < botArr.count; i++)
    {
        CustomInTurnAnnotationModel *model = [botArr objectAtIndex:i];
        CLLocationCoordinate2D convertPoint = [mapView convertPoint:CGPointMake(SCREEN_WIDTH/2.f, SCREEN_HEIGHT/2.f) toCoordinateFromView:view];
        double dis = [self distancePoint1:model.coordinate Point2:convertPoint];
        NSString *diss = [NSString stringWithFormat:@"%.2f",dis];
        [disBot addObject:diss];
    }
    
    NSMutableArray *disRight = [NSMutableArray array];//右部場所和屏幕中心距離
    for (int i = 0; i < rightArr.count; i++)
    {
        CustomInTurnAnnotationModel *model = [rightArr objectAtIndex:i];
        CLLocationCoordinate2D convertPoint = [mapView convertPoint:CGPointMake(SCREEN_WIDTH/2.f, SCREEN_HEIGHT/2.f) toCoordinateFromView:view];
        double dis = [self distancePoint1:model.coordinate Point2:convertPoint];
        NSString *diss = [NSString stringWithFormat:@"%.2f",dis];
        [disRight addObject:diss];
    }

再分別從上面四個距離數(shù)組中找出最近的一個maker

#pragma mark - 查找離屏幕某個方向最近的點
- (NSInteger)findNearModel:(NSMutableArray *)array
{
    if (array.count > 0)
    {
        double dis0 = [array[0] floatValue];
        NSInteger index = 0;
        for (int i = 1; i < array.count; i++)
        {
            NSString *diss = [array objectAtIndex:i];
            double dis = [diss floatValue];
            if (dis0 > dis) {
                dis0 = dis;
                index = i;
            }
        }
        return index;
    }
    return -1;
}

分別設置上下左右四個懸浮按鈕的大小和位置,有一個滑動范圍

CGPoint p = [mapView convertCoordinate:model.coordinate toPointToView:view];
    float angle       = atan2(p.y - SCREEN_HEIGHT/2.f,p.x - SCREEN_WIDTH/2.f) * (180.0 / M_PI);//場所和屏幕中心形成的角度
    if (angle < 0.f) {
        angle += 360;
    }
    float angleScreen = atan2(-SCREEN_HEIGHT/2.f,-SCREEN_WIDTH/2.f) * (180.0 / M_PI) + 180;//屏幕中心和原點形成的角度
    if (angle >= 180 + angleScreen && angle < 360 - angleScreen)
    {//上
        CGFloat scalet = (angle - 180 - angleScreen)/(180 - 2*angleScreen);// 0<=scalet<1
        CGFloat topL   = SCREEN_WIDTH * scalet;
        CGFloat titlew = 0;
        if (![self isNullOrNilWithObject:model.nameStr])
        {
            titlew = [self getStringWidth:model.nameStr andFont:15.f] + 22;
        }
        titlew = MAX(titlew, 50);
        titlew = MIN(titlew, SCREEN_WIDTH - 42);
        CGFloat w = titlew;//場所文字的寬度
        topL -= w/2.f;
        topL = MAX(topL, 10);
        topL = MIN(topL, SCREEN_WIDTH - 10 - w);
        self.topLabel.text = model.nameStr;
        [UIView animateWithDuration:0.1 animations:^{
            self.topViewLeft.constant = topL;
            [self layoutIfNeeded];
        }];
        self.topViewWidth.constant = w;
        self.topView.hidden = false;
    }else if (angle < 180 + angleScreen && angle >= 180 - angleScreen)
    {//左
        CGFloat scalel  = 1 - (angle - 180 + angleScreen)/(2*angleScreen);// 0<scalet<=1
        CGFloat leftT   = SCREEN_HEIGHT * scalel;
        CGFloat titlew = 0;
        if (![self isNullOrNilWithObject:model.nameStr])
        {
            titlew = [self getStringWidth:model.nameStr andFont:15.f] + 42;
        }
        titlew = MAX(titlew, 50);
        titlew = MIN(titlew, SCREEN_WIDTH - 30);
        CGFloat w = titlew + 20;//場所文字的寬度
        leftT -= 19.f;
        leftT = MAX(leftT, 68);
        leftT = MIN(leftT, SCREEN_HEIGHT - 78 - 48);
        self.leftLabel.text = model.nameStr;
        [UIView animateWithDuration:0.1 animations:^{
            self.leftViewTop.constant = leftT;
            [self layoutIfNeeded];
        }];
        self.leftViewWidth.constant = w;
        self.leftView.hidden = false;
    }else if (angle < 180 - angleScreen && angle >= angleScreen)
    {//下
        CGFloat scaleb  = 1 - (angle - angleScreen)/(180 - 2*angleScreen);// 0<scalet<=1
        CGFloat botL   = SCREEN_WIDTH * scaleb;
        CGFloat titlew = 0;
        if (![self isNullOrNilWithObject:model.nameStr])
        {
            titlew = [self getStringWidth:model.nameStr andFont:15.f] + 22;
        }
        titlew = MAX(titlew, 50);
        titlew = MIN(titlew, SCREEN_WIDTH - 42);
        CGFloat w = titlew;//場所文字的寬度
        botL -= w/2.f;
        botL = MAX(botL, 10);
        botL = MIN(botL, SCREEN_WIDTH - 10 - w);
        self.bottomLabel.text = model.nameStr;
        [UIView animateWithDuration:0.1 animations:^{
            self.bottomLeft.constant = botL;
            [self layoutIfNeeded];
        }];
        self.bottomWidth.constant = w;
        self.bottomView.hidden = false;
    }else
    {//右
        CGFloat q = angle + angleScreen;
        if (angle >= 360 - angleScreen && angle < 360)
        {
            q = angle - 360 + angleScreen;
        }
        CGFloat scaler  = q/(2*angleScreen);// 0<=scalet<1
        CGFloat rightT  = SCREEN_HEIGHT * scaler;
        CGFloat titlew = 0;
        if (![self isNullOrNilWithObject:model.nameStr])
        {
            titlew = [self getStringWidth:model.nameStr andFont:15.f] + 42;
        }
        titlew = MAX(titlew, 50);
        titlew = MIN(titlew, SCREEN_WIDTH - 30);
        CGFloat w = titlew + 20;//場所文字的寬度
        rightT -= 19.f;
        rightT = MAX(rightT, 68);
        rightT = MIN(rightT, SCREEN_HEIGHT - 48 - 78);
        self.rightLabel.text = model.nameStr;
        [UIView animateWithDuration:0.1 animations:^{
            self.rightViewTop.constant = rightT;
            [self layoutIfNeeded];
        }];
        self.rightViewWidth.constant = w;
        self.rightView.hidden = false;
    }

因為高德API比較大,cocoapods沒有下載API 終端cd到文件目錄pod install 一下就可以了DEMO地址戳這里記得star一下哦

以上。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容