【问题标题】:Google Maps Cluster markers array format谷歌地图集群标记数组格式
【发布时间】:2017-11-30 20:38:39
【问题描述】:

我有简单的地图,其中包含数组中的集群和标记。标记是从位置数组中设置的,格式为:{lat: 36.4381369,lng: -93.0502099},我有很多坐标要从其他地图添加,但它们的格式为:(36.4381369,-93.0502099) 我需要以某种方式将第一种格式转换为第二种格式。 我尝试了 LatLng(36.4381369,-93.0502099) 和其他组合,但标记/集群未显示在地图上。

这可行,但需要位置数组没有 lat: lang: 在每个数字前面。

function initMap() {

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          //center: {lat: -41.850033, lng: -87.6500523}
        });
        map.setCenter(new google.maps.LatLng(35.850033, -98.6500523));

        // Create an array of alphabetical characters used to label the markers.
        var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

        // Add some markers to the map.
        // Note: The code uses the JavaScript Array.prototype.map() method to
        // create an array of markers based on a given "locations" array.
        // The map() method here has nothing to do with the Google Maps API.
        var markers = locations.map(function(location, i) {
          return new google.maps.Marker({
            position: location,
            label: labels[i % labels.length]
          });
        });

        // Add a marker clusterer to manage the markers.
        var markerCluster = new MarkerClusterer(map, markers,
            {zoomOnClick: false, imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
      }
      var locations = [
        {lat: 36.4381369,lng: -93.0502099},
        {lat: 36.2259742,lng: -92.6828437}
      ]

这不起作用。

 var markers = locations.map(function(location, i) {
        var point = location.maps.LatLng(location);
          return new google.maps.Marker({
            //position: new google.maps.LatLng(locations),
            //position: point,
            position: location,
            label: labels[i % labels.length]
          });
        });

        // Add a marker clusterer to manage the markers.
        var markerCluster = new MarkerClusterer(map, markers,
            {zoomOnClick: false, imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
      }
      var locations = [
        (36.4381369,-93.0502099),
        (36.2259742,-92.6828437)
      ]

【问题讨论】:

  • var locations = [(36.4381369,-93.0502099),(36.2259742,-92.6828437)] 不是有效的 JavaScript。您要解决的问题是什么?输入数据是字符串吗?这些数据是从哪里来的?您是否有理由不能使用有效的嵌套 JavaScript 数组:[[36.4381369,-93.0502099],[36.2259742,-92.6828437]]
  • 我试图避免 lat: lng: 在数组中,因为我可以从其他页面复制和粘贴位置,因为它们已经采用该格式 (36.4381369,-93.0502099) 而不是 {lat: 36.4381369,lng: -93.0502099} 在 var 位置部分代码。 [[36.4381369,-93.0502099],[36.2259742,-92.6828437]] 也不起作用,不返回任何标记/簇
  • 可以使嵌套的 JavaScript 数组工作,但您必须修改代码。
  • 那么我需要什么以及如何修改代码?我不是很好,我从谷歌地图示例页面复制了代码。谢谢。

标签: javascript arrays google-maps google-maps-api-3


【解决方案1】:

如果您将位置更改为有效的嵌套 javascript 数组,您可以修改代码以使用它来创建标记:

var locations = [
  [36.4381369,-93.0502099],
  [36.2259742,-92.6828437]
];

然后像这样创建你的标记:

var markers = locations.map(function(location, i) {
  var pt = {lat: location[0], lng: location[1]};
  return new google.maps.Marker({
    position: pt,
    label: labels[i % labels.length]
  });
});

proof of concept fiddle

代码 sn-p:

function initMap() {

  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    //center: {lat: -41.850033, lng: -87.6500523}
  });
  map.setCenter(new google.maps.LatLng(35.850033, -98.6500523));

  // Create an array of alphabetical characters used to label the markers.
  var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

  // Add some markers to the map.
  // Note: The code uses the JavaScript Array.prototype.map() method to
  // create an array of markers based on a given "locations" array.
  // The map() method here has nothing to do with the Google Maps API.
  var markers = locations.map(function(location, i) {
    var pt = {
      lat: location[0],
      lng: location[1]
    };
    return new google.maps.Marker({
      position: pt,
      label: labels[i % labels.length]
    });
  });

  // Add a marker clusterer to manage the markers.
  var markerCluster = new MarkerClusterer(map, markers, {
    zoomOnClick: false,
    imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
  });
}
var locations = [
  [36.4381369, -93.0502099],
  [36.2259742, -92.6828437]
]
google.maps.event.addDomListener(window, "load", initMap);
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/99a385c1/markerclustererplus/src/markerclusterer.js"></script>
<div id="map"></div>

【讨论】:

  • 那是缺失的部分,有效的嵌套数组以及更改标记创建代码的内容我无法弄清楚。谢谢,这行得通。
  • 我将您的答案标记为已接受,因为它直接回答了我的问题。现在是否可以为每个标记集群添加自定义标签和信息窗口?也许您已经在stackoverflow的其他地方回答了类似的问题,您可以指点我吗?非常感谢。
猜你喜欢
  • 2013-03-24
  • 1970-01-01
  • 2016-04-03
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
相关资源
最近更新 更多