2014-10-01 156 views
0

我試圖對地址進行地址解析,在這種情況下,Google地址是HQ。解析從Google Maps API返回的JSON

當我試圖讓jQuery的$.ajax()請求得到JSON,它說:未捕獲的SyntaxError:意外的標記:我好像

無法揣摩出意外:是。我想要做的是將地址作爲一個字符串解碼lat和long,並使用Places API找到這些座標附近的機制。

$.ajax({ 
    url: 'https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=myKey', 
    dataType: 'jsonp', 
    jsonp: 'callback', 
    method: 'GET', 
    success: function(results){ 
     console.log(results); 
     queryLat = results['results']['geometry']['location']['lat']; 
     queryLong = results['results']['geometry']['location']['lng']; 
     console.log('latitude: '+queryLat); 
     console.log('longitude: '+queryLong); 


     function callback(mapsResults, status){ 
      if(status == google.maps.places.PlacesServiceStatus.OK){ 
       for(var i = 0; i < mapResults.length; i++){ 
        var place = mapResultsi]; 
        createMarker(mapResults[i]); 
       } 
      } 
     } 
     var map; 
     var service; 

     map = new google.maps.Map(document.getElementById('map'), { 
      center: new google.maps.LatLng(queryLat, queryLong), 
      zoom: 15 
     }); 

     var request ={ 
      location: new google.maps.LatLng(queryLat, queryLong) 
      radius:'500', 
      query:'mechanic' 
     }; 

     service = new google.maps.places.PlacesService(map); 
     service.textSearch(request, callback); 
}); 
+1

'var place = mapResultsi];'應該是'var place = mapResults [i];' – 2014-10-01 21:08:24

+1

''location:new google.maps.LatLng(queryLat,queryLong)'' – 2014-10-01 21:13:46

+0

@PaulRoub謝謝。 – wordSmith 2014-10-01 21:15:12

回答

1

三個問題:

  1. 告訴$.ajax()期待jsonp當你location: new google.maps.LatLng(queryLat, queryLong)
  2. 後調用的JSON API返回
  3. var place = mapResultsi];應該var place = mapResults[i];
  4. 缺少一個逗號
0
function callback(mapsResults, status){ 
      if(status == google.maps.places.PlacesServiceStatus.OK){ 
       for(var i = 0; i < mapResults.length; i++){ 
        **var place = mapResultsi];** 
        createMarker(mapResults[i]); 
       } 
      } 
     } 
相關問題