Mar*_*k H 6 html javascript google-maps geolocation google-maps-api-3
大家好,我有以下代码:
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']
};
...
Run Code Online (Sandbox Code Playgroud)
基本上,如果我设置 Long/Lat 坐标,我可以让地图工作得很好,但我希望通过获取用户位置来传递这些坐标。使用上面的代码,地图显示在我的手机上,但出现以下错误:
TypeError: 'undefined' is not an object (evaluating 'position.coords.latitude')
Run Code Online (Sandbox Code Playgroud)
但在桌面上我没有得到地图和以下错误:
Uncaught TypeError: Cannot read property 'latitude' of undefined
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒我是 Javascript 和这些 API 的新手
稍后在您的代码中,在您的页面http://markhaynes.me/mwp/addloc.html 上,您正在调用google.maps.event.addDomListener(window, 'load', initialize);
这会导致 Google 调用 initialize() 函数,但它传递一个事件对象作为position
参数而不是您期望的位置对象。