我将HERE SDK整合到我的应用中。除了一个简单的地图设置,HERE网站上的所有示例都以Objective-C显示,我尽我所能将它们翻译成Swift,但它还没有100%正常工作。我试图将两个坐标之间的路线放到地图视图上,按照其显示的路由示例显示:
https://developer.here.com/mobile-sdks/documentation/ios/topics/routing.html
有趣的是,如果我只是调用地图一切正常,但如果我添加路由部分,我会收到以下错误: NMAKit致命错误:未设置许可证密钥,应用程序ID或应用程序代码。启动错误 这很奇怪,因为凭据很好!所以我认为这个bug完全在我的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:violateOptions:在你的委托类中。
注意:即使收到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.
}
}
答案 0 :(得分:1)
我尝试了你的代码,基本上对我工作很好。
但我还在AppDelegate.swift中添加了凭证:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NMAApplicationContext.setAppId(YourAppID, appCode: YourToken, licenseKey: YourKey);
return true;
}
这很关键,因为如果它丢失了,它就会完全抛出你得到的错误。