微信内置浏览器如何定位用户所在城市


我的代码这样写的,但是有个问题,在wifi网络下可以正确定位(杭州),但是把网络切换到移动网络比喻4G网络就不准确了,我在杭州,但是定位显示温州。


 <script type="text/javascript" src="http://api.map.baidu.com/getscript?v=1.4&ak=&services=&t=20150522093217"></script>
     <script>
    $(function(){
        var x=document.getElementById("demo");
        var location = new Array(); 
        function getLocation(){ 
          if (navigator.geolocation){ 
            navigator.geolocation.getCurrentPosition(showPosition); 
          }else{
            x.innerHTML="Geolocation is not supported by this browser.";} 
          } 

        function showPosition(position){ 
         x.innerHTML = position.coords.latitude + "|" + position.coords.longitude;
         alert(position.coords.longitude);
         alert(position.coords.latitude);

         var map = new BMap.Map("allmap");
            var point = new BMap.Point(position.coords.longitude,position.coords.latitude);
            map.centerAndZoom(point,12);

            function myFun(result){
                var cityName = result.name;
                map.setCenter(cityName);
                alert(cityName);
            }
            var myCity = new BMap.LocalCity();
            myCity.get(myFun);
            //return  position.coords.latitude + '|' +  position.coords.longitude;
        }
        getLocation();  
    })
    </script>

html5 JavaScript

我愛曬星星 9 years, 5 months ago

Your Answer