google 本地主机上的距离矩阵
google distance matrix on localhost
我正在尝试使用 google 距离矩阵来找出从一个源到一个目的地的距离和时间。
我正在调用函数
$('#postCode').change(function () {
var address = "sydney";
var source = "melbourne"
var url = "https://maps.googleapis.com/maps/api/distancematrix/js?units=imperial&origins=" + address + "&destinations=" + source + "&key=MYKEY_HERE";
$.get(url, function (data) {
window.alert(data);
});
});
我只需要获取返回的任何数据,但问题是每当我加载包含 link
的页面时
<script src="https://maps.google.com/maps/api/js?sensor=false?key=MYKEY_HERE"></script>
或触发邮政编码字段的更改,它在控制台中显示以下 2 个错误
Google 地图 API 错误:MissingKeyMapError
请求中没有 'Access-Control-Allow-Origin' header
资源。因此不允许来源'http://localhost:60197'
访问.
还有两个警告
util.js:210 Google 地图 API 警告:NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys
util.js:210 Google 地图 API 警告:SensorNotRequired https://developers.google.com/maps/documentation/javascript/error-messages#sensor-not-required
哪里做错了,或者实现所需输出(两点之间的距离和时间)的正确方法是什么。
不要使用 DistanceMatrix web service from javascript on the client, use the Google Maps Javascript API v3 DistanceMatrix Service。
$('#postCode').change(function () {
var address = "sydney";
var source = "melbourne"
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [address],
destinations: [source],
travelMode: 'DRIVING',
unitSystem: google.maps.UnitSystem.IMPERIAL,
}, callback);
function callback(response, status) {
// See Parsing the Results for
// the basics of a callback function.
}
});
我正在尝试使用 google 距离矩阵来找出从一个源到一个目的地的距离和时间。
我正在调用函数
$('#postCode').change(function () {
var address = "sydney";
var source = "melbourne"
var url = "https://maps.googleapis.com/maps/api/distancematrix/js?units=imperial&origins=" + address + "&destinations=" + source + "&key=MYKEY_HERE";
$.get(url, function (data) {
window.alert(data);
});
});
我只需要获取返回的任何数据,但问题是每当我加载包含 link
的页面时<script src="https://maps.google.com/maps/api/js?sensor=false?key=MYKEY_HERE"></script>
或触发邮政编码字段的更改,它在控制台中显示以下 2 个错误
Google 地图 API 错误:MissingKeyMapError
请求中没有 'Access-Control-Allow-Origin' header
资源。因此不允许来源'http://localhost:60197'
访问.
还有两个警告
util.js:210 Google 地图 API 警告:NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys
util.js:210 Google 地图 API 警告:SensorNotRequired https://developers.google.com/maps/documentation/javascript/error-messages#sensor-not-required
哪里做错了,或者实现所需输出(两点之间的距离和时间)的正确方法是什么。
不要使用 DistanceMatrix web service from javascript on the client, use the Google Maps Javascript API v3 DistanceMatrix Service。
$('#postCode').change(function () {
var address = "sydney";
var source = "melbourne"
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [address],
destinations: [source],
travelMode: 'DRIVING',
unitSystem: google.maps.UnitSystem.IMPERIAL,
}, callback);
function callback(response, status) {
// See Parsing the Results for
// the basics of a callback function.
}
});