我試圖在this教程的幫助下顯示兩個位置之間的路徑。他們使用了一個CSV文件的座標,我使用谷歌API獲取座標。但結果是完全不同的。 使用MKPolyline在兩個位置之間繪製路徑
正如你所看到的,它不能繪製正確的路徑。 Plz建議我一些東西。
我試圖在this教程的幫助下顯示兩個位置之間的路徑。他們使用了一個CSV文件的座標,我使用谷歌API獲取座標。但結果是完全不同的。 使用MKPolyline在兩個位置之間繪製路徑
正如你所看到的,它不能繪製正確的路徑。 Plz建議我一些東西。
您需要解碼,你是從響應得到折線..並以你需要谷歌的算法...
// http://code.google.com/apis/maps/documentation/utilities/polylinealgorithm.html
//
-(NSMutableArray *)decodePolyLine:(NSString *)encodedStr {
NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];
[encoded appendString:encodedStr];
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
NSInteger lat=0;
NSInteger lng=0;
while (index < len) {
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];
NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
// printf("[%f,", [latitude doubleValue]);
// printf("%f]", [longitude doubleValue]);
CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease];
[array addObject:loc];
}
[encoded release];
return array;
}
這會給你所有的點的可變數組(以CLLocation對象的形式) ,也不只是解碼主折線。解碼您收到的每一條子折線,而不是劇情,否則方向將不正確。
我使用相同的編碼功能,但應用程序崩潰。刪除[編碼的replaceOccurrencesOfString:@「\\\\」withString:@「\\」選項:NSLiteralSearch範圍:NSMakeRange(0,[encoded length])];它正在工作,但沒有給出準確的結果。 –
我在這裏也遇到了一些問題......它曾經從小距離工作......在AGRA和德里之間,當我使用德里和特里凡得琅時,它最終崩潰了。所以我不確定它是如何工作的。從來沒有對此給過多考慮。最後我最終用google地圖加載了webview。 :P –
你能提出一些準確的建議嗎?它不會在路上 –
您可以通過在代碼中添加這種簡單的方法添加實線(路徑):
-(void)addSolidLine:(CLLocation *)source andDestination:(CLLocation*)destination
{
CLLocationCoordinate2D coordinates[2] = {source.coordinate, destination.coordinate};
MKGeodesicPolyline *geodesicPolylineSolid = [MKGeodesicPolyline polylineWithCoordinates:coordinates count:2];
[self.mapView addOverlay:geodesicPolylineSolid];
}
#pragma mark - map view delegate
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:(MKPolyline *)overlay];
renderer.lineWidth = 1.5f;
renderer.strokeColor = [UIColor greenColor];
renderer.alpha = 50;
return renderer;
}
}
如果你想顯示虛線,你可以添加此medthod:
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:(MKPolyline *)overlay];
renderer.lineDashPattern = @[@2, @5];
renderer.lineWidth = 1.5f;
renderer.strokeColor = [UIColor redColor];
renderer.alpha = 50;
isShowingDottedLine = false;
return renderer;
但我開發了一個應用程序使用相同的方向API,它仍然正常工作。它的部署目標是3.2,所以現在我正在使用MKPolyline爲iOS4再次開發它。 –
問題不在於ios,而在於您的csv文件的製作方式以及閱讀方式。另一個提示nslog記錄正在形成MKPolyline的所有座標。我敢打賭,你會看到你會感到驚訝。 – Robin
你使用JSON響應..? –