简体   繁体   English

JavaScript Google Geocode返回未定义(无法计算回调函数)

[英]Javascript Google Geocode Returning Undefined (Can't figure callback function)

I'm trying to store the values of the latitude and the longitude from the response that I get from the Google Geocoding API. 我正在尝试从Google Geocoding API获得的响应中存储纬度和经度的值。 Right now I am using a fixed location in California. 现在,我在加利福尼亚使用固定位置。 My HTTP request keeps returning undefined. 我的HTTP请求不断返回未定义。

I've been looking around a lot and I see that people have this same problem, but I can't wrap my head around making a callback function which seems to be the solution, could anybody explain to me how to do this? 我一直到处逛逛,我发现人们也有同样的问题,但是我不能全力以赴地制作一个似乎可以解决问题的回调函数,有人可以向我解释如何做到这一点吗? I know that my CORS request is working because I am using it to talk GET from a Heroku server that I set up. 我知道我的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() 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;
}

Assuming createCORSRequest returns an xhr or xhr -like object (which seems to be the typical boilerplate for createCORSRequest from places like the HTML5 Rocks website), you need to include geocode.send(); 假设createCORSRequest返回一个xhrcreateCORSRequest xhr的对象(这似乎是来自HTML5 Rocks网站的createCORSRequest的典型样板),则需要包括geocode.send(); at the end of your code. 在代码末尾。 Otherwise the request never fires and therefore the onload handler never gets called. 否则,该请求永远不会触发,因此永远不会调用onload处理程序。

Look like you're following HTML5Rocks site Using CORS article. 您似乎正在关注HTML5Rocks网站“ 使用CORS”一文。

Once you create the xhr request. 创建xhr请求后。 You must invoke send() method to trigger the ajax call. 您必须调用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();

Unless you call geocode.send() , the value will be undefined as no request has been fired. 除非您调用geocode.send() ,否则该值将是未定义的,因为未触发任何请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM