Created
November 5, 2013 01:23
-
-
Save DaveGoosem/7312320 to your computer and use it in GitHub Desktop.
geo lookup from google api
sample code which uses callback query string to load <script> for gmaps api only if element exists on page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* GeoLocation | |
* @requires jQuery v1.7.2+ | |
* | |
* Description | |
* auto-populates a field with geo-location data | |
*/ | |
; (function ($) { | |
window.Motorama.geoLocation = window.Motorama.geoLocation || window.Motorama.BaseModule(function () { | |
var _self, $el, $locationField, geocoder; | |
//Get geolocation lat & long and format it to address location | |
function codeLatLng(lat, lng) { | |
var latlng = new google.maps.LatLng(lat, lng); | |
geocoder.geocode({ 'latLng': latlng }, function (results, status) { | |
if (status == google.maps.GeocoderStatus.OK) { | |
if (results[1]) { | |
//formatted address | |
$locationField.val(results[0].formatted_address); | |
//find country name | |
for (var i = 0; i < results[0].address_components.length; i++) { | |
for (var b = 0; b < results[0].address_components[i].types.length; b++) { | |
//there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate | |
if (results[0].address_components[i].types[b] == "administrative_area_level_1") { | |
//this is the object you are looking for | |
var city = results[0].address_components[i]; | |
break; | |
} | |
} | |
} | |
} | |
else { | |
console.log("No results found"); | |
} | |
} | |
else { | |
console.log("Geocoder failed due to: " + status); | |
} | |
}); | |
} | |
//Get the latitude and the longitude; | |
function geoLocationSuccess(position) { | |
var lat = position.coords.latitude; | |
var lng = position.coords.longitude; | |
codeLatLng(lat, lng); | |
} | |
function geoLocationError() { | |
console.log('unable to load geo-location data'); | |
} | |
return { | |
init: function () { | |
_self = this; | |
$el = $('body'); | |
$locationField = $('.geoLocationInputField input'); | |
if ($locationField != undefined && $locationField.length > 0) { | |
this.methods.loadGMapScript(); | |
} | |
}, | |
methods: { | |
/*gmaps api has its own script calls inside of it so we need to wait until they have also loaded before coming back to | |
do this we can add 'callback= ' as a query string to the script src options and add our own method to the end once its returned*/ | |
loadGMapScript: function() { | |
var script = document.createElement("script"); | |
script.type = "text/javascript"; | |
script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=Motorama.geoLocation.methods.intializeMap"; | |
script.async = true; | |
document.body.appendChild(script); | |
}, | |
intializeMap: function() { | |
geocoder = new window.google.maps.Geocoder(); | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(geoLocationSuccess, geoLocationError); | |
} | |
} | |
} | |
}; | |
}()); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment