调用iOS自带地图应用查询路线
MapKit.framework自带的mapView只具备定位功能,不具备查询路线功能,所以调用系统iOS系统自带的地图程序来查询线路是很常见的做法,实际上要用到Google Map的API,这里先实现由一个经纬度到另一个经纬度路线的方法
代码很短:
float latitude1 = 39.993638;
float longitude1 = 116.413156;
float latitude2 = 42.993638;
float longitude2 = 120.413156;
NSString *theString = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",latitude1,longitude1,latitude2, longitude2];
theString = [theString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:theString];
[[UIApplication sharedApplication] openURL:url];
主要内容是这句话NSString *theString = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",sourceLatitude,sourceLongitude,destinyLatitude, destinyLongitude];
可以查看这个网址Google地图API,saddr是起始地址,daddr是目的地址,同理,不用坐标也可以用地名来搜索路线:
NSString *sourcePlace=@”北京后海”;
NSString *destinyPlace=@”北京中关村图书大厦”;
NSString *theString = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@",sourcePlace, destinyPlace];
theString = [theString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:theString];
[[UIApplication sharedApplication] openURL:url];