js如何获取GPS的坐标

时间:2026-02-19 09:47:48

1、function getLocation(){      

    if(navigator.geolocation){       

     //判断是否有这个对象           

    navigator.geolocation.getCurrentPosition(function(pos){          

         console.log("经:"+pos.coords.longitude+  "纬:"+

         pos.coords.latitude)           

 })       

    }else{    

        console.log("当前不支持 ")       

     }   

 }

2、function getLocation(){       

   var options={          

            enableHighAccuracy:true,          

            maximumAge:1000      

   }       

   if(navigator.geolocation){          

      //支持geolocation           

     // alert("支持geolocation");         

   navigator.geolocation.getCurrentPosition(onSuccess,onError,options);          

}else{         

   //不支持geolocation            

  alert("不支持geolocation");      

  }  

}

3、function onSuccess(pos){       

    console.log("经:"+pos.coords.longitude+    

            "纬:"+pos.coords.latitude) 

 }

© 2026 五度知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com