电车算路
1. 导入头文件
#import <NavinfoKit/NavinfoKit.h>
2. 配置APIKEY
参考工程配置说明。
3. 定义 NavinfoRouteSearch
定义主算路对象 NavinfoRouteSearch,并继承搜索协议<NavinfoRouteQueryDelegate>。
4. 构造 NavinfoRouteSearch
构造主搜索对象 NavinfoRouteSearch,并设置代理。
NavinfoRouteSearch * routeSearch = [[NavinfoRouteSearch alloc] init]; routeSearch.delegate = self;
5. 设置发起电车算路参数
其中orig(起点坐标)vias(途经点坐标集可以为空)dest(终点坐标)query(算路类型)。capacity(充电桩充到多少电量)
- (id)initEleCarWithorig:(NavinfoLonlat * _Nonnull)orig vias:(NSArray<NavinfoLonlat *> * _Nullable)vias dest:(NavinfoLonlat * _Nonnull)dest soc:(double)soc capacity:(double)capacity
6. 发起驾车算路
通过调用 NavinfoSearchRoute 的 startSearch 方法发起驾车算路查询。
[search startSearchWith:query];
7. 在回调中处理数据
当查询成功时,会进到回调函数,通过回调函数,可获取算路的结果。
说明:
通过 response.routes获取驾车换乘方案集。
- (void)onRouteSearch:(NavinfoRouteSearch * _Nonnull)routeSearch result:(NavinfoRouteResult * _Nullable)result error:(NSError * _Nullable)error { NSLog(@"----> onRouteSearch"); }
8. 处理失败查询
当检索失败时,Error 会返回,通过该回调函数获取产生的失败的原因。
- (void)onRouteSearch:(NavinfoRouteSearch * _Nonnull)routeSearch result:(NavinfoRouteResult * _Nullable)result error:(NSError * _Nullable)error { NSLog(@"----> onRouteSearch"); }
代码示例:
- (void)routeSearch:(NavinfoQueryType)type{ NavinfoLonlat * busOrig = [[NavinfoLonlat alloc] initWith:11633930.3 latitude:4001116.0 gbType:NavinfoGbTypeG02]; NavinfoLonlat * busDest = [[NavinfoLonlat alloc] initWith:11645256.2 latitude:3993640.4 gbType:NavinfoGbTypeG02]; NavinfoLonlat * orig = [[NavinfoLonlat alloc] initWith:11634000 latitude:4045000 gbType:NavinfoGbTypeG02]; NavinfoLonlat * dest = [[NavinfoLonlat alloc] initWith:11635000 latitude:4054000 gbType:NavinfoGbTypeG02]; NavinfoLonlat * orig1 = [[NavinfoLonlat alloc] initWith:11645000 latitude:4034000 gbType:NavinfoGbTypeG02]; NavinfoLonlat * dest1 = [[NavinfoLonlat alloc] initWith:11646000 latitude:4035000 gbType:NavinfoGbTypeG02]; NavinfoLonlat * carOrig = [[NavinfoLonlat alloc] initWith:12161561 latitude:3891521 gbType:NavinfoGbTypeG02]; NavinfoLonlat * carDest = [[NavinfoLonlat alloc] initWith:12151443 latitude:3884730 gbType:NavinfoGbTypeG02]; NavinfoRouteQuery * query = nil; switch (type) { case QUERYTYPE_BUS: query = [[NavinfoRouteQuery alloc] initBusQueryWith:busOrig dest:busDest city:@"北京"]; break; case QUERYTYPE_CAR: query = [[NavinfoRouteQuery alloc] initWithorig:carOrig vias:nil dest:carDest]; break; case QUERYTYPE_ELE_CAR: query = [[NavinfoRouteQuery alloc] initEleCarWithorig:orig vias:nil dest:dest soc:20 capacity:40]; break; case QUERYTYPE_BULK: query = [[NavinfoRouteQuery alloc] initBulkQueryWithorigs:@[orig,orig1] dests:@[dest,dest1]]; break; default: break; } NavinfoRouteSearch * search = [[NavinfoRouteSearch alloc]init]; search.delegate = self; [search startSearchWith:query]; } - (void)onRouteSearch:(NavinfoRouteSearch * _Nonnull)routeSearch response:(NavinfoRouteResult * _Nullable)response error:(NSError * _Nullable)error{ }
运行程序,效果图如下:
2. 配置APIKEY
参考工程配置说明。
3. 定义 NavinfoEleCarReachAreaSearch
定义电车行驶范围对象 NavinfoEleCarReachAreaSearch,并继承搜索协议<NavinfoEleCarReachAreaSearchQueryDelegate>。
4. 构造 NavinfoEleCarReachAreaSearch
构造主搜索对象 NavinfoEleCarReachAreaSearch,并设置代理。
NavinfoEleCarReachAreaSearch * areaSearch = [[NavinfoEleCarReachAreaSearch alloc] init]; areaSearch.delegate = self;
5. 设置发起电车行驶范围请求参数
匹路查询的请求参数类为 NavinfoEleCarAreaQuery,快捷构造NavinfoEleCarAreaQuery方法。其中必填参数 centerLonlat (自车位置),soc(电车当前电量)。
- (id)initWithAreaCenter:(NavinfoLonlat *)centerLonlat soc:(double)soc;
6. 发起行驶范围请求
通过调用 NavinfoEleCarReachAreaSearch的 startSearchWith方法发起算路查询。
[routeMatch startMatchWith:matchQuery];
7. 在回调中处理数据
当查询成功时,会进到回调函数,通过回调函数,可获取算路的结果。
说明:
通过 response.lonlats获取电车能够到达点的集合。
- (void)onEleCarReachAreaSearch:(NavinfoEleCarReachAreaSearch * _Nonnull)routeSearch response:(NavinfoEleCarReachAreaResult * _Nullable)response error:(NSError * _Nullable)error;
8. 处理失败查询
当检索失败时,Error 会返回,通过该回调函数获取产生的失败的原因。
- (void)onEleCarReachAreaSearch:(NavinfoEleCarReachAreaSearch * _Nonnull)routeSearch response:(NavinfoEleCarReachAreaResult * _Nullable)response error:(NSError * _Nullable)error{ NSLog(@"Error: %@", error); }
代码示例:
- (void)reachAreaTest{ NavinfoLonlat * centerLonlat = [[NavinfoLonlat alloc]initWith:11637830 latitude:3989856 gbType:NavinfoGbTypeG02]; NavinfoEleCarAreaQuery * areaQuery = [[NavinfoEleCarAreaQuery alloc]initWithAreaCenter:centerLonlat soc:30]; NavinfoEleCarReachAreaSearch * areaSearch = [[NavinfoEleCarReachAreaSearch alloc]init]; [areaSearch startSearchWith:areaQuery]; } - (void)onEleCarReachAreaSearch:(NavinfoEleCarReachAreaSearch * _Nonnull)routeSearch response:(NavinfoEleCarReachAreaResult * _Nullable)response error:(NSError * _Nullable)error{ NSLog(@"Error: %@", error); }
运行程序,效果如下图所示: