【发布时间】:2019-07-18 14:06:57
【问题描述】:
我正在尝试使用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(我复制/粘贴了很多次以确定)。
你有什么建议吗?
谢谢。
【问题讨论】:
-
您好,您可以尝试创建一个新的 API KEY 并再次测试吗?
-
您好,我试过 3 个键,但没有用,但奇怪的是昨天它开始工作了......我不知道为什么,因为我没有改变任何东西
-
工作真好!
标签: javascript here-api