【发布时间】:2012-12-22 14:39:02
【问题描述】:
大家好,我有以下代码:
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 的新手
【问题讨论】:
-
这个答案有帮助吗 -> stackoverflow.com/a/3885172/572939 - 谈论添加超时以确保调用失败回调(顺便说一句,您还没有实现)
-
嗨,我仍然收到错误消息,但地图显示在我的手机上,但不在桌面上。您可以在以下位置查看它的实际效果:http://markhaynes.me/mwp/addloc.html
标签: javascript html google-maps google-maps-api-3 geolocation