14

我遇到了标记重叠的问题,请不要投反对票,因为我在 javascript 方面没有受过高等教育,我也查看了 stackoverflow 上提供的不同答案,但无法使用它们找到解决方案,所以请帮忙!该代码根据我给出的静态点为 lat 和 long 抓取 php 我只想在同一个标​​记中列出所有信息,而不是让它重叠标记任何帮助将不胜感激这里是代码:

  <head>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<META HTTP-EQUIV="refresh" >
<title>Operations</title>

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=APIKEY&sensor=false"></script>
<script src="oms.min.js"></script>
<script type="text/javascript">
//<![CDATA[

var customIcons = {      

restaurant: {        icon: 'mm_20_blue.png'      },      

bar: {        icon: 'mm_20_blue.png'      }    

};

function load() {
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(32.298342, -64.791098),
    zoom: 17,
    mapTypeId: google.maps.MapTypeId.SATELLITE
  });
  var infoWindow = new google.maps.InfoWindow;

  // Change this depending on the name of your PHP file
  downloadUrl("genxml.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var name = markers[i].getAttribute("name");
      var address = markers[i].getAttribute("address");
      var type = markers[i].getAttribute("type");
       var id = markers[i].getAttribute("permitnumber");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = "<b>" + name + "</b> <br/>" + address + "<br/>" + '<a href="http://server/editform.php?find=' + id + '" >More Info</a> <br/> <form action="del.php" method="post" enctype="multipart/form-data"> <input type="checkbox" name="checkbox" id="checkbox" value="' + id +'"> <input name="delete" type="submit" id="delete" value="Delete">  ' ;
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow,
        animation: google.maps.Animation.BOUNCE

      });

      bindInfoWindow(marker, map, infoWindow, html);
    }
   });
   }




    function bindInfoWindow(marker, map, infoWindow, html) {
    google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
   });
   }

   function downloadUrl(url, callback) {
   var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

   request.onreadystatechange = function() {
     if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request, request.status);
    }
    };

   request.open('GET', url, true);
   request.send(null);
   }

    function doNothing() {}



   //]]>

  </script>


  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
  <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.min.js" type="text/javascript"></script>
  <script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-24568877-1']);
  _gaq.push(['_trackPageview']);

   (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
     ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-         analytics.com/ga.js';
     var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
     })();

    </script>

   </head>

   <body onload="load()" bgcolor="#A8F748" >

  <div id="map" style="margin:auto; width: 90%; height: 470px"></div>






  </body>
4

6 回答 6

9

如果您不希望标记重叠,您可能需要使用google-maps-utility-library-v3。它有一个名为 Marker Clusterer 的功能,它基本上允许您将附近的标记放入一个单一的标记中。可以对其进行配置,以便一旦用户放大到足够远,这些标记就会重新显示为单独的实体。这是一个演示

于 2013-11-06T19:51:19.283 回答
3

我用这个 - https://github.com/jawj/OverlappingMarkerSpiderfier

在此处查看演示 - http://jawj.github.io/OverlappingMarkerSpiderfier/demo.html

于 2013-11-08T20:29:44.453 回答
1

我通过大量搜索找到了一个解决方案,并且感谢@geocodezip 的帖子,当放大标记时我仍然遇到一个小问题,任何直接位于另一个标记上的标记都将保持集群模式,我想实现 infoWindow 共享两个标记内容任何帮助都会很棒,再次感谢 geocodezip 的帖子!

<!DOCTYPE html >
<head>

    <meta charset="utf-8"/>
    <title>Google Maps API V3 with Marker Manager</title>
    <meta name="description" content="Google Maps API V3 with MarkerCluster, Coincident Markers Share Infowindow" />
    <meta name="keywords" content="google maps api v3, markerclusterer, markers, infowindow" />
    <meta name="author" content="Casey P. Thomas" />
    <script type="text/javascript"  src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript"  src="http://maps.caseypthomas.org/markerclusterer.js"></script>
<script type="text/javascript">
//<![CDATA[
        var map;

    //marker clusterer
    var mc;
    var mcOptions = {gridSize: 20, maxZoom: 17};
    var markers;        
    //global infowindow
    var infowindow = new google.maps.InfoWindow();

    //geocoder
    var geocoder = new google.maps.Geocoder(); 


  var customIcons = {
  restaurant: {
    icon: 'mm_20_blue.png',
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  },
  bar: {
    icon: 'mm_20_blue.png',
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  }
 };

 function load() {
  var cluster = [];
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(32.298342, -64.791098),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.SATELLITE
  });
var infowindow = new google.maps.InfoWindow();

  // Change this depending on the name of your PHP file
  downloadUrl("genxml.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var name = markers[i].getAttribute("name");
      var address = markers[i].getAttribute("address");
      var type = markers[i].getAttribute("type");
       var id = markers[i].getAttribute("permitnumber");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = "<b>" + name + "</b> <br/>" + address + "<br/>" + '<a href="http://server/editform.php?find=' + id + '" >More Info</a> <br/> <form action="del.php" method="post" enctype="multipart/form-data"> <input type="checkbox" name="checkbox" id="checkbox" value="' + id +'"> <input name="delete" type="submit" id="delete" value="Delete">  ' ;
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow,
        animation: google.maps.Animation.BOUNCE
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                         var id = markers[i].getAttribute("permitnumber");
                        var name = markers[i].getAttribute("name");
                        var address = markers[i].getAttribute("address");
                        var type = markers[i].getAttribute("type");
                        infowindow.setContent("<b>" + name + "</b> <br/>" + address + "<br/>" + '<a href="http://server/editform.php?find=' + id + '" >More Info</a> <br/> <form action="del.php" method="post" enctype="multipart/form-data"> <input type="checkbox" name="checkbox" id="checkbox" value="' + id +'"> <input name="delete" type="submit" id="delete" value="Delete">  ');
                        infowindow.open(map, marker);

                    }
                })(marker, i));
      cluster.push(marker);
    }
    var mc = new MarkerClusterer(map,cluster);
  });
}

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}

function downloadUrl(url, callback) {
  var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
  }

  function doNothing() {}

  //]]>
 </script> 



 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
 <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.min.js" type="text/javascript"></script>
 <script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-24568877-1']);
_gaq.push(['_trackPageview']);

 (function() {
 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-  analytics.com/ga.js';
 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();

 </script>


 </head>

 <body onload="load()" bgcolor="#A8F748" >

 <div id="map" style="margin:auto; width: 90%; height: 470px"></div>






</body>

</html>
于 2013-11-08T20:25:00.930 回答
0

您可以使用谷歌地图实用程序库中的 MarkerClusterer。

这是一个分步示例:https ://developers.google.com/maps/documentation/javascript/marker-clustering

您可以从 google maps github repo 下载该库: https ://github.com/googlemaps/v3-utility-library/tree/master/markerclusterer

于 2017-02-21T08:44:57.727 回答
0

向标记纬度和经度添加小的随机数。

这会使重叠标记围绕其正确位置移动。放大时,标记将分开。较小的随机数可以提高准确性,但需要更多的放大。还要更改标记图标或标签以指示它是重叠的。

在此示例中,使用了四种不同的标记。如果重叠点超过 4 个,则添加轻微随机化。

示例:http ://waveneyramblers.org.uk/walks/next_eight_days

查看源代码以查看 JavaScript。

于 2017-05-08T12:53:37.740 回答
0

这是一个不使用任何库的解决方案,而只是简单的 javascript 作为重叠问题的解决方法。

它仅在相同 lat 和 lng 具有超过 1 个标记的标记上打开一个信息窗口

var maplocations = [["1200€", "52.50752849999999", "13.471243500000014", "96063", "No", 1],
["12€", "52.50752849999999", "13.471243500000014", "198866", "No", 1],
["12€", "52.504747174113696", "13.420449523925754", "228262", "No", 1],
["888€", "52.5024031", "13.377901000000065", "228317", "No", 1]]

 function initMap() {

        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 10,
            center: new google.maps.LatLng(52.530339,13.347451),
            mapTypeId: google.maps.MapTypeId.ROADMAP
          });


         groupedMapList=[];
        for(var i=0;i<maplocations.length;i++){
            if(!groupedMapList.find(item => item[1] == maplocations[i][1] && item[2]== maplocations[i][2])){
                groupedMapList.push(maplocations[i])
            }else{
               let valueIndex = groupedMapList.findIndex(item => item[1] == maplocations[i][1] && item[2]== maplocations[i][2]);
               groupedMapList[valueIndex][5] = groupedMapList[valueIndex][5] + 1;
            }
        }

您将获得一个需要在地图上映射的分组列表。仅将信息窗口添加到超过 1 的项目 - “groupedMapList[i][5]>1”

var groupedMapList = [ [“1200€”,“52.50752849999999”,“13.471243500000014”,“96063”,“否”,2],[“12€”,“52.504747174113696”,“13.420449523925754”,“22”“28” , 1], ["888€", "52.5024031", "13.377901000000065", "228317", "否", 1]]

        var infowindow = new google.maps.InfoWindow();
        //   var bounds = new google.maps.LatLngBounds();
          var marker, i;
          var prevId;
          var prevMarker;

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

                marker = new google.maps.Marker({
                position: new google.maps.LatLng(parseFloat(groupedMapList[i][1]), parseFloat(groupedMapList[i][2])),
                label: {text: groupedMapList[i][0], color: "black"},
                map: map
                });

              google.maps.event.addListener(marker, 'click', (function(marker, i) {
              return function() {
                //reset previous marker

                if(prevMarker)
                {

                    var label = prevMarker.getLabel();
                    label.color="black";
                    prevMarker.setLabel(label); 
                    prevMarker.setZIndex();
                    infowindow.close();
                }

                if(groupedMapList[i][5] > 1){
                    let contentString = '<div id="content">'+ '<b>'+ groupedMapList[i][5] +' Apartments</b></div>'
                    infowindow.setContent(contentString);
                    infowindow.open(map, marker);
                }

                prevId = groupedMapList[i][3];
                prevMarker = this;

                var label = this.getLabel();
                label.color="white";
                this.setLabel(label); 
                this.setZIndex(1001);

              }

            })(marker, i));

        }

    }
于 2019-10-25T14:11:40.207 回答