2016-07-26 93 views
-2

的財產我有Ajax請求如下的谷歌地圖API,如何訪問對象

$.ajax({ 
    type: "POST", 
    async: false, 
    dataType: "json", 
    url: 'https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyDWPSMsE_CuOAk5iKJ48ReJQYRe9NUn_mo&address='35010'&sensor=false', 
    success: function (response) { 

    console.log(response); 

    var latlong = JSON.stringify(response.results[0].geometry.filter(function(obj){ 
    return obj.types.indexOf("location") != -1 
    })[0].lat, null, 4); 

    latlong = latlong.replace(/['"]+/g, ''); 

    alert(latlong); 

    } 
}); 

而且我得到了以下JSON響應,

{ 
    "results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "35010", 
       "short_name" : "35010", 
       "types" : [ "postal_code" ] 
      }, 
      { 
       "long_name" : "Alexander City", 
       "short_name" : "Alexander City", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "Alabama", 
       "short_name" : "AL", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "Alexander City, AL 35010, USA", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 33.1044549, 
        "lng" : -85.76817199999999 
       }, 
       "southwest" : { 
        "lat" : 32.703111, 
        "lng" : -86.050735 
       } 
      }, 
      "location" : { 
       "lat" : 32.94464350000001, 
       "lng" : -85.91000889999999 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 33.1044549, 
        "lng" : -85.76817199999999 
       }, 
       "southwest" : { 
        "lat" : 32.703111, 
        "lng" : -86.050735 
       } 
      } 
     }, 
     "place_id" : "ChIJgSRw98ETjIgROEXAlUkHoR8", 
     "postcode_localities" : [ "ALEX CITY", "Alexander City" ], 
     "types" : [ "postal_code" ] 
     } 
    ], 
    "status" : "OK" 
} 

我想LAT長值(在「location」字段中)。但是,當我運行代碼我得到了「jquery.min.js:4 Uncaught TypeError: response.results[0].geometry.filter is not a function"

請幫助。我是新來的JavaScript。

+0

請有什麼建議嗎? –

+0

等待我試圖解決你的問題 – mean

+1

'filter'是一個數組上的方法,'response.results [0] .geometry'是一個普通對象,而不是數組。 – Quentin

回答

0
$.ajax({ 
     type: "POST", 
     async: false, 
     dataType: "json", 
     url: 'https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyDWPSMsE_CuOAk5iKJ48ReJQYRe9NUn_mo&address='35010'&sensor=false', 
     success: function (response) { 

     console.log(response); 
    if(response.results[0].geometry.location != null){ 
     var latlong = response.results[0].geometry.location.lat +','+response.results[0].geometry.location.lng 

} 

     alert(latlong); 

     } 
    }); 

請檢查是否正常工作

0

從源,以獲得緯度和經度在位置=>幾何形狀,嘗試

$.ajax({ 
 
    type: "POST", 
 
    async: false, 
 
    dataType: "json", 
 
    url: 'https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyDWPSMsE_CuOAk5iKJ48ReJQYRe9NUn_mo&address=35010&sensor=false', 
 
    success: function (response) { 
 
    console.log(response); 
 

 
    //var latlong = JSON.stringify(response.results.geometry.filter(function(obj){ return obj.types.indexOf("location") != -1})[0].lat, null, 4); 
 

 
    //latlong = latlong.replace(/['"]+/g, ''); 
 

 
    alert(response.results[0].geometry.location.lat); 
 
    alert(response.results[0].geometry.location.lng); 
 

 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0

您可以簡單地訪問latlong

var latlong = response.results[0].geometry.location.lat+","+response.results[0].geometry.location.lng;