【发布时间】:2016-06-03 23:37:25
【问题描述】:
我正在将 HERE SDK 整合到我的应用中。除了一个简单的地图设置之外,HERE 网站上的所有示例都显示在 Objective-C 中,我正在尽力将它们翻译成 Swift,但它还没有 100% 工作。我正在尝试根据以下所示的路由示例将 2 个坐标之间的路线放到地图视图中:
https://developer.here.com/mobile-sdks/documentation/ios/topics/routing.html
有趣的是,如果我只调用地图,一切正常,但如果我添加路由部分,我会收到以下错误: NMAKit 致命:未设置许可证密钥、应用程序 ID 或应用程序代码。启动时出错,这很奇怪,因为凭据很好!所以我认为这个错误完全在我的 Swift 翻译中。
objective-C中的指令很清楚:
1.采用 NMARouteManagerDelegate 协议并创建 NMARouteManager:
@interface ClassName : NSObject <NMARouteManagerDelegate>
{
// Setup your class
}
(void)setup
{
Create a NMARouteManager.**
NMARouteManager* routeManager = [NMARouteManager sharedRouteManager];
// Setup delegate
[routeManager setDelegate:self];
}
2。创建一个 NSMutableArray 并添加两个 NMAGeoCoordinates 停靠点:
NSMutableArray* stops = [[NSMutableArray alloc] initWithCapacity:4];
NMAGeoCoordinates* geoCoord1 = [[NMAGeoCoordinates alloc]
initWithLatitude:49.1966286 longitude:-123.0053635];
NMAGeoCoordinates* geoCoord2 = [[NMAGeoCoordinates alloc]
initWithLatitude:49.1947289 longitude:-123.1762924];
[stops addObject:geoCoord1];
[stops addObject:geoCoord2];
3.创建一个 NMARoutingMode 并设置其 NMATransportMode、NMARoutingType 和 NMARoutingOption 值:
NMARoutingMode* routingMode = [[NMARoutingMode alloc]
initWithRoutingType:NMARoutingTypeFastest
transportMode:NMATransportModeCar
routingOptions:0];
4.计算路线:
[routeManager calculateRouteWithStops:stops routingMode:routingMode];
5.接收路由计算的结果,实现 NMARouteManagerDelegate 协议方法 routeManager:didCalculateRoutes:withError:violatedOptions: 在你的委托类中。
注意:即使您收到 NMARouteManagerErrorViolatesOptions 错误,也会返回路由。由您来处理这些违反路由选项的路由结果。
-(void) routeManager: (NMARouteManager*)routeManager
didCalculateRoutes:(NSArray*)routes
withError:(NMARouteManagerError)error
violatedOptions:(NSArray*)violatedOptions
{
// If the route was calculated successfully
if (!error && routes && routes.count > 0)
{
NMARoute* route = [routes objectAtIndex:0];
// Render the route on the map
mapRoute = [NMAMapRoute mapRouteWithRoute:route];
[mapView addMapObject:mapRoute];
}
else if (error)
{
// Display a message indicating route calculation failure
}
}
这就是我想要在 Swift 中做的事情:
import UIKit
//I changed the NMARouteManagerDelegate to my original class here
//and couldnt allow NSObject in the class delegation because it conflicts with UIViewController
class TestViewController: UIViewController, NMARouteManagerDelegate {
var mapCircle:NMAMapCircle?
@IBOutlet weak var mapView: NMAMapView!
@IBAction func get_route_action(sender: AnyObject) {
doRouting()
}
let routeManager = NMARouteManager.sharedRouteManager()
func doRouting() {
let geoCoord1 = NMAGeoCoordinates(latitude:41.350949, longitude:-74.182097)
let geoCoord2 = NMAGeoCoordinates(latitude:41.3437502, longitude:-74.1624284)
let stops = [geoCoord1, geoCoord2]
routeManager.calculateRouteWithStops(stops)
}
func routeManager(routeManager: NMARouteManager!, didCalculateRoutes routes: [AnyObject]!, withError error: NMARouteManagerError, violatedOptions: [AnyObject]!) {
print(routes)
print(error)
print(violatedOptions)
guard error == NMARouteManagerError.None else {
print("Route calculation error: \(error)")
return
}
guard let routes = routes, route = routes[0] as? NMARoute else {
print("Route calculation error: no routes")
return
}
let mapRoute = NMAMapRoute(route: route)
// Render the route on the map
mapView.addMapObject(mapRoute)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//mapView.useHighResolutionMap = true
var coordinates: NMAGeoCoordinates
coordinates = NMAGeoCoordinates(latitude: 41.350949, longitude: -74.182097)
mapView.zoomLevel = 13.2
mapView.setGeoCenter(coordinates, withAnimation: NMAMapAnimation.Linear)
mapView.copyrightLogoPosition = NMALayoutPosition.BottomCenter
addMapCircle()
}
func addMapCircle() {
if mapCircle == nil {
let coordinates: NMAGeoCoordinates =
NMAGeoCoordinates(latitude: 41.350949, longitude: -74.182097)
mapCircle = NMAMapCircle(geoCoordinates: coordinates, radius: 50)
mapView.addMapObject(mapCircle)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
【问题讨论】:
-
错误发生在哪一行?
-
Xcode 中没有错误!编译器对我的代码没有问题。我从 HERE 的 API 收到一个错误,我没有正确验证,这让我和 HERE 的人都感到困惑。我假设我的 Swift 版本与 Objective - C 指令没有一起正确发送。
-
但是 your 代码的哪一行会再次出现 HERE 错误?
-
抱歉耽搁了,在以下行:let routeManager = NMARouteManager.sharedRouteManager() 错误是:NMAKit FATAL: License Key, App ID, or App Code not set。 (lldb)
-
在该行设置断点,并在您认为设置了许可证密钥、应用程序 ID 和应用程序代码的行上设置断点,并查看实际上哪个首先执行。我没有看到您设置任何许可证密钥等,并且您的
let routeManager发生在您的 TestViewController 被实例化的那一刻,这让我认为您确实过早地调用了let routeManager。
标签: objective-c swift here-api