我使用api v3.1,正在尝试计算2个点之间的距离
这就是我所拥有的
<script src="https://js.api.here.com/v3/3.1/mapsjs-core.js"
type="text/javascript" charset="utf-8"></script>
<script src="https://js.api.here.com/v3/3.1/mapsjs-service.js"
type="text/javascript" charset="utf-8"></script>
var platform = new H.service.Platform({
'apikey': 'key'
})
const params = {
mode: 'car;',
waypoint0: lat1 + ',' + lon1,
waypoint1: lat2 + ',' + lon2,
representation: 'display',
routeAttributes: 'summary'
}
var routingService = platform.getRoutingService()
routingService.calculateRoute(params, success => {
console.log(success.response.route[0].summary)
}, error => {
console.log(error)
})
但是我得到这个错误:
{
_type: "ns2:RoutingServiceErrorType",
type: "PermissionError",
subtype: "InvalidCredentials",
details: "Unauthorized. The request is not from an authorized source.",
metaInfo: {
timestamp: "2019-12-04T16:02:45Z",
mapVersion: "8.30.103.150",
moduleVersion: "7.2.201948-5874",
interfaceVersion: "2.6.74",
availableMapVersion: [
"8.30.103.150"
]
}
}
我尝试使用域白名单,而没有
答案 0 :(得分:0)
您可以尝试执行此代码吗?
var platform = new H.service.Platform({
'apikey': 'api_key'
});
//you can remove this if you dont want to see the map.
var maptypes = platform.createDefaultLayers();
var map = new H.Map(
document.getElementById('mapContainer'),
maptypes.vector.normal.map,
{
zoom: 10,
center: { lng: 13.4, lat: 52.51 }
});
// Till here
const params = {
mode: 'fastest;car',
waypoint0: '52.5160,13.3779',
waypoint1: '52.5206,13.3862',
representation: 'display',
routeAttributes: 'summary'
}
function calcuateRouteFromAtoB(platform){
var routingService = platform.getRoutingService()
routingService.calculateRoute(params, success => {
console.log(success.response.route[0].summary);
}, error => {
console.log(error);
})
}
calcuateRouteFromAtoB(platform);
希望这对您有帮助!
答案 1 :(得分:0)
您的apikey肯定有问题。要么无效,要么它们插入方式不正确。
我用apikey尝试了您的代码,它看起来不错(只需用您自己的apikey值替换'your_apikey_string'):
const apikey = 'your_apikey_string';
var platform = new H.service.Platform({
'apikey': apikey
})
var params = {
mode: 'fastest;car',
waypoint0: 49 + ',' + 19,
waypoint1: 49 + ',' + 19.5,
representation: 'display',
routeAttributes: 'summary'
}
var routingService = platform.getRoutingService()
routingService.calculateRoute(params, success => {
console.log(success.response.route[0].summary)
}, error => {
console.log(error)
})