Google 地图 Safari Javascript 错误

发布于 2024-11-07 05:59:08 字数 2596 浏览 0 评论 0原文

当我在 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 秒后它将启动并运行。但然后再次运行它(更新或再次点击运行)。这是行不通的。

http://jsfiddle.net/gYJrF/1/

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.

http://jsfiddle.net/gYJrF/1/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老街孤人 2024-11-14 05:59:08

您是否尝试过切换这两行代码

    google.maps.event.addListener(map, 'idle', function() {
     saveSession();
    });
    getInitialLocation();

    getInitialLocation();
    google.maps.event.addListener(map, 'idle', function() {
     saveSession();
    });

以防 safari 在设置地图中心之前触发 idle 事件。


或者如果您可以在设置地图中心后绑定到 idle 事件,那就更好了。因此,也许可以将其设置在成功回调中的 getInitialLocation 内,以及在handleNoGeolocation 中失败的回调中设置它,


这似乎是 Safari 的一个广泛存在的问题。

我假设这是一个仅限桌面的问题,因为从帖子中我看到带有 GPS 的设备可以使用(iphone 等

Have you tried switching these two lines of code

    google.maps.event.addListener(map, 'idle', function() {
     saveSession();
    });
    getInitialLocation();

to

    getInitialLocation();
    google.maps.event.addListener(map, 'idle', function() {
     saveSession();
    });

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 the getInitialLocation in the success callbacks and for the failed in the handleNoGeolocation


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)

若沐 2024-11-14 05:59:08

Javascript 告诉您它不知道 map.getCenter 是什么。这可能意味着 map 未初始化、已删除或在生成错误的任何函数中不可用。尝试设置断点并向后查看堆栈跟踪以查看哪里出了问题。

您可以通过以下方式重现该错误:

<script>
function init() {
    var map = new google.maps.Map( /* params */ );
}

function get_map_center() {
    return map.getCenter();  // map is not defined here, it was only defined
                             // in the init() function above. 
}
</script>

...

<body onload="init">
...
</body>

Javascript is telling you that it doesn't know what map.getCenter is. It probably means that map 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:

<script>
function init() {
    var map = new google.maps.Map( /* params */ );
}

function get_map_center() {
    return map.getCenter();  // map is not defined here, it was only defined
                             // in the init() function above. 
}
</script>

...

<body onload="init">
...
</body>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文