【问题标题】:Swift Here API ExamplesSwift Here API 示例
【发布时间】: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


【解决方案1】:

我试过你的代码,基本上对我来说工作得很好。

但我还在 AppDelegate.swift 中添加了凭据:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        NMAApplicationContext.setAppId(YourAppID, appCode: YourToken, licenseKey: YourKey);
        return true;
    }

这很关键,因为如果它丢失了,它就会抛出你得到的错误。

【讨论】:

  • 哇,这太奇怪了,因为我的凭据在 AppDelegate.swift 中完全如此设置。您确定您完全按照我在上面发布的代码尝试了我的代码吗?请再次回复。因为如果是这样,那么我的凭据必须与常规映射一起使用,但未被批准用于路由。我现在就和 HERE 的人一起解决这个问题。
  • Marco,请再次回答我。您是否尝试过我的确切 Swift 代码,或者您的意思是说它可以在 Objective C 中工作?
  • 我采用了这个 swift 示例 tcs.ext.here.com/sdk_examples/SwiftExample.zip 并添加了您的路线计算和委托代码。
  • 确保您设置了代码、令牌和许可证密钥。一些较旧的示例有时会丢失许可证密钥。
  • 现在一切正常。有一些事情发生了。主要是你不能从类的开始立即调用 doRouting() 函数,因为它当时还没有地图。身份验证没有任何问题。我现在有完整的 Swift 工作代码。如果有人想看,请联系我。
猜你喜欢
  • 2020-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
相关资源
最近更新 更多