【问题标题】:jQuery JSON parse google maps problemjQuery JSON解析谷歌地图问题
【发布时间】:2011-02-10 08:39:24
【问题描述】:

我正在尝试使用此代码从谷歌地图获取 json 响应:

var address = 'Sydney,NSW';
$.ajax({  
    type: "GET",  
    url: "http://maps.googleapis.com/maps/api/geocode/json?sensor=false",  
    dataType: "jsonp",
    data: ({"address":address}),
    success: function(json){
        if (json.status == "OK") {
           alert(":)");   
        } else {
            alert('wrong!');
        }
   }
});

效果很好,但是...

结果我得到了

Uncaught SyntaxError: Unexpected token: 在 Chrome 和 错误:在 FireFox 中找不到元素..

返回的 JSON 似乎有问题,但看起来不错:

{  
 "status": "OK",
 "results": [ {
 "types": [ "locality", "political" ],
 "formatted_address": "Sydney New South Wales, Australia",
   "address_components": [ {
   "long_name": "Sydney",
   "short_name": "Sydney",
   "types": [ "locality", "political" ]
  }, {
   "long_name": "New South Wales",
   "short_name": "New South Wales",
   "types": [ "administrative_area_level_1", "political" ]
}, {
  "long_name": "Australia",
  "short_name": "AU",
  "types": [ "country", "political" ]
} ],
"geometry": {
  "location": {
    "lat": -33.8689009,
    "lng": 151.2070914
  },
  "location_type": "APPROXIMATE",
  "viewport": {
    "southwest": {
      "lat": -34.1648540,
      "lng": 150.6948538
    },
    "northeast": {
      "lat": -33.5719182,
      "lng": 151.7193290
    }
  },
  "bounds": {
    "southwest": {
      "lat": -34.1692489,
      "lng": 150.5022290
    },
    "northeast": {
      "lat": -33.4245980,
      "lng": 151.3426361
    }
  }
}

}] }

【问题讨论】:

    标签: javascript jquery json api google-maps


    【解决方案1】:

    返回必须是 jsonp 而不是 json(json 应该被包装到函数调用中)

    尝试使用GM2(GM3似乎不支持jsonp)

    $.ajax({  
    type: "GET",  
    url: "http://maps.google.com/maps/geo?sensor=false&output=json&"+$.param({"q":address}),  
    dataType: "jsonp",
    success: function(json){
        if (json.Status.code == "200") {
           alert(":)");   
        } else {
            alert('wrong!');
        }
    

    } });

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    相关资源
    最近更新 更多