将 Google Apps API 密钥与距离矩阵结合使用 [英] Using a Google Apps API key with Distance Matrix
问题描述
我正在使用 Google 距离矩阵 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.
我有一个来自 Google Developers Console 的有效浏览器应用程序 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
我的页面上有 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
我正在使用这样的代码
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
<script src="https://maps.googleapis.com/maps/api/js?key={MY_KEY}&v=3.exp"></script>代码>
和
<script src="https://maps.googleapis.com/maps/api/js?key={MY_KEY}"></script>
没有运气.当我执行其中任何一项时,我都会收到有关无效 URL 的错误,类似于此问题.
with no luck. When I do either of those I get an error about invalid URL, similar to this question.
我也尝试将 key: {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 的回答让我得到了我想要的.我打开了Google Maps JavaScript API v3"并更改了 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
到 <script src="https://maps.googleapis.com/maps/api/js?key={MY_KEY}&v=3.exp"></script>
现在我可以在开发者控制台中随意查看活动了.
@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.
推荐答案
当您在加载 maps-Javascript-API 时使用密钥时,您必须在控制台内启用 APIGoogle Maps 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.
链接的文档是针对 Web 服务的,当您通过 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
这篇关于将 Google Apps API 密钥与距离矩阵结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!