Skip to content

Instantly share code, notes, and snippets.

@dfrankland
Last active August 29, 2015 14:13
Show Gist options
  • Save dfrankland/41896bb66f1cfe3fe5b1 to your computer and use it in GitHub Desktop.
Save dfrankland/41896bb66f1cfe3fe5b1 to your computer and use it in GitHub Desktop.
Geocode Addresses Cross Domain using Google Maps API
var jquery = document.createElement('script');
jquery.src = "https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jquery);
document.addEventListener("DOMContentLoaded", function(event) {
sheet = "title^is_active^phone^country_id^region^street^city^postal_code^desc^latitude^longitude^stores^website^icon^image^position\n";
$('.magasin').each(function(){
/*Dealer Name*/
$(this).children('.nom_mag').children('font').remove();
title = $(this).children('.nom_mag').text();
$(this).children('div').remove();
/*Dealer Website*/
website = $(this).children('a').attr('href');
if(!website){website = "";}
$(this).children('a').remove();
/*Google maps url*/
addressUrl = encodeURI("https://maps.googleapis.com/maps/api/geocode/json?address="+$(this).children('font').children('a').attr('href').split("http://maps.google.com/maps?q=")[1]+"&sensor=false");
$(this).children('font').remove();
/*Dealer phone*/
getPhone = $(this).html().split("<br>");
getPhone[getPhone.length-1] ? phone = getPhone[getPhone.length-1] : phone = getPhone[getPhone.length-2];
console.log(addressUrl);
street_number = route = city = admin1 = admin2 = country_id = postal_code = street = region = latitude = longitude = "";
$.ajax({
type: "POST",
async: false,
url: addressUrl,
success: function( data ){
if(data.status !== "ZERO_RESULTS"){
for(i=0;i < data.results[0].address_components.length; i++){
switch(data.results[0].address_components[i].types[0]){
case "street_number":
street_number = data.results[0].address_components[i].long_name;
break;
case "route":
route = data.results[0].address_components[i].long_name;
break;
case "locality":
city = data.results[0].address_components[i].long_name;
break;
case "administrative_area_level_2":
admin2 = data.results[0].address_components[i].long_name;
break;
case "administrative_area_level_1":
admin1 = data.results[0].address_components[i].long_name;
break;
case "country":
country_id = data.results[0].address_components[i].short_name;
break;
case "postal_code":
postal_code = data.results[0].address_components[i].long_name;
break;
}
}
latitude = data.results[0].geometry.location.lat;
longitude = data.results[0].geometry.location.lng;
}
}
});
if(street_number !== "" && route !== ""){street_number += " ";}
street = street_number + route;
if(admin1 !== "" !== "" && admin2 !== ""){admin1 += ", ";}
region = admin1 + admin2;
sheet += title + "^1^" + phone + "^" + country_id + "^" + region + "^" + street + "^" + city + "^" + postal_code + "^^" + latitude + "^" + longitude + "^0^" + website + "^^^\n";
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment