谷歌地图中图层的复选框

Checkbox for layers in Google Maps

本文关键字:复选框 图层 谷歌地图      更新时间:2023-09-26

我的目标是有四组不同的标记(每组中可以是三种不同的颜色)。我需要能够根据地图顶部的复选框打开/关闭:组 1、2、3、4 以及按颜色过滤这些组的方法(例如:组 1、组 2、组 3、组 4 和红色、黄色、绿色)。我最初为下拉列表设置了这个;现在我在获取任何复选框以响应这些点击事件时遇到问题。我不是一个有经验的开发人员,所以如果我没有正确解释这一点,我提前道歉,任何建议都值得赞赏。代码如下:

<!DOCTYPE html> <!-- saved from url=(0014)about:internet  -->    
<html>
  <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta charset="utf-8">
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
        <link rel="stylesheet" type="text/css" href="included.css">
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
        <Title>Web Tool</Title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <div data-role="page"> 
    <div data-role="header"> 
      <h1>jQuery Header</h1>   
    </div><!-- /header -->
    <br>    
    </div>
    <br>
        <script type="text/javascript"
          src="http://maps.googleapis.com/maps/api/js?key=myKey&sensor=false">
        </script>
        <br>
        <script>
    var map;
    var markersArray = [];  
    var infoWindow;     
    var places = [ 
        ['loc1', 47.364, -92.690, 12, '<h2>place 1</h2>'], //Test RED 
        ['loc2', 43.711, -95.719, 1, '<h2>place 2</h2>'], //Test YLW 
        ['loc3', 44.947, -92.854, 2, '<h2>place 3</h2>'], //Test GRN 
        ['loc4', 45.899, -91.521, 10, '<h2>place 4</h2>'], //Test RED 
        ['loc5', 45.223, -91.127, 5, '<h2>place 5</h2>'], //Test YLW 
        ['loc6', 46.448, -90.166, 2, '<h2>place 6</h2>'], //Test GRN 
        ['loc7', 40.471, -107.580, 3, '<h2>place 7</h2>'], //Test RED 
        ['loc8', 38.208, -104.574, 11, '<h2>place 8</h2>'], //Test YLW 
        ['loc9', 39.623, -104.452, 2, '<h2>place 9</h2>'], //Test GRN 
        ['loc10', 33.186, -101.407, 3, '<h2>place 10</h2>'], //Test RED
        ['loc11', 32.210, -103.262, 9, '<h2>place 11</h2>'], //Test YLW
        ['loc12', 33.991, -103.858, 4, '<h2>place 12</h2>'], //Test GRN
        ['loc13', 47.364, -92.690, 3, '<h2>place 1</h2>'], //Test RED 
        ['loc14', 43.711, -95.719, 1, '<h2>place 2</h2>'], //Test YLW 
        ['loc15', 44.947, -92.854, 6, '<h2>place 3</h2>'], //Test GRN 
        ['loc16', 45.899, -91.521, 3, '<h2>place 4</h2>'], //Test GRN 
        ['loc17', 45.223, -91.127, 8, '<h2>place 5</h2>'], //Test YLW 
        ['loc18', 46.448, -90.166, 2, '<h2>place 6</h2>'], //Test GRN 
        ['loc19', 40.471, -107.580, 0, '<h2>place 7</h2>'], //Test RED 
        ['loc20', 38.208, -104.574, 1, '<h2>place 8</h2>'], //Test GRN
    ];
    //alternate btw 3 different colored markers for this..
    var icons = [
        'http://maps.google.com/mapfiles/kml/paddle/red-circle.png',
        'http://maps.google.com/mapfiles/kml/paddle/ylw-circle.png',
        'http://maps.google.com/mapfiles/kml/paddle/grn-circle.png',
    ];
    // center map in middle of Nebraska
    var mapCenter = new google.maps.LatLng(40.658014, -99.439275);
    //create the map
    function createMap() {
        map = new google.maps.Map(document.getElementById("map"), {
            center: mapCenter,
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.HYBRID,
            zoomControl: true,
            streetViewControl: true,
            zoomControlOptions: {
                style: google.maps.ZoomControlStyle.LARGE
            }
        });
        //create a global infowindow to show content
        //set a maxwidth of 300 pixel
        infoWindow = new google.maps.InfoWindow({
            maxWidth: 300,
            map: map
        });
    }
    function initMarkers() {
        for (var i=0; i<places.length; i++) {
            var place=places[i];
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(place[1], place[2],place[3]),
                map: map,
                //set icon, category as icons index 
                //outcomment this line if you just want to show the defuult icon
                icon : icons[place[3]],
                //add data from places to the marker
                title : place[0],
                category: place[3],
                content: place[4]
            });
            //add the marker to the markersArray, used to hide/show markers
            markersArray.push(marker);
            //create a click event that shows the infowindow when a marker is clicked
            //the infowindow get latlng and content from the marker
            google.maps.event.addListener(marker, 'click', function() {
                infoWindow.setPosition(this.position);
                infoWindow.setContent(this.content);
                infoWindow.open(map);
            });
        }
    }
    //show / hide markers based on category
    //if category is 0, show all markers
    function showMarkersByCategory(category) {
        for (var i=0; i<markersArray.length; i++) {
            if ((category==0) || (markersArray[i].category==category)) {
                markersArray[i].setVisible(true);
            } else {
                markersArray[i].setVisible(false);
            }
        }
    }
    function initialize() {
        createMap();
        initMarkers();
        //init the select box where you show/hide the markers per category
        var checkbox=document.getElementById('checkbox');
        checkbox.onclick = function() {
            var category = this.value;
            showMarkersByCategory(category);
        }
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    </script>
</head> 
    <body>
    <div id="map" style="width:100%;height:800px;float:left;clear:none;"></div>
    <div style="position: absolute; left: 5px; top: 20px; padding: 10px 10px 10px 10px;">   
            <br>
            <form id="checkbox">
            <input type="checkbox" name="" value="0">All Groups&nbsp&nbsp
            <input type="checkbox" name="Group 1" value="1">Group 1
            <input type="checkbox" name="Group 2" value="2">Group 2
            <input type="checkbox" name="Group 3" value="3">Group 3
            <input type="checkbox" name="Group 4" value="4">Group 4
            <input type="checkbox" name="Group 5" value="5">Red
            <input type="checkbox" name="Group 6" value="6">Yellow
            <input type="checkbox" name="Group 7" value="7">Green
            </form>
    </div>
    </body>
</html>

您正在尝试传递表单的值,而不是复选框。

另外,我相信您今后也会面临其他问题,但这应该可以让您的代码"正常工作":

checkbox.onclick = function() {
    var checkedBoxes = $('#checkbox > input:checkbox:checked');
    for (var i = 0; i < checkedBoxes.length; i++){
        var category = checkedBoxes[i].value;
        showMarkersByCategory(category);
    }
}

我看到您遇到的其他问题是,当选中多个复选框时,您将只能从循环中最后一个复选框的值中获取标记。

您可能想尝试一下:

function showMarkersByCategory(category) {
    for (var i=0; i<markersArray.length; i++) {
        if ((category==0) || (markersArray[i].category==category)) {
            markersArray[i].setVisible(true);
        }
    }
}
function initialize() {
    createMap();
    initMarkers();
    //init the select box where you show/hide the markers per category
    var checkbox=document.getElementById('checkbox');
    checkbox.onclick = function() {
        for (var i=0; i<markersArray.length; i++) {
            markersArray[i].setVisible(false);
        }
        var checkedBoxes = $('#checkbox > input:checkbox:checked');
        for (var i = 0; i < checkedBoxes.length; i++){
            var category = checkedBoxes[i].value;
            showMarkersByCategory(category);
        }
    }
}

这是一个"快速修复",代码可以清理,但这应该让你朝着正确的方向前进。