Google Map无法在HTML / JS网站中使用

用户3465481

我在这里感到困惑,因为昨天一切正常,但是今天Google地图未显示在网站上。我没有更改任何代码,并且浏览器控制台中存在php或js错误。下面是完整的代码:

<section class="row map_row">
        <div class="container">
            <h3 class="contact_section_title">Street Address:</h3>
            <div id="gmap" class="row m0"></div>
        </div>
    </section>

//    google map start
    (function($) {
        "use strict";

        google.maps.event.addDomListener(window, 'load', init);

        var map;

        function init() {
            var mapOptions = {
                center: new google.maps.LatLng(26.201833, 78.159359),
                zoom: 13,
                zoomControl: true,
                zoomControlOptions: {
                    style: google.maps.ZoomControlStyle.DEFAULT,
                },
                panControl: true,
                disableDoubleClickZoom: false,
                mapTypeControl: false,
                mapTypeControlOptions: {
                    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                },
                scaleControl: true,
                scrollwheel: false,
                streetViewControl: false,
                draggable : true,
                overviewMapControl: false,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                styles: [ 
//                    { featureType: "administrative", elementType: "all", stylers: [ { visibility: "on" }, { saturation: -100 }, { lightness: 20 } ] },
//                    { featureType: "road", elementType: "all", stylers: [ { visibility: "on" }, { saturation: -100 }, { lightness: 40 } ] },
//                    { featureType: "water", elementType: "all", stylers: [ { visibility: "on" }, { saturation: -10 }, { lightness: 30 } ] },
//                    { featureType: "landscape.man_made", elementType: "all", stylers: [ { visibility: "simplified" }, { saturation: -60 }, { lightness: 10 } ] },
//                    { featureType: "landscape.natural", elementType: "all", stylers: [ { visibility: "simplified" }, { saturation: -60 }, { lightness: 60 } ] },
//                    { featureType: "poi", elementType: "all", stylers: [ { visibility: "off" }, { saturation: -100 }, { lightness: 60 } ] }, 
//                    { featureType: "transit", elementType: "all", stylers: [ { visibility: "off" }, { saturation: -100 }, { lightness: 60 } ] }
                ]

            }

            var mapElement = document.getElementById('gmap');
            var map = new google.maps.Map(mapElement, mapOptions);

            var locations = [
                ['', 26.201833, 78.159359]
            ];
            for (var i = 0; i < locations.length; i++) {
                var marker = new google.maps.Marker({
                    icon: 'images/map-marker.png',
                    animation: google.maps.Animation.BOUNCE,
                    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                    map: map,
                });
            }


    })(jQuery)
//    google map end

请帮助我。我还在学习这样的东西。

SpyderScript

您的代码中有很多错误,仅将这段HTML和Javascript混为一谈,很难弄清楚到底是什么错误。

  1. 您是说“浏览器控制台中没有php或js错误”吗?如果有错误,我们希望看到它。:)

  2. 您的JavaScript应该在<script></script>标记,否则浏览器将假定此代码为纯文本。

  3. 我将您的函数放入JSLint中,并经历了所有语法错误。我发现了几件事。首先,您可以map在JS开头附近定义var map;,因此您无需在此行varinit()函数后面加上关键字var map = new google.maps.Map(mapElement, mapOptions);其次,您不应该在数组的最后一个元素之后(或者如果数组中只有一个元素)放在逗号之后,例如在line中zoomControlOptions: {style: google.maps.ZoomControlStyle.DEFAULT,},还有其他几个地方发生了这种情况。第三,在for循环中,没有必要var在条件内包含关键字所以(i = 0; i < locations.length...代替(var i = 0; i < locations.length...最后,永远不要像在这里那样在循环内声明变量:

    for (var i = 0; i < locations.length; i++) {
        var marker = new google.maps.Marker({
            icon: 'images/map-marker.png',
            animation: google.maps.Animation.BOUNCE,
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map,
        });
    }
    

    从技术上讲,这类似于编写:

        var marker;
        var marker;
        var marker;
        var marker;
    

    一遍又一遍。每个范围只能有一个标记变量。我建议使用数组存储标记,如下所示:

    var markers = [];
    for (i = 0; i < locations.length; i++) {
        markers.push(new google.maps.Marker({
            icon: 'images/map-marker.png',
            animation: google.maps.Animation.BOUNCE,
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
        });
    }
    

    这是我编辑后的所有JavaScript:

    function loadMap() {
      "use strict";
    
      google.maps.event.addDomListener(window, 'load', init);
    
      function init() {
        var mapOptions = {
          center: new google.maps.LatLng(26.201833, 78.159359),
          zoom: 13,
          zoomControl: true,
          zoomControlOptions: {
            style: google.maps.ZoomControlStyle.DEFAULT
          },
          panControl: true,
          disableDoubleClickZoom: false,
          mapTypeControl: false,
          mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
          },
          scaleControl: true,
          scrollwheel: false,
          streetViewControl: false,
          draggable: true,
          overviewMapControl: false,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          styles: [{
            featureType: "administrative",
            elementType: "all",
            stylers: [{
              visibility: "on"
            }, {
              saturation: -100
            }, {
              lightness: 20
            }]
          }, {
            featureType: "road",
            elementType: "all",
            stylers: [{
              visibility: "on"
            }, {
              saturation: -100
            }, {
              lightness: 40
            }]
          }, {
            featureType: "water",
            elementType: "all",
            stylers: [{
              visibility: "on"
            }, {
              saturation: -10
            }, {
              lightness: 30
            }]
          }, {
            featureType: "landscape.man_made",
            elementType: "all",
            stylers: [{
              visibility: "simplified"
            }, {
              saturation: -60
            }, {
              lightness: 10
            }]
          }, {
            featureType: "landscape.natural",
            elementType: "all",
            stylers: [{
              visibility: "simplified"
            }, {
              saturation: -60
            }, {
              lightness: 60
            }]
          }, {
            featureType: "poi",
            elementType: "all",
            stylers: [{
              visibility: "off"
            }, {
              saturation: -100
            }, {
              lightness: 60
            }]
          }, {
            featureType: "transit",
            elementType: "all",
            stylers: [{
              visibility: "off"
            }, {
              saturation: -100
            }, {
              lightness: 60
            }]
          }]
    
        };
    
        var mapElement = document.getElementById('gmap');
        var map = new google.maps.Map(mapElement, mapOptions);
    
        var locations = [
          ['', 26.201833, 78.159359]
        ];
        var markers = [];
        for (i = 0; i < locations.length; i++) {
          markers.push(new google.maps.Marker({
            icon: 'images/map-marker.png',
            animation: google.maps.Animation.BOUNCE,
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
          }));
        }
      }
    }
      loadMap();
    

    如果您使用此方法,我无法保证会成功,但是至少可以避免一些大错误,也许您可​​以进行一些调试。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

网站无法在IE8中使用,但可以在Google Chrome中使用

在Google网站中使用Google Visualization API

使用Google Map v2无法显示Google Map

在Google Map中使用变量

如何使用CSS,JS和HTML将Google搜索面板添加到我的网站?

按照Google Map网站的指示进行操作后,Google Map无法正常工作

无法使AngularJS Google Map居中/ AngularJS Google Maps API的使用

无法在本地HTML文件的Webview中加载Google Map

Google Map无法显示在HTML <div>上吗?

如何在Awesomium中使用Google Map?

在Vuejs中使用Google Map Marker集群

在Vue / Laravel中使用Google Map

在React Component中使用Google Map

在React组件中使用Google Map API

无法通过Angular.js加载Google Map URL

如何使用我的网站的图片自定义Google Map

我如何使用css,js和html将google搜索面板添加到我的网站?

无法在Google App引擎中使用TfidfVectorizer

无法在GAE应用中使用Google Cloud

无法在Google Apps脚本中使用fetchURL()

无法在 Google Cloud Datalab 中使用 graphviz

Google Map Initialize无法与Tab一起使用

无法使用Flutter中的筛选标记填充Google Map

无法使用PrimeFaces用Google Map坐标填充表格

Google Map无法在设备上使用api v1

无法使用 Google Apps 脚本从网站读取信息

无法使用 Google Sheet 中的 IMPORTHTML 或 IMPORTXML 从网站抓取数据

Google Gauge Animation API无法在Google Ajax Playground中使用

使用Backbone.js进行首次查看后,Google Map无法正确呈现