利用与距离矩阵中的谷歌应用程序API密钥 [英] Using a Google Apps API key with Distance Matrix
问题描述
我使用的是谷歌距离矩阵API 和文档告诉我,我需要API密钥(但我可以使用它没有之一)。我希望能够监控使用,但我难倒如何设置它。
I am using the Google Distance Matrix API and the documentation tells me I need an API key (but I can use it without one.) I'd like to be able to monitor use but I'm stumped as to how to set it up.
我已经从谷歌开发者控制台有效的浏览器应用程序API密钥,它是新的,所以我假定这是一个版本的3键。
I have a valid browser application API key from the Google Developers Console, it's new so I'm assuming it's a version 3 key.
我在控制台添加有效的参照网址
I have added valid referers in the console
我<脚本SRC =https://maps.googleapis.com/maps/api/js?v=3.exp>< / SCRIPT>
我的网页上
我使用code这样的
function callback(response, status) {
if (status!==google.maps.DistanceMatrixStatus.OK) {
_googleError('Error was: ' + status);
} else {
var origins = response.originAddresses;
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
$("#calcDistance").val(results[j].distance.text);
//Other stuff that works here
}
}
}
}
function calculateDistances(start, end) {
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [start],
destinations: [end],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL,
avoidHighways: false,
avoidTolls: false
}, callback);
}
由于这是工作的事情就好了。当我尝试的事情南下补充的关键。我试过
As it is things work just fine. When I try to add the key in things go south. I've tried
&LT;脚本SRC =https://maps.googleapis.com/maps/api/js?key={MY_KEY}&v=3.exp&GT;&LT; / SCRIPT&GT ;
和
&LT;脚本SRC =https://maps.googleapis.com/maps/api/js?key={MY_KEY}&GT;&LT; / SCRIPT&GT;
没有运气。当我这样做不是那些我得到无效的URL,<一个错误href=\"http://stackoverflow.com/questions/12456714/google-distance-matrix-api-v3-showing-error\">similar对这个问题。
with no luck. When I do either of those I get an error about invalid URL, similar to this question.
我也尝试添加键:{} MY_KEY,
到 calculateDistances()
- 与没有运气无论是。
I've also tried adding key: {MY_KEY},
into calculateDistances()
- no luck with that either.
我失去了一些东西明显? (我觉得我)
Am I missing something obvious? (I feel like I am)
更新:
@ Dr.Molle的回答让我什么,我一直在寻找。我打开谷歌地图API的JavaScript V3,改变了&LT;脚本SRC =https://maps.googleapis.com/maps/api/js?v=3.exp&GT;&LT; / SCRIPT&GT;
到&LT;脚本SRC =https://maps.googleapis.com/maps/api/js?key={MY_KEY}&v=3。 EXP&GT;&LT; / SCRIPT&GT;
现在我可以在开发者控制台查看活动像我想
@Dr.Molle's answer got me what I was looking for. I turned on "Google Maps JavaScript API v3" and changed <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
to <script src="https://maps.googleapis.com/maps/api/js?key={MY_KEY}&v=3.exp"></script>
Now I can view activity in the Developer Console like I wanted.
推荐答案
当您使用加载地图,Javascript的API,您必须启用控制台内部的API谷歌地图的JavaScript API V3时的关键。
When you use the key when loading the maps-Javascript-API you must enable the API "Google Maps JavaScript API v3" inside the console.
链接的文档为WebService,当你请求通过JavaScript的API的DistanceMatrixService本文档的关键相关的部分是无关紧要的。
The linked documentation is for the Webservice, the key-related part of this documentation is irrelevant when you request the DistanceMatrixService via the javascript-API.
正确的文档,你会发现在 https://developers.google.com/图/文档/ JavaScript的/ distancematrix
The correct documentation you'll find at https://developers.google.com/maps/documentation/javascript/distancematrix
这篇关于利用与距离矩阵中的谷歌应用程序API密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!