Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PREFILL ADDRESS OR STREET USING GOOGLE API
function getStreetNumber(lat, lng) {
var json = getRGeocodeJSON(lat, lng);
var obj = JSON.parse(json);
try { return obj.results[0].address_components[0].short_name; }
catch(e) { return ''; }
}
function getStreetName(lat, lng) {
var json = getRGeocodeJSON(lat, lng);
var obj = JSON.parse(json);
try { return obj.results[0].address_components[1].short_name; }
catch(e) { return ''; }
}
function getCity(lat, lng) {
var json = getRGeocodeJSON(lat, lng);
var obj = JSON.parse(json);
try { return obj.results[0].address_components[2].short_name; }
catch(e) { return ''; }
}
function getState(lat, lng) {
var json = getRGeocodeJSON(lat, lng);
var obj = JSON.parse(json);
try { return obj.results[0].address_components[5].short_name; }
catch(e) { return ''; }
}
function getZip(lat, lng) {
var json = getRGeocodeJSON(lat, lng);
var obj = JSON.parse(json);
try { return obj.results[0].address_components[7].short_name; }
catch(e) { return ''; }
}
function getRGeocodeJSON(lat, lng) {
var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lng + "&[API KEY]";
var resp ;
var xmlHttp ;
resp = '' ;
xmlHttp = new XMLHttpRequest();
if(xmlHttp != null)
{
xmlHttp.open( "GET", url, false );
xmlHttp.send( null );
resp = xmlHttp.responseText;
}
return resp ;
}
function getEverything(lat, lng) {
var json = getRGeocodeJSON(lat, lng);
var obj = JSON.parse(json);
var address_components =JSON.stringify( obj.results[0].address_components);
return address_components;
}
function getFormattedAddress(lat, lng) {
var json = getRGeocodeJSON(lat, lng);
var obj = JSON.parse(json);
try { return obj.results[0].formatted_address; }
catch(e) { return ''; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment