使用 JSON 的 Google 地图标记。地图上仅显示循环中的最后一个标记

Grem357

我正在使用 JSON(本地文件)加载一些标记信息,但由于某种原因,只有循环中的最后一个标记显示在地图上。

我可以看到在控制台中正确打印了所有坐标,所以我很确定循环很好,而且大部分代码都很好。

<script>
  function myMap() {

        var mapCanvas = document.getElementById("content_map" );
        var mapOptions = {
            center: new google.maps.LatLng(55.812757, -4.195363),
            zoom: 5,
            disableDefaultUI: true,
            styles: [
                  {
                    "elementType": "geometry",

                    "stylers": [
                      {
                        "color": "#f5f5f5"
                      }
                    ]
                  },
                  {
                    "elementType": "labels.icon",
                    "stylers": [
                                  {
                                    "visibility": "off"
                                  }
                                ]
                              },
                              {
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#616161"
                                  }
                                ]
                              },
                              {
                                "elementType": "labels.text.stroke",
                                "stylers": [
                                  {
                                    "color": "#f5f5f5"
                                  }
                                ]
                              },
                              {
                                "featureType": "administrative.land_parcel",
                                "elementType": "labels",
                                "stylers": [
                                  {
                                    "visibility": "on"
                                  }
                                ]
                              },
                              {
                                "featureType": "administrative.land_parcel",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#bdbdbd"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#eeeeee"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi",
                                "elementType": "labels.text",
                                "stylers": [
                                  {
                                    "visibility": "on"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#757575"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi.business",
                                "stylers": [
                                  {
                                    "visibility": "on"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi.park",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#e5e5e5"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi.park",
                                "elementType": "labels.text",
                                "stylers": [
                                  {
                                    "visibility": "on"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi.park",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#0d99ba"
                                  }
                                ]
                              },
                              {
                                "featureType": "road",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#ffffff"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.arterial",
                                "elementType": "labels",
                                "stylers": [
                                  {
                                    "visibility": "on"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.arterial",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#757575"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.highway",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#9e9e9e"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.highway",
                                "elementType": "labels",
                                "stylers": [
                                  {
                                    "visibility": "on"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.highway",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#616161"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.local",
                                "stylers": [
                                  {
                                    "visibility": "off"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.local",
                                "elementType": "labels",
                                "stylers": [
                                  {
                                    "visibility": "off"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.local",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#9e9e9e"
                                  }
                                ]
                              },
                              {
                                "featureType": "transit.line",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#e5e5e5"
                                  }
                                ]
                              },
                              {
                                "featureType": "transit.station",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#eeeeee"
                                  }
                                ]
                              },
                              {
                                "featureType": "water",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#0d99ba"
                                  }
                                ]
                              },
                              {
                                "featureType": "water",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#9e9e9e"
                                  }
                                ]
                              }
                            ]
        };
        var contentString = '<div id="content">'+

        '<img src="./img/logo_img_map.png" alt="">'+
        '</div>';

        var infowindow = new google.maps.InfoWindow({
          content: contentString
        });


        $.getJSON('data.json', function(json) {



            for (i = 0; i < json.ChargeDevice.length; i++) { 

                addMarker(json.ChargeDevice[i].ChargeDeviceLocation.Latitude,json.ChargeDevice[i].ChargeDeviceLocation.Longitude);
                console.log(json.ChargeDevice[i].ChargeDeviceLocation.Latitude, json.ChargeDevice[i].ChargeDeviceLocation.Longitude);
            };


            function addMarker(lat,lng) {

                    var image = './img/logo_img_map.png';
                    var map = new google.maps.Map(mapCanvas, mapOptions);
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(lat,lng),
                        map: map,
                        icon: image
                    });

            };




      });

    };

  </script>
  <script src="https://maps.googleapis.com/maps/api/js?key=MYKEY"></script>

我现在已经查看了大量线程,但我无法弄清楚为什么它在这种情况下不起作用。我正在运行我的函数,因为它发生在循环中......所以标记真的不应该保留吗?

注意:我的 API 密钥已被删除,因此它不会在线显示,但它可以在我的网络服务器上运行并且地图加载正常。实际上只有一个标记显示,而我的 JSON 文件中有 2 个数据集。

我还没有完成点击环境,但我认为现在这不重要,是吗?

任何帮助,将不胜感激!

蜜蜂

我认为问题可能在于您为每个标记声明了一张新地图。我会声明一次地图,并为每个标记引用它。

请尝试以下操作:

<script>
  function myMap() {

        var mapCanvas = document.getElementById("content_map" );
        var mapOptions = {
            center: new google.maps.LatLng(55.812757, -4.195363),
            zoom: 5,
            disableDefaultUI: true,
            styles: [
                  {
                    "elementType": "geometry",

                    "stylers": [
                      {
                        "color": "#f5f5f5"
                      }
                    ]
                  },
                  {
                    "elementType": "labels.icon",
                    "stylers": [
                                  {
                                    "visibility": "off"
                                  }
                                ]
                              },
                              {
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#616161"
                                  }
                                ]
                              },
                              {
                                "elementType": "labels.text.stroke",
                                "stylers": [
                                  {
                                    "color": "#f5f5f5"
                                  }
                                ]
                              },
                              {
                                "featureType": "administrative.land_parcel",
                                "elementType": "labels",
                                "stylers": [
                                  {
                                    "visibility": "on"
                                  }
                                ]
                              },
                              {
                                "featureType": "administrative.land_parcel",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#bdbdbd"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#eeeeee"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi",
                                "elementType": "labels.text",
                                "stylers": [
                                  {
                                    "visibility": "on"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#757575"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi.business",
                                "stylers": [
                                  {
                                    "visibility": "on"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi.park",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#e5e5e5"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi.park",
                                "elementType": "labels.text",
                                "stylers": [
                                  {
                                    "visibility": "on"
                                  }
                                ]
                              },
                              {
                                "featureType": "poi.park",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#0d99ba"
                                  }
                                ]
                              },
                              {
                                "featureType": "road",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#ffffff"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.arterial",
                                "elementType": "labels",
                                "stylers": [
                                  {
                                    "visibility": "on"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.arterial",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#757575"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.highway",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#9e9e9e"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.highway",
                                "elementType": "labels",
                                "stylers": [
                                  {
                                    "visibility": "on"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.highway",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#616161"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.local",
                                "stylers": [
                                  {
                                    "visibility": "off"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.local",
                                "elementType": "labels",
                                "stylers": [
                                  {
                                    "visibility": "off"
                                  }
                                ]
                              },
                              {
                                "featureType": "road.local",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#9e9e9e"
                                  }
                                ]
                              },
                              {
                                "featureType": "transit.line",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#e5e5e5"
                                  }
                                ]
                              },
                              {
                                "featureType": "transit.station",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#eeeeee"
                                  }
                                ]
                              },
                              {
                                "featureType": "water",
                                "elementType": "geometry",
                                "stylers": [
                                  {
                                    "color": "#0d99ba"
                                  }
                                ]
                              },
                              {
                                "featureType": "water",
                                "elementType": "labels.text.fill",
                                "stylers": [
                                  {
                                    "color": "#9e9e9e"
                                  }
                                ]
                              }
                            ]
        };

        var map = new google.maps.Map(mapCanvas, mapOptions);
        var contentString = '<div id="content">'+

        '<img src="./img/logo_img_map.png" alt="">'+
        '</div>';

        var infowindow = new google.maps.InfoWindow({
          content: contentString
        });


        $.getJSON('data.json', function(json) {



            for (var i = 0; i < json.ChargeDevice.length; i++) { 

                addMarker(json.ChargeDevice[i].ChargeDeviceLocation.Latitude,json.ChargeDevice[i].ChargeDeviceLocation.Longitude);
                console.log(json.ChargeDevice[i].ChargeDeviceLocation.Latitude, json.ChargeDevice[i].ChargeDeviceLocation.Longitude);
            };


            function addMarker(lat,lng) {

                    var image = './img/logo_img_map.png';
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(lat,lng),
                        map: map,
                        icon: image
                    });

            };




      });

    };

  </script>
  <script src="https://maps.googleapis.com/maps/api/js?key=MYKEY"></script>

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

未使用JSON在Google地图中显示的标记

使用按钮在Google地图上添加标记

如何将React与Google Places API结合使用,以在Google地图上显示位置标记?

将 JSON 文件链接到 google-map-react 以在地图上显示标记

如何将json ajax用于Google地图标记

Google地图,绘制带有多个标记的路线,隐藏最后一个标记以外的标记

如何使用Ajax在Google地图上设置标记?

使用簇自定义Google地图上的标记

如何使用Kotlin在Google地图上添加标记?

隐藏和显示无法在我的Google地图上使用的自定义标记

使用Google Maps API从表格中获取地址并在地图上显示标记,但标记的悬停标题或infoWindow(onclick)无效

如何使用 asynctask 从 URL 获取 Google 地图标记?

从服务器检索位置并使用排球库显示在Google地图标记中?

使用json将infoWindow添加到地图标记

标记未显示在Google地图上

Google标记未显示在地图上

标记未在Google地图上显示

循环声明Google地图标记

从json数据在Google地图上显示路径

使用 Javascript 在 Google 地图中显示/隐藏标记集群

如何将Xib作为标记放置在Google地图上而不是Google地图标记图标

Android设备上的地图仅显示网址中的一个标记,但显示使用Toast时的所有值

清除Google地图上的标记?

Google地图上的多个标记

使用Flutter在Google地图中添加标记

在同一个HTML页面Google地图上使用initMap和initAutocomplete

在角度Google地图上创建动态标记图标

如何在Google地图上获得平滑的标记(SVG图标)?

Google地图上的多个标记带有不同的图标?