Skip to content

Instantly share code, notes, and snippets.

@this-is-r-gaurav
Last active August 18, 2018 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save this-is-r-gaurav/c8be3c67874faad12e9133cfc4d24982 to your computer and use it in GitHub Desktop.
Save this-is-r-gaurav/c8be3c67874faad12e9133cfc4d24982 to your computer and use it in GitHub Desktop.
Struggling With Google Map Json Data, This Gist helps You analyze the data sent as Google JSON response and Get The Street City State automatically.
let lat = null; // place Your Latitude Value here
let lng = null; // place Your Longitude Value here
const YOUR_KEY = null; // place Your API Key here
$.get('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + lat + ", " + lng + '&key=' + YOUR_KEY, function (data) {
if (data['status'] === "OK") {
let result = data['results'][0].address_components;
let street, city, state, postal_code, area = "";
for (let i = 0; i < result.length; i++) {
let type_of_param = result[i].types[0];
let long_name = result[i].long_name;
if (type_of_param === "postal_code") {
postal_code = long_name;
} else if (type_of_param === "administrative_area_level_1") {
state = long_name;
} else if (type_of_param === "administrative_area_level_2") {
city = long_name;
} else if (type_of_param === "route") {
street = long_name;
} else if (type_of_param === "neighborhood") {
if (area === "") {
area = long_name;
} else {
area += ", " + long_name;
}
} else if (result[i].types[1] === "sublocality") {
if (area === "") {
area = long_name;
} else {
area += ", " + long_name
}
}
}
setValue(street, area, city, postal_code, state);//Utility Function Call
}
})
//Utility Fuction definition
function setValue(street, area, city, postal_code, state) {
//all the xx_input are form input
let city_input = document.getElementById("city");
let area_input = document.getElementById("area");
let pin_input = document.getElementById("pin");
let state_input = document.getElementById("state");
let street_input = document.getElementById("street");
if (typeof city !== "undefined") {
city_input.value = city;
console.log('')
}
if (typeof area !== "undefined") {
area_input.value = area;
}
if (typeof postal_code !== "undefined") {
pin_input.value = postal_code;
}
if (typeof state !== "undefined") {
state_input.value = state;
}
if (typeof street !== "undefined") {
street_input.value = street;
}
console.log(street, area, city, postal_code, state);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment