我正在尝试使用Here Maps 3.1版的api,尤其是地理编码服务。
我已按照步骤获得api密钥,然后尝试使用documentation的示例代码访问地址解析器服务。
我用新的apikey替换了{YOUR_API_KEY}
,并将示例代码放入了新的index.html文件中:
<html>
<head>
<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>
</head>
<body>
<div id="mapContainer"></div>
<script>
// Instantiate a map and platform object:
var platform = new H.service.Platform({
apikey: "MY_API_KEY"
});
// Retrieve the target element for the map:
var targetElement = document.getElementById("mapContainer");
// Get default map types from the platform object:
var defaultLayers = platform.createDefaultLayers();
// Instantiate the map:
var map = new H.Map(
document.getElementById("mapContainer"),
defaultLayers.vector.normal.map,
{
zoom: 10,
center: { lat: 52.51, lng: 13.4 }
}
);
// Create the parameters for the geocoding request:
var geocodingParams = {
searchText: "200 S Mathilda Ave, Sunnyvale, CA"
};
// Define a callback function to process the geocoding response:
var onResult = function(result) {
var locations = result.Response.View[0].Result,
position,
marker;
// Add a marker for each location found
for (i = 0; i < locations.length; i++) {
position = {
lat: locations[i].Location.DisplayPosition.Latitude,
lng: locations[i].Location.DisplayPosition.Longitude
};
marker = new H.map.Marker(position);
map.addObject(marker);
}
};
// Get an instance of the geocoding service:
var geocoder = platform.getGeocodingService();
// Call the geocode method with the geocoding parameters,
// the callback and an error callback function (called if a
// communication error occurs):
geocoder.geocode(geocodingParams, onResult, function(e) {
alert(e);
});
</script>
<style>
#mapContainer {
width: 100%;
height: 300px;
}
</style>
</body>
</html>
当我在浏览器中打开此文件时,我可以看到包含城市名称和所有内容的地图,因此我认为我有权访问地图api,但是地址解析服务失败并出现401错误:{"error":"Unauthorized","error_description":"ApiKey invalid. ApiKey not found."}
>
但是我的apikey在我的项目页面中已启用,我已经检查过并且我没有拼错apikey(我已经多次复制/粘贴了它以确保)。
您有什么建议吗?
谢谢。
答案 0 :(得分:1)
在添加ApiKey时,请始终牢记删除键前后的多余空间,并同时删除{}。
https://developer.here.com/documentation/maps/dev_guide/topics/geocoding.html