Javascript Google 地理编码返回未定义(无法计算回调函数)
Javascript Google Geocode Returning Undefined (Can't figure callback function)
我正在尝试存储我从 Google 地理编码 API 获得的响应中的纬度和经度值。现在我在加利福尼亚使用固定位置。我的 HTTP 请求一直返回 undefined。
我四处张望,发现人们遇到了同样的问题,但我无法全神贯注地制作一个似乎是解决方案的回调函数,有人可以向我解释如何做这个?我知道我的 CORS 请求正在运行,因为我正在使用它从我设置的 Heroku 服务器获取 GET。
var geocode = new XMLHttpRequest();
geocode = createCORSRequest('GET', 'https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=');
geocode.onload = function() {
var geoData = JSON.parse(geocode.responseText); // parse the JSON from geocode response
var results = geoData["results"]; // create variable for results
var userLong = results["geometry"]["location"]["lng"]; // parse the latitude
var userLat = results["geometry"]["location"]["lat"]; // parse the longitude
console.log(userLong);
console.log(userLat);
}
createCORSRequest()
// Create the XHR object.
function createCORSRequest(method, url) {
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, false);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
假设 createCORSRequest
returns 一个 xhr
或 xhr
类对象(这似乎是来自 HTML5 等地方的 createCORSRequest 的典型样板Rocks 网站),您需要在代码末尾包含 geocode.send();
。否则请求永远不会触发,因此 onload
处理程序永远不会被调用。
看起来您正在关注 HTML5Rocks
网站 Using CORS 文章。
创建 xhr
请求后。您必须调用 send()
方法来触发 ajax 调用。
var geocode = new XMLHttpRequest();
geocode = createCORSRequest('GET', 'https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=');
geocode.onload = function() {
var geoData = JSON.parse(geocode.responseText); // parse the JSON from geocode response
var results = geoData["results"]; // create variable for results
var userLong = results["geometry"]["location"]["lng"]; // parse the latitude
var userLat = results["geometry"]["location"]["lat"]; // parse the longitude
console.log(userLong);
console.log(userLat);
}
geocode.send();
除非您调用 geocode.send()
,否则该值将是未定义的,因为尚未触发任何请求。
我正在尝试存储我从 Google 地理编码 API 获得的响应中的纬度和经度值。现在我在加利福尼亚使用固定位置。我的 HTTP 请求一直返回 undefined。
我四处张望,发现人们遇到了同样的问题,但我无法全神贯注地制作一个似乎是解决方案的回调函数,有人可以向我解释如何做这个?我知道我的 CORS 请求正在运行,因为我正在使用它从我设置的 Heroku 服务器获取 GET。
var geocode = new XMLHttpRequest();
geocode = createCORSRequest('GET', 'https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=');
geocode.onload = function() {
var geoData = JSON.parse(geocode.responseText); // parse the JSON from geocode response
var results = geoData["results"]; // create variable for results
var userLong = results["geometry"]["location"]["lng"]; // parse the latitude
var userLat = results["geometry"]["location"]["lat"]; // parse the longitude
console.log(userLong);
console.log(userLat);
}
createCORSRequest()
// Create the XHR object.
function createCORSRequest(method, url) {
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, false);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
假设 createCORSRequest
returns 一个 xhr
或 xhr
类对象(这似乎是来自 HTML5 等地方的 createCORSRequest 的典型样板Rocks 网站),您需要在代码末尾包含 geocode.send();
。否则请求永远不会触发,因此 onload
处理程序永远不会被调用。
看起来您正在关注 HTML5Rocks
网站 Using CORS 文章。
创建 xhr
请求后。您必须调用 send()
方法来触发 ajax 调用。
var geocode = new XMLHttpRequest();
geocode = createCORSRequest('GET', 'https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=');
geocode.onload = function() {
var geoData = JSON.parse(geocode.responseText); // parse the JSON from geocode response
var results = geoData["results"]; // create variable for results
var userLong = results["geometry"]["location"]["lng"]; // parse the latitude
var userLat = results["geometry"]["location"]["lat"]; // parse the longitude
console.log(userLong);
console.log(userLat);
}
geocode.send();
除非您调用 geocode.send()
,否则该值将是未定义的,因为尚未触发任何请求。