Google 地图 Safari Javascript 错误
当我在 Safari 中加载页面时收到此错误:
类型错误:表达式的结果 'map.getCenter' [未定义] 不是 功能。
相同的页面在其他所有主要浏览器(Chrome、FF 和 IE)中加载效果都非常好。
我正在使用 Google Maps Javascript API V3 及其 Geolocation API。
以前有人遇到过这个问题吗?
这是代码路径:
$(document).ready(function () {
initialize();
});
function initialize() {
var myOptions = {
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), myOptions);
google.maps.event.addListener(map, 'idle', function() {
saveSession();
});
getInitialLocation();
}
function getInitialLocation()
{
// Try W3C Geolocation (Preferred)
if (navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function (position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(initialLocation);
}, function () {
handleNoGeolocation(browserSupportFlag);
});
// Try Google Gears Geolocation
} else if (google.gears) {
browserSupportFlag = true;
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(function (position) {
initialLocation = new google.maps.LatLng(position.latitude, position.longitude);
map.setCenter(initialLocation);
}, function () {
handleNoGeoLocation(browserSupportFlag);
});
// Browser doesn't support Geolocation
} else {
browserSupportFlag = false;
handleNoGeolocation(browserSupportFlag);
}
function handleNoGeolocation(errorFlag) {
if (!errorFlag)
{
initialLocation = new google.maps.LatLng(geoip_latitude(), geoip_longitude());
map.setCenter(initialLocation);
}
}
}
您会看到 idle
绑定到 saveSession()
function saveSession() {
if ((map != undefined) && (map.getCenter() != undefined)
{
// do stuff
}
}
但这应该仅在该内容未定义时运行代码。另外,再一次,这适用于除 Safari 之外的所有平台。
编辑:在 JFiddle 中使用 safari 运行它。大约 5 秒后它将启动并运行。但然后再次运行它(更新或再次点击运行)。这是行不通的。
I'm getting this error when I load my page in Safari:
TypeError: Result of expression
'map.getCenter' [undefined] is not a
function.
The same page loads absolutely fine in every other major browser (Chrome, FF, and IE).
I'm using Google Maps Javascript API V3 along with their Geolocation API.
Has anyone had this issue before?
Here's the code path:
$(document).ready(function () {
initialize();
});
function initialize() {
var myOptions = {
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), myOptions);
google.maps.event.addListener(map, 'idle', function() {
saveSession();
});
getInitialLocation();
}
function getInitialLocation()
{
// Try W3C Geolocation (Preferred)
if (navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function (position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(initialLocation);
}, function () {
handleNoGeolocation(browserSupportFlag);
});
// Try Google Gears Geolocation
} else if (google.gears) {
browserSupportFlag = true;
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(function (position) {
initialLocation = new google.maps.LatLng(position.latitude, position.longitude);
map.setCenter(initialLocation);
}, function () {
handleNoGeoLocation(browserSupportFlag);
});
// Browser doesn't support Geolocation
} else {
browserSupportFlag = false;
handleNoGeolocation(browserSupportFlag);
}
function handleNoGeolocation(errorFlag) {
if (!errorFlag)
{
initialLocation = new google.maps.LatLng(geoip_latitude(), geoip_longitude());
map.setCenter(initialLocation);
}
}
}
You see the idle
is bound to saveSession()
function saveSession() {
if ((map != undefined) && (map.getCenter() != undefined)
{
// do stuff
}
}
But this should only run the code if that stuff ISN'T undefined. Also, once again, this WORKS in everything but Safari.
EDIT: Run this in JFiddle WITH safari. It will come up and work after about 5 seconds. But then run it again (either update or hit run again). It will not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过切换这两行代码
,
以防 safari 在设置地图中心之前触发
idle
事件。或者如果您可以在设置地图中心后绑定到
idle
事件,那就更好了。因此,也许可以将其设置在成功回调中的 getInitialLocation 内,以及在handleNoGeolocation 中失败的回调中设置它,这似乎是 Safari 的一个广泛存在的问题。
我假设这是一个仅限桌面的问题,因为从帖子中我看到带有 GPS 的设备可以使用(iphone 等)
Have you tried switching these two lines of code
to
in case safari fires the
idle
event before having set the center of the map.or even better if you could bind to the
idle
event after certainly having set the map center. So maybe set it inside thegetInitialLocation
in the success callbacks and for the failed in thehandleNoGeolocation
It seems that it is a wide-spread issue with Safari.
I am assuming it is a desktop-only problem, as from posts i see that devices with GPS will work with (iphone etc)
Javascript 告诉您它不知道
map.getCenter
是什么。这可能意味着map
未初始化、已删除或在生成错误的任何函数中不可用。尝试设置断点并向后查看堆栈跟踪以查看哪里出了问题。您可以通过以下方式重现该错误:
Javascript is telling you that it doesn't know what
map.getCenter
is. It probably means thatmap
wasn't initialized, was deleted, or is not available in whatever function is generating the error. Try setting a breakpoint and look backwards through the stack trace to see where things have gone wrong.You can reproduce the error with something like this: