有沒有一種方法,我可以得到特定的駕駛距離geo-cordinate
?我知道我必須使用http://maps.googleapis.com/maps/api/directions/output?parameters
並獲得JSON輸出以獲得結果。計算行車距離到一個特定的座標
是否有教程解釋如何完成此任務?或者我開始的任何示例代碼?
有沒有一種方法,我可以得到特定的駕駛距離geo-cordinate
?我知道我必須使用http://maps.googleapis.com/maps/api/directions/output?parameters
並獲得JSON輸出以獲得結果。計算行車距離到一個特定的座標
是否有教程解釋如何完成此任務?或者我開始的任何示例代碼?
您可以嘗試以下操作。此代碼提取Google Directions Services返回的所有路線(甚至是替代路線)。你可以在第2步(解析響應)
第1步的距離:輸入起始和結束位置
NSMutableString *googleURL = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=true&alternatives=true", searchFrom.text, searchTo.text]];
NSURL *url = [NSURL URLWithString:googleURL];
[googleURL release];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
request = nil;
responseData = [[NSMutableData alloc] init];
第2步:當你進行的查詢的響應,解析結果
- (id)parseResponseWithDictionary:(NSDictionary *)dictResponse {
if ([[dictResponse objectForKey:@"status"] isEqualToString:kGoogleDirectionsStatusOK]) {
NSArray *listRoutes = (NSArray *)[dictResponse objectForKey:@"routes"];
NSMutableString *result = nil;
NSMutableArray *listPolylines = nil;
for (NSDictionary *dictRouteValues in listRoutes) {
NSDictionary *dictPolyline = [dictRouteValues objectForKey:@"overview_polyline"];
if (!result) {
result = [[NSMutableString alloc] init];
}
[result appendString:[dictPolyline objectForKey:@"points"]];
if (!listPolylines) {
listPolylines = [[NSMutableArray alloc] init];
}
[listPolylines addObject:result];
[result release];
result = nil;
}
return [listPolylines autorelease];
}
else {
NSString *error = @"No result found. Please check start and end location again!";
return error;
}
return nil;
}
第3步:解碼多段線列表中的每條多段線。這將給出路徑座標。
-(NSMutableArray *)decodePolyLine:(NSMutableString *)encoded {
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *listCoordinates = [[[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];
NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
//CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
[listCoordinates addObject:[NSString stringWithFormat:@"%f,%f", [latitude floatValue], [longitude floatValue]]];
//[loc release];
[latitude release];
[longitude release];
}
return listCoordinates;
}
這個更新的tutorial/sample code使用CoreLocation和MapKit幫助了我一堆。
我不知道這方面的任何教程,所以我會推薦Google – khr055
下面有一個答案,你可以從中學習! – sharon