HTML5 geolocation watchposition is reloading full map not only position

天涯浪子 提交于 2020-01-06 13:49:10

问题


I have a code but the problem is the watchposition() is relaoding the full map,not only the position,what i want is when i zoom even if position changes it will just change the marker to that position

<p id="demo">Click the button to get your position:</p>

<button onclick="getLocation()">Try It</button>

<div id="mapholder"></div>

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
    navigator.geolocation.watchPosition(showPosition,showError);
} else { 
    x.innerHTML = "Geolocation is not supported by this browser.";}
}

 function showPosition(position) {
 lat = position.coords.latitude;
 lon = position.coords.longitude;
  latlon = new google.maps.LatLng(lat, lon)
 mapholder = document.getElementById('mapholder')
 mapholder.style.height='250px';
 mapholder.style.width='500px';

 var myOptions={
 center:latlon,zoom:14,
 mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
}

var map = new google.maps.Map(document.getElementById("mapholder"),myOptions);
var marker = new google.maps.Marker({position:latlon,map:map,title:"You are here!"});
 }

 function showError(error) {
switch(error.code) {
    case error.PERMISSION_DENIED:
        x.innerHTML = "User denied the request for Geolocation."
        break;
    case error.POSITION_UNAVAILABLE:
        x.innerHTML = "Location information is unavailable."
        break;
    case error.TIMEOUT:
        x.innerHTML = "The request to get user location timed out."
        break;
    case error.UNKNOWN_ERROR:
        x.innerHTML = "An unknown error occurred."
        break;
      }
     }
     </script>
    </body>
      </html>

回答1:


That's because you are reloading the map. You should change the center.

So you should modify your code to have 2 functions:

1- To inizialize the map.

     function initialize(position) {
       lat = position.coords.latitude;
       lon = position.coords.longitude;
       latlon = new google.maps.LatLng(lat, lon)
       mapholder = document.getElementById('mapholder')
       mapholder.style.height='250px';
       mapholder.style.width='500px';

       var myOptions={
        center:latlon,zoom:14,
        mapTypeId:google.maps.MapTypeId.ROADMAP,
        mapTypeControl:false,
        navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
     }

     var map = new google.maps.Map(document.getElementById("mapholder"),myOptions);
     var marker = new google.maps.Marker({position:latlon,map:map,title:"You are here!"});
}

2- To change the focus.

 function showPosition(position) {

  map.setCenter(position);

 }


来源:https://stackoverflow.com/questions/26419317/html5-geolocation-watchposition-is-reloading-full-map-not-only-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!