大家好,我有以下代码:
var map;
var infowindow;
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(initialize);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function initialize(position) {
var pyrmont = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: 500,
types: ['restaurant']
};
...
基本上,如果我设置 Long/Lat 坐标,我可以让地图正常工作,但我希望通过获取用户位置来传递这些坐标。上面的代码显示在我的手机上,但出现以下错误:
TypeError: 'undefined' is not an object (evaluating 'position.coords.latitude')
但在桌面上我没有得到地图和以下错误:
Uncaught TypeError: Cannot read property 'latitude' of undefined
任何帮助都会很棒我是 Javascript 和这些 API 的新手