调用GBrowserIsCompatible()时出现Google Maps API JavaScript错误

3

在以下行出现了错误:

 if (GBrowserIsCompatible()) {

这是我的代码,但仍然无法工作。

<script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=<?php echo $google_api['VALUE'];?>" type="text/javascript"></script>

<script type="text/javascript">
var map = null;
var geocoder = null;

function initialize(address) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        geocoder = new GClientGeocoder();

        /* Start */

        if (geocoder) {
            geocoder.getLatLng(
                address,
                function (point) {
                    if (!point) {
                        alert(address + " not found");
                    } else {
                        map.setCenter(point, 13);
                        var marker = new GMarker(point);
                        map.addOverlay(marker);
                        marker.openInfoWindowHtml(address);
                    }
                }
            );
        }

        /* End */
    }
}
</script>

你能具体说明一下你在哪些浏览器中遇到了什么错误吗? - alexia
1
你是在什么时候调用initialize函数的?如果你是在页面加载之前调用的话,那很可能就是问题所在。 - Bob
4个回答

2
你是否使用了你自己的API密钥加载了Google Maps API?
<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ENTER_API_KEY_HERE'></script>

来源:http://code.google.com/p/jmaps/issues/detail?id=12


更新

查看IE故障排除页面

也许您检查得太早了。尝试像这样:

var iterations = 0;
function check_compat() {
    if (iterations === 75) {
        alert('Failed to load Google Maps API. Clear your browser cache, open Google Maps then try again.');
        return;
    }
    if (typeof GBrowserIsCompatible === 'undefined') {
        // It isn't loaded, schedule the next check.
        setTimeout(check_compat, 200);
        iterations++;
    } else {
        if (GBrowserIsCompatible()) {
            mapReadyFn();
        } else {
            alert('Sorry, your browser is not supported.');
        }
    }
}

接下来,只需要替换这一行代码即可:

if (GBrowserIsCompatible()) {

使用以下方法:

function mapReadyFn() {

如果尝试失败超过15秒,系统会停止尝试并显示错误信息。

我不确定那段代码是否能正常工作。第一次运行它会返回 true 或 false,而 initialize 永远不会再被调用。你需要想办法再次调用 initialize。 - Bob
这在运行ICS的Galaxy S3上不起作用,也在运行JellyBean的Galaxy Camera上不起作用。它会陷入一个循环中,不停地调用GBrowserUncompatible。 - Andrew S
1
值得注意的是,GBrowserIsCompatible无法与v3 API一起使用(它不可用),而且在v3 API中也没有提供替代方法。 - Andrew S

2

如果网页使用https协议而不是http协议,我在Internet Explorer上也遇到了同样的问题。你是用https还是http访问它?无论如何,如果使用https,会出现警告,除非你向Google支付大约10,000美元。


只需包含脚本而不指定协议: <script src="//maps.google.com/maps?file=api&v=2&key=%yourkey%" type="text/javascript"></script> HTTPS对每个人都可用。您无需为此付费 ;) - Philipp Michael

0

@Andrew S的回答现在似乎是最好的,因为v3是API现在唯一可用的版本。没有GBrowserIsCompatible方法,也没有任何替代方法。我猜在调用地图API之前最好检查一些你打算使用的Web API(例如地理位置)。

Google Maps v2 to v3: Removing Obsolete Code


0

我也遇到了同样的问题。当我检查http://universimmedia.pagesperso-orange.fr/geo/loc.htm上的示例程序时,它可以正常工作;但是当我将API密钥值更改为我的密钥后,在Gbrowser行出现了错误。在检查了我的Google账户API后,我意识到有两个Google地图API版本2和3,并且我只启用了版本3并在JavaScript中引用了地图。当我启用API版本2时,错误得到了修复。请尝试并给出您的反馈。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接