【问题标题】:why is firefox throwing the error google.maps.geocoder is not a constructor?为什么firefox会抛出错误google.maps.geocoder is not a constructor?
【发布时间】:2015-06-16 19:59:22
【问题描述】:

我最近开始在 Firefox 中测试我的 JavaScript,最初我使用的是 chrome。

这部分代码在 chrome 中运行良好:

geocoder = new google.maps.Geocoder();

但是在 Firefox 中会抛出上述错误,这是为什么呢? Firefox 是否需要额外注意事项?

下面是我的 JS 和 aspx 代码

var map;
var placesService;
var geocoder;


function loadMap() {

    $("#map-canvas").ready(function () {
        alert("ready");

        geocoder = new google.maps.Geocoder();
        alert("1");

        map = new google.maps.Map(document.getElementById('map-canvas'),
            {
                center: new google.maps.LatLng(-34.397, 150.644),
                zoom: 12
            });
        alert("2");

        placesService = new google.maps.places.PlacesService(map);
        alert("3");

        $('#control').hide();
        $('#errorLog').hide(); // the error log text box would cause the map to only display half the tiles, delaying the showing of these elements stopped that.
        alert("4");

        google.maps.event.addListenerOnce(map, 'tilesloaded', function () {
            alert("5");
            map.controls[google.maps.ControlPosition.TOP_LEFT].push(document.getElementById("control"));
            map.controls[google.maps.ControlPosition.RIGHT].push(document.getElementById("errorLog"));
            $('#control').show();
            $('#errorLog').show();
            alert("6");
        });

        alert("7");




        $('#locateTown').click(function () { locateTown(); });
        $('#scanTown').click(function () { scanTownStart(); });
        $('#newSearch').click(function () { backToSearch() });
        $('.scanTown').hide();
        $('.info').hide();
        alert("8");

    })
}
<!DOCTYPE html>

<html lang="en">
    <head runat="server">
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
        <title>Title</title>

        <asp:PlaceHolder runat="server">
            <%: Scripts.Render("~/bundles/modernizr") %>
        </asp:PlaceHolder>

        <webopt:bundlereference runat="server" path="~/Content/css" />
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />

    </head>
    <body>
        
        <form runat="server">
            <asp:ScriptManager runat="server" EnablePartialRendering="true">
                <Scripts>
                    <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%>
                    <%--Framework Scripts--%>
                    <asp:ScriptReference Name="MsAjaxBundle" />
                    <asp:ScriptReference Name="jquery" />
                    <asp:ScriptReference Name="respond" />
                    <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                    <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                    <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                    <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                    <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                    <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                    <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                    <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                    <asp:ScriptReference Name="WebFormsBundle" />
                    <%--Site Scripts--%>
                    <asp:ScriptReference Path="~/bundles/Page" />
                    <asp:ScriptReference Path="~/bundles/Utils" />
                    <asp:ScriptReference Path="https://maps.googleapis.com/maps/api/js?key=AIzaSyDjcLgEOEkdQfwxEyGOu4hHlSY_s-LOGkQ" />
                    <asp:ScriptReference Path="https://maps.googleapis.com/maps/api/js?libraries=places" />
                </Scripts>
            </asp:ScriptManager>

            <div class="fill flex-container flex-column">
                <asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="always">
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="uploadResults" EventName="Click" />
                    </Triggers>
                    <ContentTemplate>
                        <div id="control" style="margin-top:0.3%">
                            <input type="text" id="towntoSearch" class="locateTown" value="Hatfield" />
                            <input type="button" id="locateTown" class="locateTown" value="Locate Town" />
                            <select id="selectedTown" class="scanTown" ></select> 
                            <input type="button" id="scanTown" class="scanTown" value="Scan Town" />
                            <input type="button" id="newSearch" class="scanTown" value="Search Again" />
                            <input type="text" readonly id="infoDisplay" class="info" />
                            <asp:Button runat="server" ID="uploadResults" OnClick="uploadResults_Click" ClientIDMode="Static" Text="Upload" CssClass="hidden" />
                        </div>
                        <div id="errorLog" style="margin-top:0.3%; margin-right:0.3%">
                            <textarea cols="20" rows="25" id="errorText"></textarea>
                        </div>
                    </ContentTemplate>
                </asp:UpdatePanel>

                <div class="flex-1" id="map-canvas">MAP</div>
    

    
            </div>



        </form>

    </body>
</html>

【问题讨论】:

  • 这信息不足以给出答案,发布更多相关代码。

标签: javascript google-maps google-chrome firefox


【解决方案1】:

您收到此错误是因为您两次加载 Google Maps API,这会导致冲突。您应该会在控制台中看到它。

因此,只需将 API 与您想要的库一起添加一次:

<asp:ScriptReference Path="https://maps.googleapis.com/maps/api/js?key=AIzaSyDjcLgEOEkdQfwxEyGOu4hHlSY_s-LOGkQ&libraries=places" />

【讨论】:

  • 嗯,是的,停止了该警告。但是根据 Firefox,Geocoder 仍然不是构造函数。
  • 奇怪.. 为我工作jsfiddle.net/6yvrfctc。或者至少没有错误,地图显示得很有趣,但这是另一个问题。
猜你喜欢
  • 2010-10-30
  • 2022-12-05
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 2023-02-14
相关资源
最近更新 更多