Iphone 计算到特定坐标的行驶距离
有没有一种方法可以让我获得到某个特定Iphone 计算到特定坐标的行驶距离,iphone,objective-c,xcode,Iphone,Objective C,Xcode,有没有一种方法可以让我获得到某个特定地理坐标的行驶距离?我知道我必须使用http://maps.googleapis.com/maps/api/directions/output?parameters并获取JSON输出以获取结果 有没有一个教程可以解释如何做到这一点?或者任何我开始的示例代码?您可以尝试以下方法。这段代码获取谷歌方向服务返回的所有方向(甚至是备用方向)。您可以在步骤2中获得距离(解析响应) 步骤1:输入起始和结束位置 NSMutableString *googleURL = [[
地理坐标的行驶距离?我知道我必须使用http://maps.googleapis.com/maps/api/directions/output?parameters
并获取JSON输出以获取结果
有没有一个教程可以解释如何做到这一点?或者任何我开始的示例代码?您可以尝试以下方法。这段代码获取谷歌方向服务返回的所有方向(甚至是备用方向)。您可以在步骤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;
}
-(NSMutableArray*)解码多段线:(NSMutableString*)编码{
[encoded ReplaceAccurrencesofString:@“\\\\”和字符串:@“\\”
选项:NSLiteralSearch
范围:NSMakeRange(0,[编码长度]);
NSInteger len=[编码长度];
NSInteger指数=0;
NSMutableArray*listCoordinates=[[NSMutableArray alloc]init]autorelease];
NSInteger-lat=0;
NSInteger液化天然气=0;
while(指数>1):(结果>>1));
lat+=dlat;
移位=0;
结果=0;
做{
b=[编码字符索引:索引++]-63;
结果|=(b&0x1f)=0x20);
NSInteger dlng=((结果&1)~(结果>>1):(结果>>1));
液化天然气+=液化天然气;
NSNumber*纬度=[[NSNumber alloc]initWithFloat:lat*1e-5];
NSNumber*经度=[[NSNumber alloc]initWithFloat:lng*1e-5];
//CLLocation*loc=[[CLLocation alloc]initWithLatitude:[纬度浮点值]经度:[经度浮点值]];
[listCoordinates添加对象:[NSString stringWithFormat:@“%f,%f”,[latitude floatValue],[latitude floatValue]];
//[loc释放];
[纬度释放];
[经度释放];
}
返回列表坐标;
}
您可以尝试以下操作。此代码获取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;
}
-(NSMutableArray*)解码多段线:(NSMutableString*)编码{
[encoded ReplaceAccurrencesofString:@“\\\\”和字符串:@“\\”
选项:NSLiteralSearch
范围:NSMakeRange(0,[编码长度]);
NSInteger len=[编码长度];
NSInteger指数=0;
NSMutableArray*listCoordinates=[[NSMutableArray alloc]init]autorelease];
NSInteger-lat=0;
NSInteger液化天然气=0;
while(指数>1):(结果>>1));
lat+=dlat;
移位=0;
结果=0;
做{
b=[编码字符索引:索引++]-63;
结果|=(b&0x1f)=0x20);
NSInteger dlng=((结果&1)~(结果>>1):(结果>>1));
液化天然气+=液化天然气;
NSNumber*纬度=[[NSNumber alloc]initWithFloat:lat*1e-5];
NSNumber*经度=[[NSNumber alloc]initWithFloat:lng*1e-5];
//CLLocation*loc=[[CLLocation alloc]initWithLatitude:[纬度浮点值]经度:[经度浮点值]];
[listCoordinates添加对象:[NSString stringWithFormat:@“%f,%f”,[latitude floatValue],[latitude floatValue]];
//[loc释放];
[纬度释放];
[经度释放];
}
返回列表坐标;
}
这个使用CoreLocation和MapKit的更新版本帮助了我很多。这个使用CoreLocation和MapKit的更新版本帮助了我很多。我不知道这方面的任何教程,所以我推荐谷歌。下面有一个答案,你可以从中学习!我不知道这方面的任何教程,所以我推荐谷歌有一个ans在下面,你也许可以从中学习!