【问题标题】:Google Map APIs - Combining geolocation and cluster markersGoogle Map APIs - 结合地理位置和集群标记
【发布时间】:2018-10-23 02:43:17
【问题描述】:

我正在为广告分类创建一个网站。我希望用户在他们所在位置附近的地图上看到该项目的位置。

将 Google 的 JS 代码模板用于 Geolocation 和 Cluster Markers 分开工作,当添加到同一个 JS 时,它们不起作用。有没有办法结合 JS 让他们在同一张地图上工作?

提前致谢

编辑 - 我已经添加了 JS 代码

/* 地理位置 */

   var map, infoWindow;
  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: -34.397, lng: 150.644},
      zoom: 6
    });
    infoWindow = new google.maps.InfoWindow;

    // Try HTML5 geolocation.
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var pos = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        };

        infoWindow.setPosition(pos);
        infoWindow.setContent('Location found.');
        infoWindow.open(map);
        map.setCenter(pos);
      }, function() {
        handleLocationError(true, infoWindow, map.getCenter());
      });
    } else {
      // Browser doesn't support Geolocation
      handleLocationError(false, infoWindow, map.getCenter());
    }
  }

  function handleLocationError(browserHasGeolocation, infoWindow, pos) {
    infoWindow.setPosition(pos);
    infoWindow.setContent(browserHasGeolocation ?
                          'Error: The Geolocation service failed.' :
                          'Error: Your browser doesn\'t support geolocation.');
    infoWindow.open(map);
  }

/* 地图聚类 */

函数 initMap() {

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 6,
      center: {lat: 55.378052, lng: -3.435973}
    });

    var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';


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

    var markerCluster = new MarkerClusterer(map, markers,
        {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
  }
  var locations = [
    {lat: 55.633792, lng: -4.661550},
    {lat: 55.864239, lng: -4.251806},
    {lat: 55.614302, lng: -4.665554},
    {lat: 55.543781, lng: -4.664010},
    {lat: 55.551993, lng: -4.623667},
    {lat: -26.246920, lng: -4.523914}
  ]

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    在没有任何具体示例的情况下回答您的问题有点困难。您需要将地理定位 JS 代码组合到集群标记上,这是一个显而易见的答案。因此,如果您有代码示例,我们将能够提供帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      相关资源
      最近更新 更多