<p id="mapBtn" >
Click the button to get your position.
</p>
<div id="mapholder"></div>
&#13;
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
var x = document.getElementById("mapBtn");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
lat = position.coords.latitude;
lon = position.coords.longitude;
accur = position.coords.accuracy;
latlon = new google.maps.LatLng(lat, lon, accur)
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>
&#13;
我在我的网站上添加了一张地图,它正在开发iphone,windows和一些Android设备,比如三星s4,但它不能用于谷歌手机,华硕手机和其他一些Android设备。< / p>
答案 0 :(得分:1)
我在这些设备上测试了您的代码:
2.3.5: Alcatel Venture
4.0.4: Kyocera Event
4.4.2: LG Tribute
4.4.2: Galaxy S3
4.4.4: Nexus 5
5.1 : Nexus 7
并且您的代码适用于所有设备。
一种可能性是,当用户首次运行您的应用时,他们点击了deny
来访问位置。在这种情况下,您可以更改以下设置:
Menu > Settings > Site Settings > All sites > your.website.com >Location access > Allow
答案 1 :(得分:1)
似乎Android设备上的Location
模式必须设置为High Accuracy
。神奇的是,地图出现了!