在谷歌地图APi v3上切换标记组

时间:2015-04-12 19:27:22

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

我有一个带有一些标记的热图。我可以同时切换所有标记,并且我尝试按类别切换它们(例如:http://jsfiddle.net/huMtu/361/)。

我试图将位置var分为1和2(与var标记相同),但我是初学者,不知道为什么它不起作用。

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">    

    <title>Agenda</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
      #panel {
        position: absolute;
        top: 5px;
        left: 50%;
        margin-left: -180px;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=visualization"></script>
    <script>    



var map, pointarray, heatmap;

var taxiData = [

new google.maps.LatLng(-23.574917, -46.392787),
new google.maps.LatLng(-23.580376, -46.389232),
new google.maps.LatLng(-23.402486, -46.750809),
new google.maps.LatLng(-23.403860, -46.752856),
new google.maps.LatLng(-23.404044, -46.761739),
new google.maps.LatLng(-23.410588, -46.760297),
new google.maps.LatLng(-23.426872, -46.724692),
new google.maps.LatLng(-23.438656, -46.802163),
new google.maps.LatLng(-23.439153, -46.708424),
new google.maps.LatLng(-23.444647, -46.553665),
new google.maps.LatLng(-23.445394, -46.731450),
new google.maps.LatLng(-23.447098, -46.589690),
new google.maps.LatLng(-23.447697, -46.799269),
new google.maps.LatLng(-23.447982, -46.734821),
new google.maps.LatLng(-23.450721, -46.573869),
new google.maps.LatLng(-23.455090, -46.716561),
new google.maps.LatLng(-23.457128, -46.583936),
new google.maps.LatLng(-23.458946, -46.604814),
new google.maps.LatLng(-23.460150, -46.582184),
new google.maps.LatLng(-23.460162, -46.579276),
new google.maps.LatLng(-23.461555, -46.709902),
new google.maps.LatLng(-23.473637, -46.684330),
new google.maps.LatLng(-23.473637, -46.684330),
new google.maps.LatLng(-23.475226, -46.581618),
new google.maps.LatLng(-23.475864, -46.697348),
new google.maps.LatLng(-23.476219, -46.664631),
new google.maps.LatLng(-23.478657, -46.420357),
new google.maps.LatLng(-23.478879, -46.384664),
new google.maps.LatLng(-23.479145, -46.415483),
new google.maps.LatLng(-23.479875, -46.381180),


];





function initialize() {
  var mapOptions = {
    zoom: 11,
    center: new google.maps.LatLng(-23.5934217, -46.6627102),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  var pointArray = new google.maps.MVCArray(taxiData);
  heatmap = new google.maps.visualization.HeatmapLayer({data: pointArray});




  heatmap.setMap(map);





  var locations = [
['GREEN, 07/08/2014',-23.513202, -46.390331,'green-dot.png'],

    ];

    var infowindow = new google.maps.InfoWindow();
    var iconBase = 'http://maps.google.com/mapfiles/ms/icons/';
    var marker, i;
    window.markers = new Array();
    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map,
        icon: iconBase + locations[i][3]
      });
      markers.push(marker);
      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }

    function AutoCenter() {
      //  Create a new viewpoint bound
      var bounds = new google.maps.LatLngBounds();
      //  Go through each...
      for (var i = 0; i < markers.length; i++) {
      bounds.extend(markers[i].position)
      }
      //  Fit these bounds to the map
      map.fitBounds(bounds);
    }
    AutoCenter();

}


  var locations2 = [
['BLUE, 04/12/2014',-23.514345, -46.530521,'blue-dot.png'],
['RED, 21/05/2013',-23.514924, -46.538907,'red-dot.png'],
['YELLOW, 25/03/2015',-23.531998, -46.711497,'yellow-dot.png'],

    ];

    var infowindow2 = new google.maps.Infowindow2();
    var iconBase = 'http://maps.google.com/mapfiles/ms/icons/';
    var marker2, i;
    window.markers2 = new Array();
    for (i = 0; i < locations2.length; i++) {  
      marker2 = new google.maps.Marker2({
        position: new google.maps.LatLng(locations2[i][1], locations2[i][2]),
        map: map,
        icon: iconBase + locations2[i][3]
      });
      markers2.push(marker2);
      google.maps.event.addListener(marker2, 'click', (function(marker2, i) {
        return function() {
          infowindow2.setContent(locations2[i][0]);
          infowindow2.open(map, marker2);
        }
      })(marker2, i));
    }



function toggleMarkers() {
    if (markers[0].getMap() != null) {var arg = null;} else {var arg = map;
    }
    for (var i = 0; i < markers.length; i++) {markers[i].setMap(arg);}
}

function toggleMarkers2() {
    if (markers2[0].getMap() != null) {var arg = null;} else {var arg = map;
    }
    for (var i = 0; i < markers2.length; i++) {markers2[i].setMap(arg);}
}

function toggleHeatmap() {

    if (heatmap.getMap() != null) {
        heatmap.setMap(null);
    } else {
        heatmap.setMap(map);
    }
}


function changeGradient() {
  var gradient = [
    'rgba(0, 255, 255, 0)',
    'rgba(0, 255, 255, 1)',
    'rgba(0, 191, 255, 1)',
    'rgba(0, 127, 255, 1)',
    'rgba(0, 63, 255, 1)',
    'rgba(0, 0, 255, 1)',
    'rgba(0, 0, 223, 1)',
    'rgba(0, 0, 191, 1)',
    'rgba(0, 0, 159, 1)',
    'rgba(0, 0, 127, 1)',
    'rgba(63, 0, 91, 1)',
    'rgba(127, 0, 63, 1)',
    'rgba(191, 0, 31, 1)',
    'rgba(255, 0, 0, 1)']

heatmap.set('gradient', heatmap.get('gradient') ? null : gradient);}

function changeRadius() {
  heatmap.set('radius', heatmap.get('radius') ? null : 20);}

function changeOpacity() {
  heatmap.set('opacity', heatmap.get('opacity') ? null : 0.2);}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>

  <body>
    <div id="panel">
      <button onclick="toggleMarkers()">Toggle Markers</button>
      <button onclick="toggleMarkers2()">Toggle Markers2</button>
      <button onclick="toggleHeatmap()">Toggle Heatmap</button>
      <button onclick="changeGradient()">Change Color</button>
      <button onclick="changeRadius()">Change radius</button>
      <button onclick="changeOpacity()">Change pacity</button>
    </div>
    <div id="map-canvas"></div>
  </body>
</html>

你能帮我找到问题吗?

1 个答案:

答案 0 :(得分:0)

  1. Uncaught ReferenceError: markers is not defined
  2. Uncaught ReferenceError: markers2 is not defined
  3. Uncaught TypeError: undefined is not a function
    • var infowindow2 = new google.maps.Infowindow2();(有google.maps.Infowindow2对象)
    • marker2 = new google.maps.Marker2(没有google.maps.Marker2对象)。
  4. 修正那些和it works

    代码段:

    &#13;
    &#13;
    var map, pointarray, heatmap;
    var markers = [];
    var markers2 = [];
    
    var taxiData = [
    
      new google.maps.LatLng(-23.574917, -46.392787),
      new google.maps.LatLng(-23.580376, -46.389232),
      new google.maps.LatLng(-23.402486, -46.750809),
      new google.maps.LatLng(-23.403860, -46.752856),
      new google.maps.LatLng(-23.404044, -46.761739),
      new google.maps.LatLng(-23.410588, -46.760297),
      new google.maps.LatLng(-23.426872, -46.724692),
      new google.maps.LatLng(-23.438656, -46.802163),
      new google.maps.LatLng(-23.439153, -46.708424),
      new google.maps.LatLng(-23.444647, -46.553665),
      new google.maps.LatLng(-23.445394, -46.731450),
      new google.maps.LatLng(-23.447098, -46.589690),
      new google.maps.LatLng(-23.447697, -46.799269),
      new google.maps.LatLng(-23.447982, -46.734821),
      new google.maps.LatLng(-23.450721, -46.573869),
      new google.maps.LatLng(-23.455090, -46.716561),
      new google.maps.LatLng(-23.457128, -46.583936),
      new google.maps.LatLng(-23.458946, -46.604814),
      new google.maps.LatLng(-23.460150, -46.582184),
      new google.maps.LatLng(-23.460162, -46.579276),
      new google.maps.LatLng(-23.461555, -46.709902),
      new google.maps.LatLng(-23.473637, -46.684330),
      new google.maps.LatLng(-23.473637, -46.684330),
      new google.maps.LatLng(-23.475226, -46.581618),
      new google.maps.LatLng(-23.475864, -46.697348),
      new google.maps.LatLng(-23.476219, -46.664631),
      new google.maps.LatLng(-23.478657, -46.420357),
      new google.maps.LatLng(-23.478879, -46.384664),
      new google.maps.LatLng(-23.479145, -46.415483),
      new google.maps.LatLng(-23.479875, -46.381180),
    
    
    ];
    
    
    
    
    
    function initialize() {
      var mapOptions = {
        zoom: 11,
        center: new google.maps.LatLng(-23.5934217, -46.6627102),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
    
      map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);
    
      var pointArray = new google.maps.MVCArray(taxiData);
      heatmap = new google.maps.visualization.HeatmapLayer({
        data: pointArray
      });
    
    
    
    
      heatmap.setMap(map);
    
    
    
    
    
      var locations = [
        ['GREEN, 07/08/2014', -23.513202, -46.390331, 'green-dot.png']
    
      ];
    
      var infowindow = new google.maps.InfoWindow();
      var iconBase = 'http://maps.google.com/mapfiles/ms/icons/';
      var marker, i;
      window.markers = new Array();
      for (i = 0; i < locations.length; i++) {
        marker = new google.maps.Marker({
          position: new google.maps.LatLng(locations[i][1], locations[i][2]),
          map: map,
          icon: iconBase + locations[i][3]
        });
        markers.push(marker);
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
          return function() {
            infowindow.setContent(locations[i][0]);
            infowindow.open(map, marker);
          };
        })(marker, i));
      }
    
      function AutoCenter() {
        //  Create a new viewpoint bound
        var bounds = new google.maps.LatLngBounds();
        //  Go through each...
        for (var i = 0; i < markers.length; i++) {
          bounds.extend(markers[i].position)
        };
        //  Fit these bounds to the map
        map.fitBounds(bounds);
      }
      AutoCenter();
    
    }
    
    
    var locations2 = [
      ['BLUE, 04/12/2014', -23.514345, -46.530521, 'blue-dot.png'],
      ['RED, 21/05/2013', -23.514924, -46.538907, 'red-dot.png'],
      ['YELLOW, 25/03/2015', -23.531998, -46.711497, 'yellow-dot.png']
    
    ];
    
    var infowindow2 = new google.maps.InfoWindow();
    var iconBase = 'http://maps.google.com/mapfiles/ms/icons/';
    var marker2, i;
    window.markers2 = new Array();
    for (i = 0; i < locations2.length; i++) {
      marker2 = new google.maps.Marker({
        position: new google.maps.LatLng(locations2[i][1], locations2[i][2]),
        map: map,
        icon: iconBase + locations2[i][3]
      });
      markers2.push(marker2);
      google.maps.event.addListener(marker2, 'click', (function(marker2, i) {
        return function() {
          infowindow2.setContent(locations2[i][0]);
          infowindow2.open(map, marker2);
        };
      })(marker2, i));
    }
    
    
    
    function toggleMarkers() {
      if (markers[0].getMap() != null) {
        var arg = null;
      } else {
        var arg = map;
      }
      for (var i = 0; i < markers.length; i++) {
        markers[i].setMap(arg);
      }
    }
    
    function toggleMarkers2() {
      if (markers2[0].getMap() != null) {
        var arg = null;
      } else {
        var arg = map;
      }
      for (var i = 0; i < markers2.length; i++) {
        markers2[i].setMap(arg);
      }
    }
    
    function toggleHeatmap() {
    
      if (heatmap.getMap() != null) {
        heatmap.setMap(null);
      } else {
        heatmap.setMap(map);
      }
    }
    
    
    function changeGradient() {
      var gradient = [
        'rgba(0, 255, 255, 0)',
        'rgba(0, 255, 255, 1)',
        'rgba(0, 191, 255, 1)',
        'rgba(0, 127, 255, 1)',
        'rgba(0, 63, 255, 1)',
        'rgba(0, 0, 255, 1)',
        'rgba(0, 0, 223, 1)',
        'rgba(0, 0, 191, 1)',
        'rgba(0, 0, 159, 1)',
        'rgba(0, 0, 127, 1)',
        'rgba(63, 0, 91, 1)',
        'rgba(127, 0, 63, 1)',
        'rgba(191, 0, 31, 1)',
        'rgba(255, 0, 0, 1)'
      ];
    
      heatmap.set('gradient', heatmap.get('gradient') ? null : gradient);
    }
    
    function changeRadius() {
      heatmap.set('radius', heatmap.get('radius') ? null : 20);
    }
    
    function changeOpacity() {
      heatmap.set('opacity', heatmap.get('opacity') ? null : 0.2);
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    &#13;
    html,
    body,
    #map-canvas {
      height: 100%;
      margin: 0px;
      padding: 0px
    }
    #panel {
      position: absolute;
      top: 5px;
      left: 50%;
      margin-left: -180px;
      z-index: 5;
      background-color: #fff;
      padding: 5px;
      border: 1px solid #999;
    }
    &#13;
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=visualization"></script>
    <div id="panel">
      <button onclick="toggleMarkers()">Toggle Markers</button>
      <button onclick="toggleMarkers2()">Toggle Markers2</button>
      <button onclick="toggleHeatmap()">Toggle Heatmap</button>
      <button onclick="changeGradient()">Change Color</button>
      <button onclick="changeRadius()">Change Radius</button>
      <button onclick="changeOpacity()">Change Opacity</button>
    </div>
    <div id="map-canvas"></div>
    &#13;
    &#13;
    &#13;