In case someone has confusion (like mine), the method used here is the own "watchPosition" method of the geolocation object.
Specifications WatchDosition MDN
The watchPosition method will be called when the user moves the location, and you can specify your lat / long for the zip generator as a callback.
From the specifications:
var watchID = navigator.geolocation.watchPosition(function(position) { do_something(position.coords.latitude, position.coords.longitude); });
so that it looks like this:
var watchID = navigator.geolocation.watchPosition(function(position) { $.getJSON( "http://ws.geonames.org/findNearestAddressJSON?callback=?", { lat : position.coords.latitude, lng : position.coords.longitude }, function (data) { $(function () { $('#zip').text(data.address.postalcode); }); } ); });
which will achieve even greater simplicity - the user will not need to press the location button, it must be updated as they move.
To be clear, the Greg function uses this watchPosition method, but if you want to understand what works, or use very lean native code and configure it yourself, watchPosition is your tool.
source share