我不知道我的代码出了什么问题,我把它建在括号里,它起了作用,但当我搬到visual studio时,它告诉我,他无法用谷歌开发工具更精确地读取纬度属性,我得到了这个"Unaught TypeError:无法读取未定义的属性‘纬度’"
<script>
$(document).ready(function(){
getLocation();
});
function getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(initialize);
}else{
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function initialize(position){
var mapCanvas = document.getElementById('map-canvas');
var myLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var mapOptions = {
center: myLatLng ,
zoom: 18,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions);
map.set('styles', [
{
featureType: 'road',
elementType: 'geometry',
stylers:[
{color: '#C4C4C4'},
{weight: 0.6}
]
},
{
featureType: 'road',
elementType: 'labels',
stylers:[,
{saturation: -100},
{invert_lightness: false}
]
},
{
featureType: 'landscape',
elementType: 'geometry',
stylers: [
{ visibility: 'on' },
{ hue: '#ffffff' },
{ gamma: 1.4 },
{ saturation: 82 },
{ lightness: 80 }
]
},
{
featureType: 'poi',
elementType: 'geometry',
stylers: [
{ visibility: 'off' }
]
},
{
featureType: 'poi.school',
elementType: 'geometry',
stylers: [
{ visibility: 'off' },
{ hue: '#fff700' },
{ lightness: -15 },
{ saturation: 99 }
]
}
]);
var iconBase = 'Images/';
var marker = new google.maps.Marker({
position: myLatLng,
icon: iconBase + 'romb.png',
map:map
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
更新!
在再次查看我的代码后,我发现括号中有一个问题,我没有在html文档顶部使用DOCTYPE标记,但在vs2013中我使用了它,在我删除vs2013中的标记后,我也删除了谷歌电话线。但现在我有另一个问题:为什么DOCTYPE标签不起作用?
谢谢你的帮助,Aurelian!
您的问题是这一行:
google.maps.event.addDomListener(window, 'load', initialize);
它在没有参数的情况下调用initialize函数,并且没有纬度属性。
function initialize(position){
我认为您没有将位置变量传递回初始化函数。因此,当它试图获得纬度和经度属性时,它无法获得,因为位置是未定义的。
尝试更改此
navigator.geolocation.getCurrentPosition(initialize);
到这个
navigator.geolocation.getCurrentPosition(function(position) {
initialize(position);
});