Web Connection
get latitude and longitude from browser into WWWC?
Gravatar is a globally recognized avatar based on your email address. get latitude and longitude from browser into WWWC?
  Michael B
  All
  May 4, 2017 @ 10:32am

Has anyone tried to use the browser to get geo coordinates and then load them into WWWC using ajax by any chance? Is there a graceful way to do this - something other than calling a third party web service ?

Gravatar is a globally recognized avatar based on your email address. re: get latitude and longitude from browser into WWWC?
  Rick Strahl
  Michael B
  May 4, 2017 @ 07:04pm

What are you trying to do exactly?

It's relatively easy to get Geolocation from browsers - just a few lines of code using the geolocation api. But how accurate that is depends on how the application is accessed.

Also whenever you use the GeoApis the browser prompts for allowing access to it - at least once initially, caches after that but you will get prompted occasionally, so it's not totally transparent.

Assuming that's not a problem, the actual geolocation code on the client is trivial. Here's an example I use on my site to figure out what timezone I'm in and writing that to a file on the server to let people know where I'm at:

    $("#btnGetLocation").click(setLocationFile);
    
    function setLocationFile() {
        // get geolocation with callback
        navigator.geolocation.getCurrentPosition(function (loc) {            
            var location =
            {
                offset: new Date().getTimezoneOffset(),
                lat: loc.coords.latitude,
                long: loc.coords.longitude
            }

            // post the results to the server
            $.post("location.cshtml",
                location,
                function (location) {
                    location = JSON.parse(location);
                    $("#txtPlace").val(location.Place);
                    $("#txtTimezone").val(location.Timezone);
                },
                function() {
                    alert('error');
                });
        });
    }
© 1996-2024