2012-02-07 11 views
1

私はAJAXによってGoogle Maps APIから標高データを取得しています。JSONスローエラーを取得しています:予期しないトークン:

私はChromeのコンソールを見ているかのように私は200のステータスコードを見ることができ、応答タブでデータを見ることができるようにデータを戻しています。しかし、 'Uncaught SyntaxError:Unexpected token:'がスローされ、JSONファイルから何も表示できません。

これは私のコードです:

var theURL = 'http://maps.googleapis.com/maps/api/elevation/json?locations=' + longitude + ',' + latitude + '&sensor=false&callback=?';  
$.ajax({ 
     url: theURL, 
     type: 'get', 
     dataType: 'json', 
     cache: false, 
     crossDomain: true, 
     success: function (data) { 
      var theData = $.parseJSON(data); 
      console.log(theData); 
     } 
    }); 

ライブコードはここにある:http://map.colouringcode.com/

すべてのヘルプは大歓迎です。

答えて

0

Google Maps APIは直接JSONPリクエストをサポートしていません。

代わりにJavascript APIを使用する必要があります。

+0

JSONPリクエストを受け入れない場合、データを取得するにはどうすればよいですか? –

+0

代わりに、[Javascript API](http://code.google.com/apis/maps/documentation/javascript/elevation.html)を使用する必要があります。 – SLaks

0

この質問を実現するには時代遅れですが、これが将来誰かを助けることができると願っています。これはjavascriptとの最初の出会いで、特にこのJSONのすべてのものだったので、私が間違っていたことを理解しようとすると、机の外に頭がぶつかってしまいました。

クライアントの場所(緯度と経度)を取得し、GoogleのジオコーディングAPIを使用してその場所を「人間が判読可能な」形式で特定するソリューションです。

function currentLocation() { 
    navigator.geolocation.getCurrentPosition(foundLocation, errorLocation); 

    function foundLocation(position) { 
     var lat = position.coords.latitude; 
     var lng = position.coords.longitude; 

     //This is where the geocoding starts 
     var locationURL= "http://maps.googleapis.com/maps/api/geocode/json?latlng=" 
      + lat + "," + lng + "&sensor=false&callback=myLocation" 

     //This line allows us to get the return object in a JSON 
     //instead of a JSONP object and therefore solving our problem 
     $.getJSON(locationURL, myLocation); 
    } 

    function errorLocation() { 
     alert("Error finding your location."); 
    } 
} 

function myLocation(locationReturned) { 
    var town = locationReturned.results[0].address_components[1].short_name; 
    var state = locationReturned.results[0].address_components[4].short_name; 
    console.log(town, state); 
} 
関連する問題