【问题标题】:markerclusterer not working php mysql jsonmarkerclusterer 不工作 php mysql json
【发布时间】:2019-02-15 05:06:43
【问题描述】:

需要工作的 MarkerClusterer。 我按照例子https://googlemaps.github.io/js-marker-clusterer/docs/examples.html

这对我不起作用。

我的源代码

 var buildings_map = <?php echo json_encode( $buildings_map ) ?>;
var infobox = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
function setMarker(building) {
  var latlng = new google.maps.LatLng(parseFloat(building.lat), parseFloat(building.lng));
 console.log(latlng);
  bounds.extend(latlng);
  var buildingMarkers = new google.maps.Marker({
    position: {lat: parseFloat(building.lat), lng: parseFloat(building.lng)},
    map: map,
    title: building.name
  });

  markerCluster = new MarkerClusterer(map, buildingMarkers);
  google.maps.event.addListener(buildingMarkers, 'click', function () {
    infobox.close();

    infobox.setContent(building.content);
    infobox.open(map, buildingMarkers);
  });

  google.maps.event.addListener(infobox, 'domready', function () {
    var iwOuter = $('.gm-style-iw');

【问题讨论】:

  • 请提供一个minimal reproducible example 来说明您的问题在问题本身,而不是(仅)指向外部站点的链接(最好是工作的 StackOverflow 代码 sn-p)
  • 好的。我编辑了。 ty
  • 请帮助我。 initMap&language=pt-br:123.
  • 请提供示例数据(buildings_map )和有效的javascript;所以你的问题中有一个minimal reproducible example。 (问题只有23行,第123行是什么?)
  • 我的代码很长..不可能在这里发帖..stackoverflow不允许。

标签: php mysql api google-maps markerclusterer


【解决方案1】:

您正在 setMarker 函数中创建 markerClusterer。它将继续为每个建筑标记重新初始化 markerClusterer。

在 for 循环之后移动 markerCluster 创建。我还将 setMarker 函数移到了 initMap 函数之外。

您可以替换脚本中的以下代码并进行测试。

<script>
var markersArray = new Array();

function initMap() {

    var centerMap = <?php echo json_encode( $centerMap ) ?>;
    var map = new google.maps.Map(document.getElementById('map-imoveis'), {
      zoom: 4,
      center: {lat: parseFloat(centerMap.lat), lng: parseFloat(centerMap.lng)},      
      zoomControl: true,
      language: 'pt-br'    
    });

    var buildings_map = <?php echo json_encode( $buildings_map ) ?>;
    var infobox = new google.maps.InfoWindow();
    var bounds = new google.maps.LatLngBounds();

    for (building in buildings_map) {

        if (buildings_map.hasOwnProperty(building)) {
            setMarker(buildings_map[building]);
        }
        console.log(buildings_map[building]);      
    }


    // J - CREATE MARKER CLUSTER USING THE MARKERS ARRAY 
    var markerCluster = new MarkerClusterer(map, markersArray, {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});

    if (bounds.getNorthEast().equals(bounds.getSouthWest())) {
      var extendPoint1 = new google.maps.LatLng(bounds.getNorthEast().lat() + 0.01, bounds.getNorthEast().lng() + 0.01);
      var extendPoint2 = new google.maps.LatLng(bounds.getNorthEast().lat() - 0.01, bounds.getNorthEast().lng() - 0.01);
      bounds.extend(extendPoint1);
      bounds.extend(extendPoint2);
    }

    map.fitBounds(bounds);
  }


function setMarker(building) {
      var latlng = new google.maps.LatLng(parseFloat(building.lat), parseFloat(building.lng));
      console.log(latlng);
      bounds.extend(latlng);
      var buildingMarkers = new google.maps.Marker({
        position: {lat: parseFloat(building.lat), lng: parseFloat(building.lng)},
        map: map,
        title: building.name
      });

      // J - ADD MARKER TO THE ARRAY. 
      markersArray.push(marker);

      google.maps.event.addListener(buildingMarkers, 'click', function () {
        infobox.close();

        infobox.setContent(building.content);
        infobox.open(map, buildingMarkers);
      });

      google.maps.event.addListener(infobox, 'domready', function () {
        var iwOuter = $('.gm-style-iw');

        iwOuter.css({ 'left': '0', 'width': '100%' });

        var iwBackground = iwOuter.prev();
        iwBackground.parent().css({'width': '330px'});

        iwBackground.children(':nth-child(2)').css({
          'background-color': 'transparent', 'box-shadow': 'none'
        });


        iwBackground.children(':nth-child(4)').css({
          'background-color': 'transparent'
        });

        var iwX = iwOuter.next();
        iwX.css({'right': '0', 'top': '15px'});

        iwX.children().remove();

        iwX.css({ 'width': '28px', 'height': '28px' }).append('<span style="color: #b22c2c; font-size: 22px;"><i class="rimo rimo-close">X</span>');

        iwBackground.children(':nth-child(3)').children(':first-child').children(':first-child').css({'background-color': '#fff', 'z-index': '1000', 'display': 'none', 'box-shadow': 'none'});
        iwBackground.children(':nth-child(3)').children(':last-child').children(':first-child').css({'background-color': '#fff', 'z-index': '1000', 'display': 'none', 'box-shadow': 'none'});
      });
    }  

</script>   

【讨论】:

  • 嗨,求助..它的错误:未捕获的 Kc {消息:“不是 LatLng 或 LatLngLiteral:在属性 lat:不是数字”,名称:“InvalidValueError”,堆栈:“错误↵在新 Kc (maps.googleapis.com/m...maps-api-v3/api/js/36/2/intl/pt_br/map.js:36:350)"}
  • 在控制台打印building.latbuilding.lng的值,检查是否得到正确的值。如果它们不是数字,则parseFloat 方法将抛出错误
  • 你好,在智能手机上工作正常。但是在电脑上不行,出现这个错误,请问是什么情况?
  • 您是否检查了发送到智能手机的building.latbuilding.lng 的值?
  • console.log(building.lat,building.lng) 结果.. -22.903500 -43.209621 -22.953388 -43.078121 -22.977592 -43.191483 -22.877958 -42.028561 -22.406187 -4........跨度>
猜你喜欢
  • 2013-01-21
  • 2015-10-20
  • 1970-01-01
  • 2018-09-26
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-29
相关资源
最近更新 更多