Last active
January 20, 2022 02:46
-
-
Save mhkeller/4567006 to your computer and use it in GitHub Desktop.
Open Street Map Geocoder
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
// BEGIN GEOCODING | |
$adrsInput = $('#address-submit'); | |
$adrsText = $('#address-text') | |
var geocoder, | |
firstRun = true, | |
markerGroup; | |
var geoCode = function(address) { | |
$.ajax({ | |
url:'http://open.mapquestapi.com/geocoding/v1/address?&inFormat=kvp&outFormat=json&location='+address, | |
dataType:'JSONP', | |
success: function(data){ | |
if(data.results[0].locations.length == 0){ | |
alert("We couldn't find your address, please make sure it includes a city and state.") | |
}else{ | |
var lat = data.results[0].locations[0].latLng.lat, | |
lng = data.results[0].locations[0].latLng.lng; | |
console.log(lat,lng) | |
map.panTo([lat,lng]); | |
// Set the zoom to whatever you want. | |
// the higher the number the greater the zoom. | |
map.setZoom(8); | |
// Optional, place a marker | |
if (firstRun == false) { | |
map.removeLayer(markerGroup) | |
} | |
var markerLL = L.marker([lat, lng]); | |
markerGroupLL = L.layerGroup([markerLL]); | |
map.addLayer(markerGroupLL); | |
firstRun = false; | |
} | |
}, | |
error: function(){ | |
alert("We couldn't find your address, please make sure it includes a city and state.") | |
} | |
}); | |
} | |
// Geocoding buttons | |
$adrsInput.click(function() { | |
var addrs = $adrsText.val(); | |
geoCode(addrs); | |
}); | |
// also enable the button when you hit return | |
$(document) | |
.keydown(function(e) { | |
if (e.keyCode == 13) { // return key | |
$adrsInput.trigger('click'); | |
} | |
}); // End geocoding |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment