美文网首页
iOS 高德地图 仿大众点评 区域内搜索

iOS 高德地图 仿大众点评 区域内搜索

作者: 唐人街的乞丐 | 来源:发表于2020-06-12 10:17 被阅读0次

    仿大众点评 区域内搜索

    区域内搜索

    1 申请KEY
    (1)到高德开放平台-我的应用申请iOS key;

    2.集成高德地图SDK
    (2)集成高德地图SDK,我是通过 CocoaPods 来集成的,好处就不说了~

    platform :ios, '11.0'
    target 'CJMap' do
    pod 'AMapSearch' #搜索服务SDK
    pod 'AMapLocation'
    pod 'AMapNavi'#这个要放到其他高德sdk后
    pod 'JZLocationConverter'#gps纠偏
     end
    

    3.添加位置权限

            <key>NSLocationAlwaysUsageDescription</key>
        <string>是否允许使用定位来给您提供服务?</string>
        <key>NSLocationWhenInUseUsageDescription</key>
        <string>是否允许使用定位来给您提供服务?</string>
        <key>NSAppTransportSecurity</key>
        <dict>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
        </dict>
    

    4.添加KEY

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        
        [AMapServices sharedServices].apiKey = MapKey;
        [AMapServices sharedServices].enableHTTPS = YES;
        return YES;
    }
    

    5.初始化地图,和搜索API

    //初始化地图,和搜索API
    - (void)initMapView {
        self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
        self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        self.mapView.delegate = self;
        //  不支持旋转
        self.mapView.rotateEnabled = NO;
        //倾斜收拾
        self.mapView.rotateCameraEnabled = NO;
    //    表示不显示比例尺
        self.mapView.showsScale= NO;
        ///如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
        self.mapView.showsUserLocation = YES;
        self.mapView.zoomLevel = 14;
        self.mapView.userTrackingMode = MAUserTrackingModeFollow;
        [self.view addSubview:self.mapView];
        self.search = [[AMapSearchAPI alloc] init];
         self.search.delegate = self;
    
    }
    

    5.POI搜索获取当前左上角和右下角的经纬度, 使用POI多边形搜索,构成一个封闭的矩形

    /**
     * @brief 地图区域改变完成后会调用此接口
     * @param mapView 地图View
     * @param animated 是否动画
     */
    - (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
        
      CLLocationCoordinate2D leftTop  =   [mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:self.view];
        self.leftTop = leftTop;
        CLLocationCoordinate2D rightBtm  =   [mapView convertPoint:CGPointMake(self.mapView.mj_w, self.mapView.mj_h) toCoordinateFromView:self.view];
        self.rightBtm = rightBtm;
    
    }
    

    6.按钮点击事件,执行搜索操作

    //  按钮click
    -(void)buttonClick{
        NSLog(@"click");
        NSArray *points = [NSArray arrayWithObjects:
                           [AMapGeoPoint locationWithLatitude:self.leftTop.latitude longitude:self.leftTop.longitude],
                           [AMapGeoPoint locationWithLatitude:self.rightBtm.latitude longitude:self.rightBtm.longitude],
                           nil];
        AMapGeoPolygon *polygon = [AMapGeoPolygon polygonWithPoints:points];
    
        AMapPOIPolygonSearchRequest *request = [[AMapPOIPolygonSearchRequest alloc] init];
    
        request.polygon             = polygon;
        request.keywords            = @"餐饮";
        request.requireExtension    = YES;
        [self.search AMapPOIPolygonSearch:request];
    }
    

    7.POI 多边形搜索回调.

    /* POI 搜索回调. */
    - (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
    {
        [self.mapView removeAnnotations:self.searchResultArr];
        [self.searchResultArr removeAllObjects];
        if (response.pois.count == 0)
        {
            return;
        }
        
        [response.pois enumerateObjectsUsingBlock:^(AMapPOI * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // 这里使用了自定义的坐标是为了区分系统坐标
    
          CJSearchPOIPointAnnotation *annotation = [[CJSearchPOIPointAnnotation alloc] init];
            [annotation setCoordinate:CLLocationCoordinate2DMake(obj.location.latitude, obj.location.longitude)];
            [annotation setTitle:[NSString stringWithFormat:@"%@", obj.name]];
    //        [annotation setSubtitle:[NSString stringWithFormat:@"%zd米",obj.distance]];
            [annotation setSubtitle:@""];
            [self.searchResultArr addObject:annotation];
        }];
        // 向地图窗口添加一组标注
           [self.mapView addAnnotations:self.searchResultArr];
    }
    

    8,地图添加上自定义图标的大头针即可

    //只要有标注点需要显示,该回调就会被调用
    - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
          //  自己的位置
        if ([annotation isKindOfClass:[MAUserLocation class]]) {
    
    
           return nil;
    
        }else  if ([annotation isKindOfClass:[CJSearchPOIPointAnnotation class]] )
         {
              
             static NSString *tipIdentifier = @"ZZOilAnnotation";
             NSLog(@"annotation.title----%@",annotation.title);
             MAAnnotationView *poiAnnotationView = (MAAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:tipIdentifier];
             if (poiAnnotationView == nil)
             {
                 poiAnnotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:tipIdentifier];
             }
             poiAnnotationView.canShowCallout= YES; //设置气泡可以弹出,默认为NO
             poiAnnotationView.selected = YES;  //设置标注动画显示,默认为NO
             poiAnnotationView.image = [UIImage imageNamed:@"map_local_oil1"];
             poiAnnotationView.centerOffset= CGPointMake(0, mapCenterOffset);
             [poiAnnotationView setSelected:YES animated:NO];
             
             //        点击大头针显示的右边视图
             UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
             rightButton.backgroundColor = [UIColor redColor];
             [rightButton setTitle:@"导航" forState:UIControlStateNormal];
             [rightButton addTarget:self action:@selector(navBtnClick) forControlEvents:UIControlEventTouchUpInside];
             poiAnnotationView.rightCalloutAccessoryView = rightButton;
             return poiAnnotationView;
         }
        
          return nil;
    
    }
    

    更多关于高德地图的完整代码在完整代码GitHub
    (包括:高德地图展示,当前位置,自定义圆点图,圆点带方向旋转,驾车路线轨迹,区域内搜索(类似于大众点评),地理编码(地址转坐标),反地理编码(坐标转地址),自定义地图样式)

    相关文章

      网友评论

          本文标题:iOS 高德地图 仿大众点评 区域内搜索

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