高德地圖 (多個大頭針顯示)

高德地圖git

運用場景:先獲取本機經緯度 ,從服務端獲取周圍的小區和停車場的經緯度,經過服務端返回的type賦值給大頭針的title來判斷是小區仍是停車場數組

1,首先實例化地圖app

#import <MapKit/MapKit.h>atom

#import <AMapSearchKit/AMapSearchKit.h>spa

#import <MAMapKit/MAMapView.h>代理

#import <AMapLocationKit/AMapLocationManager.h>code

<MAMapViewDelegate>orm

@property (nonatomic, strong) MAMapView *mapView;圖片

#pragma mark - 懶加載string

-(MAMapView *)mapView{

    if (!_mapView){

        _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64-44)];

        _mapView.backgroundColor = [UIColor whiteColor];

        _mapView.delegate = self;

        _mapView.zoomEnabled = YES;

        _mapView.mapType = MAMapTypeStandard;

        [_mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];

        _mapView.showsCompass= NO;

        _mapView.compassOrigin= CGPointMake(_mapView.compassOrigin.x, 22);

        _mapView.showsScale= NO;

        _mapView.scaleOrigin= CGPointMake(_mapView.scaleOrigin.x, 22);

        [_mapView setZoomLevel:16 animated:YES];

        _mapView.showsUserLocation = YES;

        [[LWLocationManager sharedManager]reverseGeocodeLocationSuccess:^(NSString *locationString) {

            NSLog(@"locationString == %@",locationString);            

        } error:^(NSError *error) {

        }];

    }

    return _mapView;

}

-(void)DatasinMap{//處理附近小區/停車場數據座標

    NSMutableArray *xiaoquArr = [NSMutableArray array];

    /**  後臺數據 **/

    for(MLFoundCarModel *model in self.dataArray){

        MAPointAnnotation *pointAnimate = [[MAPointAnnotation alloc]init];//建立大頭針  循環建立

         pointAnimate.coordinate = CLLocationCoordinate2DMake([model.latitude floatValue],[model.longitude floatValue]);

        pointAnimate.title = model.ID;//把ID傳給pointAnimate的title  進行判斷是哪一個大頭針  這個比較重要

        [xiaoquArr addObject:pointAnimate];//把大頭針裝到一個數組

    }

        [self.mapView addAnnotations:xiaoquArr];//再將數組給到mapview

}

/** 自定義大頭針 **/

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{

    if(([annotation isKindOfClass:[MAUserLocation class]])){//本機經緯度

        static NSString *userReuseIndetifier = @"userPointReuseIndetifier";

        MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:userReuseIndetifier];

        if (annotationView == nil){

            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:userReuseIndetifier];

        }

        annotationView.canShowCallout = NO;

        annotationView.animatesDrop = YES;

        UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(-20, -20, 100, 20)];

        name.adjustsFontSizeToFitWidth = YES;

        name.textColor = White_Color;

        name.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3f];

        name.layer.masksToBounds = YES;

        name.layer.cornerRadius = 8.0f;

        name.textAlignment = NSTextAlignmentCenter;

        name.font = [UIFont fontWithName:@"Arial" size:11];

        if(_types != 1){//搜索頁面進來  隱藏

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

             [annotationView addSubview:name];

        }

        double latitude  = [[MLUserModel sharedUserInfo].latitude doubleValue];

        double longitude = [[MLUserModel sharedUserInfo].longitude doubleValue];

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

            //根據經緯度解析成位置

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

            [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemark,NSError *error){

                CLPlacemark *mark = [placemark objectAtIndex:0];

                if(_types != 1){

                    NSLog(@"%@%@%@%@%@",mark.subLocality,mark.thoroughfare,mark.name,mark.locality,mark.subLocality);

                _bottomView.myLocationTX.text = [NSString stringWithFormat:@"%@%@",mark.subLocality,mark.thoroughfare];

                name.text = [NSString stringWithFormat:@"%@%@",mark.subLocality,mark.thoroughfare];//賦值

                }

            }];

        return annotationView;

    }

    if ([annotation isKindOfClass:[MAPointAnnotation class]]){//自定義後臺返回的座標大頭針

        static NSString *customReuseIndetifier = @"customReuseIndetifier";

//MLCustomAnnotationView自定義的  參考下一篇

        MLCustomAnnotationView *annotationView = (MLCustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];

        if (annotationView == nil){

             annotationView = [[MLCustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customReuseIndetifier];

            annotationView.canShowCallout = NO;

            annotationView.draggable = YES;

            annotationView.delegate = self;

            annotationView.calloutOffset = CGPointMake(0, -5);

            if(_types == 1){//搜索頁面傳過去的經緯度 進行大頭針自定義

                if([annotation.title isEqualToString:@"search"]){

                    annotationView.portrait = [UIImage imageNamed:@"parkingspaces_mine_pressit"];

                    annotationView.titles.text = _address;

                }

            }

//處理小區和停車場圖片數據

            for(MLFoundCarModel *model in _dataArray){

                if([model.ID isEqualToString:annotation.title]){//判斷是哪一個大頭針

                     annotationView.titles.text = model.number;

                    if([model.type isEqualToString:@"1"]){//小區

                        annotationView.portrait = [UIImage imageNamed:@"parking_mapposition_district"];

                    }else{//停車場

                        annotationView.portrait = [UIImage imageNamed:@"parking_mapposition_parkinglot"];

                    }

                }

            }

        }

        return annotationView;

    }

    return nil;

}

#pragma mark 在自定義大頭針view裏面寫了代理,點擊大頭針跳轉詳情頁面

-(void)clickAnnotionView:(NSString *)ID subtitle:(NSString *)subtitle{

    MLRentSeatDetailsViewController *rentvc = [[MLRentSeatDetailsViewController alloc]init];

    rentvc.carId = ID;

    [self.navigationController pushViewController:rentvc animated:YES];

}

#pragma mark  顯示具體位置

- (void)zoomToMapPoints:(MKMapView *)mapView annotations:(NSArray*)annotations{

    double minLat = 360.0f, maxLat = -360.0f;

    double minLon = 360.0f, maxLon = -360.0f;

    for (MKPointAnnotation *annotation in annotations) {

        if ( annotation.coordinate.latitude  < minLat ) minLat = annotation.coordinate.latitude;

        if ( annotation.coordinate.latitude  > maxLat ) maxLat = annotation.coordinate.latitude;

        if ( annotation.coordinate.longitude < minLon ) minLon = annotation.coordinate.longitude;

        if ( annotation.coordinate.longitude > maxLon ) maxLon = annotation.coordinate.longitude;

    }

    CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat + maxLat) / 2.0, (minLon + maxLon) / 2.0);

    MKCoordinateSpan span = MKCoordinateSpanMake(maxLat - minLat, maxLon - minLon);

    MKCoordinateRegion region = MKCoordinateRegionMake(center, span);

    [mapView setRegion:region animated:YES];

}

#pragma mark - MAMapViewDelegate

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {

    //建立一個經緯度點:

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

    //設置點的經緯度

    point.coordinate = userLocation.location.coordinate;

}

 

//地圖放大

    CGFloat oldZoom = self.mapView.zoomLevel;

        [self.mapView setZoomLevel:(oldZoom + 1) animated:YES];

//放小

 

    CGFloat oldZoom = self.mapView.zoomLevel;

 

        [self.mapView setZoomLevel:(oldZoom - 1) animated:YES];

//定位到當前位置

 if(self.mapView.userLocation.updating && self.mapView.userLocation.location) {

            [self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate animated:YES];

        }

}

相關文章
相關標籤/搜索