Skip to content

Instantly share code, notes, and snippets.

@mkoryak
Last active December 20, 2015 14:29
Show Gist options
  • Save mkoryak/6146976 to your computer and use it in GitHub Desktop.
Save mkoryak/6146976 to your computer and use it in GitHub Desktop.
reverse geolocation via one of my forked geolocation libs
var locateMe = function(){
try {
if(geoPosition.init()){
var geocoder = new google.maps.Geocoder();
var foundLocation = function(city, state, country, lat, lon){
if(country) $("#country").val(country).trigger("change");
if(state) $("#state").val(state).trigger("change");
if(city) $("#city").val(city).trigger("change");
$("#userLat").val(lat);
$("#userLon").val(lon);
};
geoPosition.getCurrentPosition(function(r){
var latlng = new google.maps.LatLng(r.coords.latitude, r.coords.longitude);
var findResult = function(results, name){
var result = _.find(results, function(obj){
return obj.types[0] == name && obj.types[1] == "political";
});
return result ? result.short_name : null;
};
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK && results.length) {
results = results[0].address_components;
var city = findResult(results, "locality");
var state = findResult(results, "administrative_area_level_1");
var country = findResult(results, "country");
foundLocation(city, state, country, r.coords.latitude, r.coords.longitude);
}
});
}, { enableHighAccuracy:true, maximumAge: 1000 * 60 * 10 });
}
} catch(e){
console.log("something bad happen:",e);
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment