我試圖用覆蓋圖(MKOverlay)在MKMapView上跟蹤路線。 但是,根據當前速度,如果顏色發生變化(例如,如果用戶從65英里/小時到30英里/小時的駕駛時從綠色變爲橙色),則我想在追蹤路線時使用具有漸變功能的耐克應用。使用MapKit的漸變折線ios
這裏有我想要的截圖:
所以每隔20米,我使用添加從舊覆蓋到新座標:
// Create a c array of points.
MKMapPoint *pointsArray = malloc(sizeof(CLLocationCoordinate2D) * 2);
// Create 2 points.
MKMapPoint startPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(oldLatitude, oldLongitude));
MKMapPoint endPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(newLatitude, newLongitude));
// Fill the array.
pointsArray[0] = startPoint;
pointsArray[1] = endPoint;
// Erase polyline and polyline view if not nil.
if (self.routeLine != nil)
self.routeLine = nil;
if (self.routeLineView != nil)
self.routeLineView = nil;
// Create the polyline based on the array of points.
self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
// Add overlay to map.
[self.mapView addOverlay:self.routeLine];
// clear the memory allocated earlier for the points.
free(pointsArray);
// Save old coordinates.
oldLatitude = newLatitude;
oldLongitude = newLongitude;
基本上我增加了很多小覆蓋。然後,我想創造這個小畫線的坡度,所以我想在覆蓋委託這樣做:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MKOverlayView* overlayView = nil;
if(overlay == self.routeLine) {
// If we have not yet created an overlay view for this overlay, create it now.
if(self.routeLineView == nil) {
self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
if (speedMPH < 25.0) {
self.routeLineView.fillColor = [UIColor redColor];
self.routeLineView.strokeColor = [UIColor redColor];
}
else if (speedMPH >= 25.0 && speedMPH < 50.0) {
self.routeLineView.fillColor = [UIColor orangeColor];
self.routeLineView.strokeColor = [UIColor orangeColor];
}
else {
self.routeLineView.fillColor = [UIColor greenColor];
self.routeLineView.strokeColor = [UIColor greenColor];
}
// Size of the trace.
self.routeLineView.lineWidth = routeLineWidth;
// Add gradient if color changed.
if (oldColor != self.routeLineView.fillColor) {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.routeLineView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[oldColor CGColor], (id)[self.routeLineView.fillColor CGColor], nil];
[self.routeLineView.layer insertSublayer:gradient atIndex:0];
}
// Record old color for gradient.
if (speedMPH < 25.0)
oldColor = [UIColor redColor];
else if (speedMPH >= 25.0 && speedMPH < 50.0)
oldColor = [UIColor orangeColor];
else
oldColor = [UIColor greenColor];
}
overlayView = self.routeLineView;
}
return overlayView;
}
我嘗試添加漸變這種方式,但我想它是不是做這件事的方式,因爲我無法做到這一點。
我每次更新用戶位置(位置對象的代表)或每20米以上時,我也可以跟蹤路徑。
你能幫我一下,給我提示! 謝謝!
routeLine和routeLineView是如何聲明的?上面的代碼是什麼類? 「不能讓它工作」 - 會發生什麼? – Rayfleck 2011-05-25 21:09:52
我只是看不到我添加到覆蓋視圖中的漸變。 'MKPolyline * routeLine;'和'MKPolylineView * routeLineView;',然後每20米創建一個新的並添加爲覆蓋。在上面的MKMapView委託函數中,我根據標準創建視圖並嘗試向其添加漸變。 – Dachmt 2011-06-17 18:16:43