2011-02-10 34 views
1

我試圖讓使用此代碼谷歌地圖JSON響應:jQuery的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!'); 
     } 
    } 
}); 

它的工作真的很好,但是......

,結果我是越來越

未捕獲的SyntaxError:意外的標記:在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 
    } 
    } 
} 

}] }

回答

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!'); 
    } 

} });

+1

看來,沒有選擇通過jsonp來做到這一點http://stackoverflow.com/questions/844341/google-geocode-via-http-callback-function – kirilloid 2011-02-10 10:10:43