ios 百度地圖劃線

最近在弄百度地圖..百度地圖的坑啊

廢話不說直接上代碼

 BMKMapView *mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 70, self.view.bounds.size.width, self.view.bounds.size.height - 70)];
    self.mapView = mapView;
    [self.view addSubview:mapView];
    
    self.mapView.mapType = BMKMapTypeStandard;
    self.mapView.zoomLevel = 19;
    self.mapView.showMapScaleBar = YES;
   
//    self.mapView.compassPosition = CGPointMake(100, 100);
//    self.mapView.centerCoordinate = self.center;
    // 允許旋轉(zhuǎn)地圖
    self.mapView.rotateEnabled = YES;
    // 定位圖層自定義樣式參數(shù)
    BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc]init];
    //跟隨態(tài)旋轉(zhuǎn)角度是否生效
    displayParam.isRotateAngleValid = NO;
    //精度圈是否顯示
    displayParam.isAccuracyCircleShow = NO;
    //定位偏移量(經(jīng)度)
    displayParam.locationViewOffsetX = 0;
    //定位偏移量(緯度)
    displayParam.locationViewOffsetY = 0;
    displayParam.locationViewImgName = @"walk";
    [self.mapView updateLocationViewWithParam:displayParam];
 // 必須在開始定位之前設(shè)置
    // 設(shè)置定位更新的最小距離(2M)
    self.service.distanceFilter = 10;
    // 設(shè)置定位精度
    self.service.desiredAccuracy = kCLLocationAccuracyBest;
    self.mapView.showsUserLocation = YES;
    self.mapView.userTrackingMode = BMKUserTrackingModeNone;
// 開始定位
- (IBAction)startLocation:(id)sender {
    
    // 先關(guān)閉顯示的定位圖層(一開始是定位到首都)
//    self.mapView.showsUserLocation = NO;
//    // 設(shè)置定位的狀態(tài)(普通定位模式)
//    self.mapView.userTrackingMode = BMKUserTrackingModeNone;
//    // 打開定位圖層
//    self.mapView.showsUserLocation = YES;
    
    // 清除上次路線以及狀態(tài)提示
    [self clean];
    
    self.sumTime = 0;
    self.sumDistance = 0;
    
    // 開始定位服務(wù)
    [self.service startUserLocationService];
    
//    self.mapView.showsUserLocation = YES;
//    self.mapView.userTrackingMode = BMKUserTrackingModeNone;
    
    // 設(shè)置當前地圖最合適的顯示范圍,直接顯示到用戶位置
    BMKCoordinateRegion adjustRegion = [self.mapView regionThatFits:BMKCoordinateRegionMake(self.service.userLocation.location.coordinate, BMKCoordinateSpanMake(0.02f,0.02f))];
    // 定位到指定經(jīng)緯度
    [self.mapView setRegion:adjustRegion animated:YES];
    
    // 7.設(shè)置軌跡記錄狀態(tài)為:開始
    self.trail = TrailStart;
}
// 結(jié)束定位
- (IBAction)stopLocation:(id)sender {
    
    // 設(shè)置軌跡記錄狀態(tài)為:結(jié)束
    self.trail = TrailEnd;
    
    // 停止定位服務(wù)
    [self.service stopUserLocationService];
    // 關(guān)閉定位圖層
//    self.mapView.showsUserLocation = NO;
    
    // 添加終點旗幟
    if (self.startPoint) {
        self.endPoint = [self creatPointWithLocaiton:self.preLocation title:@"終點"];
    }
}
/**
 *  在將要啟動定位時,會調(diào)用此函數(shù)
 */
- (void)willStartLocatingUser {
    NSLog(@"開始定位");
}

/**
 *  在停止定位后,會調(diào)用此函數(shù)
 */
- (void)didStopLocatingUser {
    NSLog(@"停止定位");
}

/**
 *  用戶方向更新后,會調(diào)用此函數(shù)
 *  userLocation 新的用戶位置(百度坐標)
 */
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation {
    
//    [self.mapView updateLocationData:userLocation];
//    NSLog(@"方向為————%@", userLocation.heading);
    
    // 動態(tài)更新位置數(shù)據(jù)
    [self.mapView updateLocationData:userLocation];
}

/**
 *  用戶位置更新后,會調(diào)用此函數(shù)
 *  userLocation 新的用戶位置(百度坐標)
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
   
    // 動態(tài)更新位置數(shù)據(jù)
    [self.mapView updateLocationData:userLocation];
//    NSLog(@"經(jīng)度————%f,緯度————%f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
   
    // 如果精度更新的水平精準度大于10M,那么直接返回該方法
    if (userLocation.location.horizontalAccuracy > kCLLocationAccuracyHundredMeters)
        return;
    
    // GPS精度定位準確無誤,那么就來開始記錄軌跡吧
    [self startTrailRouteWithUserLocation:userLocation];

}
/**
 *  開始記錄有效軌跡
 */
- (void)startTrailRouteWithUserLocation:(BMKUserLocation *)userLocation {
    // 如果該點不是第一個點,則可以進行下面的比較運算
    if (self.preLocation) {
        // 計算本次定位數(shù)據(jù)與上次定位數(shù)據(jù)之間的時間差
        NSTimeInterval dtime = [userLocation.location.timestamp timeIntervalSinceDate:self.preLocation.timestamp];
        
        // 累計步行時間
        self.sumTime += dtime;
//        self.statusView.sumTime.text = [NSString stringWithFormat:@"%.3f",self.sumTime];
        
        // 計算本次定位點與上次定位點之間的距離
        CGFloat distance = [userLocation.location distanceFromLocation:self.preLocation];
        // (5米距離的限值,存儲到數(shù)組之中) 如果距離少于2米,則忽略本次數(shù)據(jù)直接返回該方法
        if (distance < 10) {
            NSLog(@"與前一記錄點距離小于2m,直接返回該方法");
            return;
        }
        
        // 計算本地定位點與上次定位點的方向
        if (userLocation.location.coordinate.latitude > self.preLocation.coordinate.latitude && userLocation.location.coordinate.longitude > self.preLocation.coordinate.longitude) {
            self.stateView.directionLabel.text = @"東北方向行進中";
        }
        
        if (userLocation.location.coordinate.latitude > self.preLocation.coordinate.latitude && userLocation.location.coordinate.longitude < self.preLocation.coordinate.longitude) {
            self.stateView.directionLabel.text = @"西北方向行進中";
        }
        
        if (userLocation.location.coordinate.latitude < self.preLocation.coordinate.latitude && userLocation.location.coordinate.longitude > self.preLocation.coordinate.longitude) {
            self.stateView.directionLabel.text = @"東南方向行進中";
        }
        
        if (userLocation.location.coordinate.latitude < self.preLocation.coordinate.latitude && userLocation.location.coordinate.longitude < self.preLocation.coordinate.longitude) {
            self.stateView.directionLabel.text = @"西南方向行進中";
        }
        
        
        // 累加步行距離
        self.sumDistance += distance;
        self.stateView.distanceLabel.text = [NSString stringWithFormat:@"%.3f",self.sumDistance / 1000.0];
        NSLog(@"步行總距離為:%f",self.sumDistance);
        
        // 計算移動速度
        CGFloat speed = distance / dtime;
        self.stateView.speedLabel.text = [NSString stringWithFormat:@"%.3f",speed];
        NSLog(@"步行的當前移動速度為:%.3f", speed);
        
        // 計算平均速度
//        CGFloat avgSpeed  = self.sumDistance / self.sumTime;
//        self.statusView.avgSpeed.text = [NSString stringWithFormat:@"%.3f",avgSpeed];
    }
    
    // 2. 將符合的位置點存儲到數(shù)組中
    [self.locationArrayM addObject:userLocation.location];
    self.preLocation = userLocation.location;
    
    // 3. 繪圖
    [self drawWalkPolyline];
}

/**
 *  繪制軌跡路線
 */
- (void)drawWalkPolyline
{
    // 我們保存的符合軌跡點的個數(shù)
    NSUInteger count = self.locationArrayM.count;
    
    // 手動分配存儲空間,結(jié)構(gòu)體:地理坐標點,用直角地理坐標表示 X:橫坐標 Y:縱坐標
    BMKMapPoint *tempPoints = new BMKMapPoint[count];
    
    [self.locationArrayM enumerateObjectsUsingBlock:^(CLLocation *location, NSUInteger idx, BOOL *stop) {
        BMKMapPoint locationPoint = BMKMapPointForCoordinate(location.coordinate);
        tempPoints[idx] = locationPoint;
//        NSLog(@"idx = %ld,tempPoints X = %f Y = %f",idx,tempPoints[idx].x,tempPoints[idx].y);
        
        // 放置起點旗幟
        if (0 == idx && TrailStart == self.trail && self.startPoint == nil) {
            self.startPoint = [self creatPointWithLocaiton:location title:@"起點"];
        }
        
//            else { // 如果不是起點旗幟,那么肯定是中間的經(jīng)過點得旗幟
            [self creatPointWithLocaiton:location title:@"過程點"];
//        }
    }];
    
    //移除原有的繪圖
    if (self.polyLine) {
        [self.mapView removeOverlay:self.polyLine];
    }
    
    // 通過points構(gòu)建BMKPolyline
    self.polyLine = [BMKPolyline polylineWithPoints:tempPoints count:count];
    
    //添加路線,繪圖
    if (self.polyLine) {
        [self.mapView addOverlay:self.polyLine];
    }
    
    // 清空 tempPoints 內(nèi)存
    delete []tempPoints;
    
    [self mapViewFitPolyLine:self.polyLine];   
}
/**
 *  添加一個大頭針
 */
- (BMKPointAnnotation *)creatPointWithLocaiton:(CLLocation *)location title:(NSString *)title;
{
    BMKPointAnnotation *point = [[BMKPointAnnotation alloc] init];
    point.coordinate = location.coordinate;
    point.title = title;
    [self.mapView addAnnotation:point];
    
    return point;
}

/**
 *  根據(jù)polyline(軌跡線)設(shè)置地圖范圍
 */
- (void)mapViewFitPolyLine:(BMKPolyline *) polyLine {
    CGFloat ltX, ltY, rbX, rbY;
    if (polyLine.pointCount < 1) {
        return;
    }
    BMKMapPoint pt = polyLine.points[0];
    ltX = pt.x, ltY = pt.y;
    rbX = pt.x, rbY = pt.y;
    for (int i = 1; i < polyLine.pointCount; i++) {
        BMKMapPoint pt = polyLine.points[i];
        if (pt.x < ltX) {
            ltX = pt.x;
        }
        if (pt.x > rbX) {
            rbX = pt.x;
        }
        if (pt.y > ltY) {
            ltY = pt.y;
        }
        if (pt.y < rbY) {
            rbY = pt.y;
        }
    }
    BMKMapRect rect;
    rect.origin = BMKMapPointMake(ltX , ltY);
    rect.size = BMKMapSizeMake(rbX - ltX, rbY - ltY);
    [self.mapView setVisibleMapRect:rect];
    self.mapView.zoomLevel = self.mapView.zoomLevel - 0.3;
}

/**
 *  清空數(shù)組以及地圖上的軌跡
 */
- (void)clean
{
    // 清空狀態(tài)欄信息
    self.stateView.distanceLabel.text = nil;
    self.stateView.speedLabel.text = nil;
    self.stateView.directionLabel.text = nil;

    //清空數(shù)組
    [self.locationArrayM removeAllObjects];
    
    //清屏,移除標注點
    if (self.startPoint) {
        [self.mapView removeAnnotation:self.startPoint];
        self.startPoint = nil;
    }
    if (self.endPoint) {
        [self.mapView removeAnnotation:self.endPoint];
        self.endPoint = nil;
    }
    if (self.polyLine) {
        [self.mapView removeOverlay:self.polyLine];
        self.polyLine = nil;
    }
    
    
}

#pragma mark - BMKMapViewDelegate

/**
 *  根據(jù)overlay生成對應(yīng)的View
 *  @param mapView 地圖View
 *  @param overlay 指定的overlay
 *  @return 生成的覆蓋物View
 */
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
    if ([overlay isKindOfClass:[BMKPolyline class]]) {
        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        polylineView.fillColor = [[UIColor clearColor] colorWithAlphaComponent:0.7];
        polylineView.strokeColor = [[UIColor greenColor] colorWithAlphaComponent:0.7];
        polylineView.lineWidth = 10.0;
        return polylineView;
    }
    return nil;
}

/**
 *  只有在添加大頭針的時候會調(diào)用,直接在viewDidload中不會調(diào)用
 *  根據(jù)anntation生成對應(yīng)的View
 *  @param mapView 地圖View
 *  @param annotation 指定的標注
 *  @return 生成的標注View
 */
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *annotationView = [[BMKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        
        
        if (!self.startPoint) { // 沒有起點,自然要放置起點大頭針(綠色)
            annotationView.pinColor = BMKPinAnnotationColorGreen; // 替換資源包內(nèi)的圖片
//            self.statusView.stopPointLabel.text = @"YES";
        } else if (self.trail == TrailEnd) { // 點擊了結(jié)束定位,自然要放置終點大頭針(紅色)
            annotationView.pinColor = BMKPinAnnotationColorRed;
//            self.statusView.startPointLabel.text = @"YES";
        } else { // 過程大頭針(紫色)
            annotationView.pinColor = BMKPinAnnotationColorPurple;
        }
        
        // 從天上掉下效果
        annotationView.animatesDrop = YES;
        
        // 不可拖拽
        annotationView.draggable = NO;
        
        return annotationView;
    }
    return nil;
}

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

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,115評論 25 709
  • 一天下著綿綿細雨,心情糟糕的很。工作也不是很順心,時常想靜下新
    0海盜0閱讀 166評論 0 0
  • 開始學習javaweb 不是很喜歡這個東西,因為感覺把java和html等混雜在一起感覺怪怪的,很煩人,還是pyt...
    羋子契閱讀 346評論 0 0
  • 年輪 (一) 將一生的冷暖陰晴, 畫諸多的句號, 哪怕扭扭曲曲、歪歪斜斜。 (二) 只要不倒下, 你永遠看不到, ...
    37ba71a25722閱讀 318評論 4 6

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