admin管理员组文章数量:1315291
I am trying to learn about Google Maps Geocoding and how to read the JSON data it sends back. This is what Google sends back:
{
"results" : [
{
"address_ponents" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4224764,
"lng" : -122.0842499
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238253802915,
"lng" : -122.0829009197085
},
"southwest" : {
"lat" : 37.4211274197085,
"lng" : -122.0855988802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
I get this response with this code:
$("#submit").click(function(event) {
var address = encodeURIComponent($("#location").val());
$.ajax({
type: "GET",
url: "=" + address + "&sensor=false&key=" + API_KEY,
dataType: "json",
success: processJSON
});
function processJSON(json) {
// Do stuff here
}
});
The problem is I don't know what to put inside processJSON
. I was reading the tutorial here: / and the documentation on .getJSON()
on .getjson/. However, I'm having trouble understanding both sites.
If I wanted to get the postal_code
, how would I do read the data? I tried this:
function processJSON(json) {
// Do stuff here
alert("Postal Code:" + json.address_ponents[6].long_name);
}
I tried it with getJSON()
as well after success
:
$.getJSON(url, function(json) {
alert("Postal Code:" + json.address_ponents[6].long_name);
});
However, neither alert anything. What am I doing wrong? Thanks in advance!
I am trying to learn about Google Maps Geocoding and how to read the JSON data it sends back. This is what Google sends back:
{
"results" : [
{
"address_ponents" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4224764,
"lng" : -122.0842499
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238253802915,
"lng" : -122.0829009197085
},
"southwest" : {
"lat" : 37.4211274197085,
"lng" : -122.0855988802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
I get this response with this code:
$("#submit").click(function(event) {
var address = encodeURIComponent($("#location").val());
$.ajax({
type: "GET",
url: "https://maps.googleapis./maps/api/geocode/json?address=" + address + "&sensor=false&key=" + API_KEY,
dataType: "json",
success: processJSON
});
function processJSON(json) {
// Do stuff here
}
});
The problem is I don't know what to put inside processJSON
. I was reading the tutorial here: https://www.sitepoint./ajaxjquery-getjson-simple-example/ and the documentation on .getJSON()
on http://api.jquery./jquery.getjson/. However, I'm having trouble understanding both sites.
If I wanted to get the postal_code
, how would I do read the data? I tried this:
function processJSON(json) {
// Do stuff here
alert("Postal Code:" + json.address_ponents[6].long_name);
}
I tried it with getJSON()
as well after success
:
$.getJSON(url, function(json) {
alert("Postal Code:" + json.address_ponents[6].long_name);
});
However, neither alert anything. What am I doing wrong? Thanks in advance!
Share Improve this question asked Jul 30, 2016 at 1:10 syysyy 8073 gold badges16 silver badges35 bronze badges2 Answers
Reset to default 4add success function after youu ajax
$("#submit").click(function(event) {
var address = encodeURIComponent($("#location").val());
$.ajax({
type: "GET",
url: "https://maps.googleapis./maps/api/geocode/json?address=" + address + "&sensor=false&key=" + API_KEY,
dataType: "json",
success: processJSON
}).success(function(data){
processJSON(data);
}
function processJSON(json) {
// Do stuff here
console.log(json);
alert("Postal Code:" + json.results[0].address_ponents[6].long_name);
}
});
You would need to parse the data on your success function.
function processJSON(data) {
var json = JSON.parse(data);
//..
}
Also add an error function to your $.ajax as well
$.ajax({
type..
url...
success..
error: function() {
console.log('error on request')
}
...
本文标签: javascriptReading JSON data from Google Geocoding API with jQueryStack Overflow
版权声明:本文标题:javascript - Reading JSON data from Google Geocoding API with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741978633a2408268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论