1
我是比較新的IOS場景,所以這可能是一個愚蠢的問題,但反正...如何使用MKRoute創建特定路徑?
我一直在做一個項目,我想做一個特定的路線在地圖上標出。例如:公共汽車的行程總是相同的,所以我想強調公共汽車在到達最終目的地之前所經過的街道。
事情是我只能從一個點到另一個方向,但它不遵循巴士的行程,所以它不適用於我。
請幫忙!!
我是比較新的IOS場景,所以這可能是一個愚蠢的問題,但反正...如何使用MKRoute創建特定路徑?
我一直在做一個項目,我想做一個特定的路線在地圖上標出。例如:公共汽車的行程總是相同的,所以我想強調公共汽車在到達最終目的地之前所經過的街道。
事情是我只能從一個點到另一個方向,但它不遵循巴士的行程,所以它不適用於我。
請幫忙!!
首先,如果您需要特定的路線,您將需要它的一些座標。比方說,你有一些航線在NSArray
以下存儲
{
Lat = 53.3478;
Lon = -6.2597;
}
的例子稱爲routeLocations
座標作爲NSDictionary
對象一樣,你可以做到以下幾點:
CLLocationCoordinate2D routeCoord[routeLocations.count];
for (int i = 0; i < routeLocations.count; i++)
{
id location =[routeLocations objectAtIndex:i];
routeCoord[i] = CLLocationCoordine2DMake([[location objectForKey:@"Lat"]floatValue], [[location objectForKey:@"Lon"]floatValue]);
}
// create your route Polyline
MKPolyline *poly = [MKPolyline polylineWithCoordinates:routeCoord count:routeLocations.count];
// first remove previously added overlays if any then add your newly created route polyline
[self.mapView addOverlay:poly];
如果您需要在您的覆蓋,你可以定製實現以下 對於iOS> = 7
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]])
{
MKPolyline *route = overlay;
MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc]initWithPolyline:route];
routeRenderer.strokeColor = [UIColor blueColor];
return routeRenderer;
}
}
否則使用廢棄的滿足hod
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineView *route = [[MKPolylineView alloc]initWithPolyline:overlay];
route.strokeColor = [UIColor blueColor];
return route;
}
}