locked
Geolocation not working on server RRS feed

  • Question

  • User746671234 posted

    I am using following code to show the location of user.  It works find locally.  But when I host it on the server it doesn't works.  What could be an issue?

    <script>
             function displayLocation(latitude, longitude) {
                 var request = new XMLHttpRequest();
                 
                 var method = 'GET';
                 var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&sensor=true';
                 var async = true;
    
                 request.open(method, url, async);
                 request.onreadystatechange = function () {
                     if (request.readyState == 4 && request.status == 200) {
                         var data = JSON.parse(request.responseText);
                         var address = data.results[0];
                         document.write(address.formatted_address);
                     }
                 };
                 request.send();
             };
    
             var successCallback = function (position) {
                 var x = position.coords.latitude;
                 var y = position.coords.longitude;
                 
                 displayLocation(x, y);
             };
    
             var errorCallback = function (error) {
                 alert(error.errorMessage);
                 var errorMessage = 'Unknown error';
                 switch (error.code) {
                     case 1:
                         errorMessage = 'Permission denied';
                         break;
                     case 2:
                         errorMessage = 'Position unavailable';
                         break;
                     case 3:
                         errorMessage = 'Timeout';
                         break;
                 }
                 document.write(errorMessage);
             };
    
             var options = {
                 enableHighAccuracy: true,
                 timeout: 5000,
                 maximumAge: 0
             };
    
             navigator.geolocation.getCurrentPosition(successCallback, errorCallback, options);
        </script>
    

    Saturday, June 30, 2018 9:44 AM

Answers

  • User475983607 posted

    Script runs in the browser not the server.   Keep in mind that dumping code on the forum and stating "it doesn't work" is too vague to provide assistance. 

    What does not work?  Have you tried basic troubleshooting?  Are you seeing any script errors in the script console in dev tools?  As I understand Google APIs, you need a key and I don't see a key in your source code.

    Perhaps you should read through the Google API docs and make sure your code is correct.  Keep in mind this is a 3rd party API and asking for help in Google is a better path for support.

    https://forums.asp.net/t/2143113.aspx?Geolocation+not+working+on+server

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 30, 2018 11:35 AM

All replies

  • User475983607 posted

    Script runs in the browser not the server.   Keep in mind that dumping code on the forum and stating "it doesn't work" is too vague to provide assistance. 

    What does not work?  Have you tried basic troubleshooting?  Are you seeing any script errors in the script console in dev tools?  As I understand Google APIs, you need a key and I don't see a key in your source code.

    Perhaps you should read through the Google API docs and make sure your code is correct.  Keep in mind this is a 3rd party API and asking for help in Google is a better path for support.

    https://forums.asp.net/t/2143113.aspx?Geolocation+not+working+on+server

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 30, 2018 11:35 AM
  • User746671234 posted

    Thank you

    Saturday, June 30, 2018 12:02 PM