Javascript json api调用

Javascript json api调用,javascript,Javascript,我想通过传递一个城市名称(硬编码)对JSON()进行api调用,并获得纬度和经度作为回报。我尝试了下面提到的代码 function loadJSON(callback) { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("application/json"); xobj.open("GET", "https://maps.googleapis.com/maps/api/geocode/json?address=Pune

我想通过传递一个城市名称(硬编码)对JSON()进行api调用,并获得纬度和经度作为回报。我尝试了下面提到的代码

function loadJSON(callback) {

  var xobj = new XMLHttpRequest();
  xobj.overrideMimeType("application/json");
  xobj.open("GET", "https://maps.googleapis.com/maps/api/geocode/json?address=Pune", true);
  xobj.onreadystatechange = function() {
    if (xobj.readyState == 4 && xobj.status == "200") {
      // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
      callback(xobj.responseText);
    };
    xobj.send();
  }

  function init() {
    loadJSON(function(response) {
      // Parse JSON string into object
      var myObj = JSON.parse(this.responseText);

      alert(myObj.results[0].geometry.bounds.northeast.lat);
      alert(myObj.results[0].geometry.bounds.northeast.lng);
      alert(myObj.results[0].geometry.bounds.southwest.lat);
      alert(myObj.results[0].geometry.bounds.southwest.lng);
      alert(myObj.results[0].geometry.location.lat);
      alert(myObj.results[0].geometry.location.lng);

      alert(myObj.results[0].geometry.viewport.northeast.lat);
      alert(myObj.results[0].geometry.viewport.northeast.lng);
      alert(myObj.results[0].geometry.viewport.southwest.lat);
      alert(myObj.results[0].geometry.viewport.southwest.lng);

    });
  }

您的代码中存在一些问题:

  • 您缺少
    loadJSON
    的右大括号
  • xobj.send()
    位于错误的位置。。。它需要在
    onreadystatechange
    函数之外
  • 您在回调中使用的是
    this.responseText
    ,而不是传入的参数
  • 我修复了这些问题,将
    警报
    s转换为
    控制台.log
    s,并摆脱了不必要的
    覆盖类型
    调用。工作代码如下:

    函数loadJSON(回调){ var xobj=新的XMLHttpRequest(); xobj.open(“GET”https://maps.googleapis.com/maps/api/geocode/json?address=Pune“,对); xobj.onreadystatechange=函数(){ if(xobj.readyState==4&&xobj.status==200){ 回调(xobj.responseText); }; } xobj.send(); } 函数init(){ loadJSON(函数(responseText){ var myObj=JSON.parse(responseText); console.log(myObj.results[0].geometry.bounds.northeast.lat); console.log(myObj.results[0].geometry.bounds.northeast.lng); log(myObj.results[0].geometry.bounds.soutwest.lat); log(myObj.results[0].geometry.bounds.soutwest.lng); console.log(myObj.results[0].geometry.location.lat); log(myObj.results[0].geometry.location.lng); console.log(myObj.results[0].geometry.viewport.northeast.lat); console.log(myObj.results[0].geometry.viewport.northeast.lng); console.log(myObj.results[0].geometry.viewport.soutwest.lat); console.log(myObj.results[0].geometry.viewport.soutwest.lng); }); }
    init()您已经共享了一些代码,但尚未描述您遇到的问题。有错误吗?你看到输出了吗?输出是否与您预期的不同?另外,看起来您缺少了
    loadJSON
    的右大括号。这是您试图运行的实际代码吗?您的
    xobj.send()
    也位于错误的位置。嗨,smarx!是的,我面临一个错误。另外,预期的输出应该是查询字符串中发送的城市的纬度和经度。此外,我需要将这些晶格和经度传递给另一个api调用以获得格式化的地址。您能告诉我如何使用此调用的输出来打印格式化的地址吗?谢谢smarx!!:)我还需要知道是否有一种方法可以将纬度和经度值作为参数传递,然后使用它们从JSON获取格式化的地址。