您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iPhone 開發:使用 MapKit 在地圖上繪製移動軌跡

編輯:iOS 開發綜合

iOS 的 MapKit 整合了大量 Google 地圖 API 的功能,再加上 iOS 本身的定位能力,我們就能把移動的軌跡繪製在地圖上。這個功能應用廣泛,例如車輛的 GPS 追蹤、人員定位、快遞配送追蹤等等。本文將示範如何結合 MapKit 與定位服務,把運行軌跡畫在地圖上。

先從繪製曲線開始

若要把先前定位到的各個位置串接起來,就形成了移動軌跡。MapKit 提供了 MKPolyline 類別來繪製折線。以下先示範如何從檔案讀取經緯度,並建立一條 MKPolyline 路徑:

-(void) loadRoute
{
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"route" ofType:@"csv"];
NSString* fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSArray* pointStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
MKMapPoint northEastPoint;
MKMapPoint southWestPoint;
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * pointStrings.count);
for(int idx = 0; idx < pointStrings.count; idx++)
{
    NSString* currentPointString = [pointStrings objectAtIndex:idx];
    NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
    CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue];
    CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
    MKMapPoint point = MKMapPointForCoordinate(coordinate);
    if (idx == 0) { northEastPoint = point; southWestPoint = point; }
    else
    {
        if (point.x > northEastPoint.x) northEastPoint.x = point.x;
        if(point.y > northEastPoint.y) northEastPoint.y = point.y;
        if (point.x < southWestPoint.x) southWestPoint.x = point.x;
        if (point.y < southWestPoint.y) southWestPoint.y = point.y;
    }
    pointArr[idx] = point;
}
self.routeLine = [MKPolyline polylineWithPoints:pointArr count:pointStrings.count];
_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
free(pointArr);
}

建立好 MKPolyline 物件後,把它加入地圖:

[self.mapView addOverlay:self.routeLine];

並在委派方法中回傳對應的 overlay 檢視來顯示:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
{
MKOverlayView* overlayView = nil;
if(overlay == self.routeLine)
{
    if(nil == self.routeLineView)
    {
       self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
       self.routeLineView.fillColor = [UIColor redColor];
       self.routeLineView.strokeColor = [UIColor redColor];
       self.routeLineView.lineWidth = 3;
    }
    overlayView = self.routeLineView;
}
return overlayView;
}

改為即時取得定位

接著把「從檔案讀取位置」改成「用 GPS 等方式即時取得目前位置」。首先建立 CLLocationManager 並設定委派、精度與距離篩選器:

- (void)viewDidLoad {
    [super viewDidLoad];
    noUpdates = 0;
    locations = [[NSMutableArray alloc] init];
    locationMgr = [[CLLocationManager alloc] init];
    locationMgr.delegate = self;
    locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
    locationMgr.distanceFilter = 1.0f;
    [locationMgr startUpdatingLocation];
}

這裡用一個陣列保存軌跡的經緯度。每次收到位置更新時,把新的座標存進陣列並重新繪製路徑:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    noUpdates++;
    [locations addObject: [NSString stringWithFormat:@"%f,%f",[newLocation coordinate].latitude, [newLocation coordinate].longitude]];
    [self updateLocation];
    if (self.routeLine!=nil) { self.routeLine =nil; }
    if(self.routeLine!=nil) [self.mapView removeOverlay:self.routeLine];
    self.routeLine =nil;
    [self loadRoute];
    if (nil != self.routeLine) { [self.mapView addOverlay:self.routeLine]; }
    [self zoomInOnRoute];
}

最後只要把 loadRoute 中「從檔案取值」的部分改成「從 locations 陣列取值」,其餘邏輯相同,就能即時把移動軌跡繪製在地圖上。透過 MKPolyline 與 MKMapRect 的運算,還能自動計算出包圍整條軌跡的範圍,方便地圖自動縮放定位。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved