js获取你的坐标

前提要引入百度API:

<script src="http://api.map.baidu.com/api?v=1.4" type="text/javascript">script>

html

<body>
   <p id="demo">点击这个按钮,获得您的坐标:p>
   <button onclick="getLocation()">试一下button>
body>

js

<script>

      var x=document.getElementById("demo");
      if (navigator.geolocation){
          navigator.geolocation.getCurrentPosition(showPosition);
       }
      else{
      x.innerHTML="Geolocation is not supported by this browser.";}
      }
     function showPosition(position){
        x.innerHTML="Latitude: " + position.coords.latitude + 
        "
Longitude: "
+ position.coords.longitude; }
script>

谷歌打开没反应的话(被谷歌浏览器拦截),用ie或手机测试都可以打开。

你可能感兴趣的:(js)