简体   繁体   中英

HTML5 Geolocation Trouble

I am writing a Java script for HTML 5 geolocation but it doesnt seem to work and when I try to output the contents in another scope, it says undefined. Below is the code.

<script>
var lat;
var lng;
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{}
  }
function showPosition(position)
  {
     lat = position.coords.latitude;
     lng = position.coords.longitude;
     document.write(lat); // Doesnt output anything
  }


</script>

    <script type="text/javascript">
          document.write(lat);      // It says undefined
        $(document).ready(function()
        {
             //some scripts here

         }

</script>

The success callback functions are called only when the html page is hosted on a web server, not in localhost. Are you testing locally or on a webserver?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM