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è)置當(dāng)前地圖最合適的顯示范圍亥宿,直接顯示到用戶位置
    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;
    
    // 添加終點(diǎn)旗幟
    if (self.startPoint) {
        self.endPoint = [self creatPointWithLocaiton:self.preLocation title:@"終點(diǎn)"];
    }
}
/**
 *  在將要啟動(dòng)定位時(shí)俭缓,會(huì)調(diào)用此函數(shù)
 */
- (void)willStartLocatingUser {
    NSLog(@"開始定位");
}

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

/**
 *  用戶方向更新后,會(huì)調(diào)用此函數(shù)
 *  userLocation 新的用戶位置(百度坐標(biāo))
 */
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation {
    
//    [self.mapView updateLocationData:userLocation];
//    NSLog(@"方向?yàn)椤?@", userLocation.heading);
    
    // 動(dòng)態(tài)更新位置數(shù)據(jù)
    [self.mapView updateLocationData:userLocation];
}

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

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

/**
 *  繪制軌跡路線
 */
- (void)drawWalkPolyline
{
    // 我們保存的符合軌跡點(diǎn)的個(gè)數(shù)
    NSUInteger count = self.locationArrayM.count;
    
    // 手動(dòng)分配存儲(chǔ)空間景鼠,結(jié)構(gòu)體:地理坐標(biāo)點(diǎn)仲翎,用直角地理坐標(biāo)表示 X:橫坐標(biāo) Y:縱坐標(biāo)
    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);
        
        // 放置起點(diǎn)旗幟
        if (0 == idx && TrailStart == self.trail && self.startPoint == nil) {
            self.startPoint = [self creatPointWithLocaiton:location title:@"起點(diǎn)"];
        }
        
//            else { // 如果不是起點(diǎn)旗幟,那么肯定是中間的經(jīng)過點(diǎn)得旗幟
            [self creatPointWithLocaiton:location title:@"過程點(diǎn)"];
//        }
    }];
    
    //移除原有的繪圖
    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];   
}
/**
 *  添加一個(gè)大頭針
 */
- (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];
    
    //清屏铛漓,移除標(biāo)注點(diǎn)
    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;
}

/**
 *  只有在添加大頭針的時(shí)候會(huì)調(diào)用溯香,直接在viewDidload中不會(huì)調(diào)用
 *  根據(jù)anntation生成對應(yīng)的View
 *  @param mapView 地圖View
 *  @param annotation 指定的標(biāo)注
 *  @return 生成的標(biāo)注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) { // 沒有起點(diǎn),自然要放置起點(diǎn)大頭針(綠色)
            annotationView.pinColor = BMKPinAnnotationColorGreen; // 替換資源包內(nèi)的圖片
//            self.statusView.stopPointLabel.text = @"YES";
        } else if (self.trail == TrailEnd) { // 點(diǎn)擊了結(jié)束定位浓恶,自然要放置終點(diǎn)大頭針(紅色)
            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)系作者
  • 序言:七十年代末玫坛,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子包晰,更是在濱河造成了極大的恐慌湿镀,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件伐憾,死亡現(xiàn)場離奇詭異勉痴,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)树肃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進(jìn)店門蒸矛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事雏掠≌都溃” “怎么了?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵磁玉,是天一觀的道長停忿。 經(jīng)常有香客問我,道長蚊伞,這世上最難降的妖魔是什么席赂? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮时迫,結(jié)果婚禮上颅停,老公的妹妹穿的比我還像新娘。我一直安慰自己掠拳,他們只是感情好癞揉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著溺欧,像睡著了一般喊熟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上姐刁,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天芥牌,我揣著相機(jī)與錄音,去河邊找鬼聂使。 笑死壁拉,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的柏靶。 我是一名探鬼主播弃理,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼屎蜓!你這毒婦竟也來了痘昌?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤梆靖,失蹤者是張志新(化名)和其女友劉穎控汉,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體返吻,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡姑子,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了测僵。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片街佑。...
    茶點(diǎn)故事閱讀 40,015評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡谢翎,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出沐旨,到底是詐尸還是另有隱情森逮,我是刑警寧澤,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布磁携,位于F島的核電站褒侧,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏谊迄。R本人自食惡果不足惜闷供,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望统诺。 院中可真熱鬧歪脏,春花似錦、人聲如沸粮呢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽啄寡。三九已至豪硅,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間挺物,已是汗流浹背舟误。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留姻乓,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓眯牧,卻偏偏與公主長得像蹋岩,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子学少,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,152評論 25 707
  • 一天下著綿綿細(xì)雨剪个,心情糟糕的很。工作也不是很順心版确,時(shí)常想靜下新
    0海盜0閱讀 149評論 0 0
  • 開始學(xué)習(xí)javaweb 不是很喜歡這個(gè)東西扣囊,因?yàn)楦杏X把java和html等混雜在一起感覺怪怪的,很煩人绒疗,還是pyt...
    羋子契閱讀 322評論 0 0
  • 年輪 (一) 將一生的冷暖陰晴侵歇, 畫諸多的句號, 哪怕扭扭曲曲吓蘑、歪歪斜斜惕虑。 (二) 只要不倒下坟冲, 你永遠(yuǎn)看不到, ...
    37ba71a25722閱讀 276評論 4 6