本文介绍了HTML5地理位置表是重新加载全图不仅位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个代码,但问题是表位()是重新映射完整的地图,不仅是位置,我想要的是当我缩放,即使位置发生变化,它也会将标记更改为该位置。
< p id =demo>点击按钮以获取您的位置:< / p>
< button onclick =getLocation()>试试< / button>
< div id =mapholder>< / div>
< script src =http://maps.google.com/maps/api/js?sensor=false>< / script>
< script>
var x = document.getElementById(demo);
函数getLocation(){
if(navigator.geolocation){
navigator.geolocation.watchPosition(showPosition,showError);
} else {
x.innerHTML =此浏览器不支持地理定位。;}
}
函数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){$ b $ case case.PERMISSION_DENIED:
x.innerHTML =User denied地理位置请求。
休息;
case error.POSITION_UNAVAILABLE:
x.innerHTML =位置信息不可用。
休息;
case error.TIMEOUT:
x.innerHTML =请求获取用户位置超时。
休息;
case error.UNKNOWN_ERROR:
x.innerHTML =发生未知错误。
休息;
}
}
< / script>
< / body>
< / html>
解决方案
这是因为您正在重新载入地图。你应该改变中心。
所以你应该修改你的代码有2个函数:
1- inizialize map。
函数初始化(位置){
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-改变焦点
函数showPosition(position){
map.setCenter(position);
}
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>
解决方案
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);
}
这篇关于HTML5地理位置表是重新加载全图不仅位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!