Google maps 填充并清除谷歌地图

Google maps 填充并清除谷歌地图,google-maps,google-maps-api-3,Google Maps,Google Maps Api 3,我已经按照谷歌地图API v3教程创建了一个基本地图。我试图弄清楚如何首先声明地图,然后允许我根据人们选择的类别添加点。我还试图找出如何清除每个选择之间的映射。为类别设置一个表单字段。更改该选项将触发一个JS函数,该函数将清除映射中的所有标记,然后为该类别添加一些标记。如果你用你迄今为止尝试过的代码来展开你的问题,我可以展开这个答案 要清除标记,请使用marker.setMapnull;在每一个上都没有API函数可以为您清除它们 您可能还需要一个JS数组或类别关联数组,其中每个标记的数据至少为l

我已经按照谷歌地图API v3教程创建了一个基本地图。我试图弄清楚如何首先声明地图,然后允许我根据人们选择的类别添加点。我还试图找出如何清除每个选择之间的映射。

为类别设置一个表单字段。更改该选项将触发一个JS函数,该函数将清除映射中的所有标记,然后为该类别添加一些标记。如果你用你迄今为止尝试过的代码来展开你的问题,我可以展开这个答案

要清除标记,请使用marker.setMapnull;在每一个上都没有API函数可以为您清除它们

您可能还需要一个JS数组或类别关联数组,其中每个标记的数据至少为lat/lng值,这将使您的函数变得简单

好的,这里有一个有效的解决方案,为了简洁,我减少了标记的数量

<!DOCTYPE html>
<html>
<head>
<title>http://stackoverflow.com/questions/15550211/populate-clear-google-map/15550488?noredirect=1#comment22037226_15550488</title>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map-canvas { width:600px; height:500px }
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
    var MSU;
    var map;
    var BusRouteButton = document.getElementById('Add-Bus-Route');

    var arrRoutes = [
        {
            name : "Mustang",
            color : "#660000",
            route : [
                 new google.maps.LatLng(33.87528, -98.51848),
                 new google.maps.LatLng(33.87045, -98.51856),
                 new google.maps.LatLng(33.86926, -98.51844),
                 new google.maps.LatLng(33.86666, -98.51833),
                 new google.maps.LatLng(33.86503, -98.51849),
                 new google.maps.LatLng(33.87529, -98.52366),
                 new google.maps.LatLng(33.87524, -98.52195),
                 new google.maps.LatLng(33.87521, -98.52026),
                 new google.maps.LatLng(33.87528, -98.51848)
            ],
            stops : [
                [33.87262, -98.51856],
                [33.86503, -98.51849],
                 [33.87529, -98.52366]
            ],
            markers : []
        },
        {
            name : "Foobar",
            color : "#000066",
            route : [
                 new google.maps.LatLng(33.87528, -98.51848),
                 new google.maps.LatLng(33.87524, -98.52195),
                 new google.maps.LatLng(33.86926, -98.51844),
                 new google.maps.LatLng(33.87184, -98.52376)
            ],
            stops : [
                [33.87528, -98.51848],
                [33.87184, -98.52376]
            ],
            markers : []
        }
    ];

    function initialize() {
        MSU = new google.maps.LatLng(33.87356, -98.52130);
        var mapOptions = {
            center: MSU,
            zoom: 16,
            zoomControl: true,
            mapTypeControl: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

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

        // lets add the data from the array to the page!  I'm going to use jQuery's append for this, you could also use document.write if you aren't using it
        for (var i = 0; i < arrRoutes.length; i++) {
            $('#category').append(
                '<option value="' + i + '">' + arrRoutes[i].name + '</option>'
            );
        }
    }


    function BusRouteMarkers() {
            // what's the currently selected category?
            var intRoute = $('#category').val();

            for (var i = 0; i < arrRoutes.length; i++) {
                // better get rid of any previous polylines first
                if (arrRoutes[i].polyline) {
                    arrRoutes[i].polyline.setMap(null);
                }

                // ditto for any previous markers
                for (var j = 0; j < arrRoutes[i].markers.length; j++) {         
                    arrRoutes[i].markers[j].setMap(null);
                }
            }

            // add this polyline to the array for reference later
            arrRoutes[intRoute].polyline = new google.maps.Polyline({
                 path: arrRoutes[intRoute].route,
                 strokeColor: arrRoutes[intRoute].color,
                 strokeOpacity: 1.0,
                 strokeWeight: 4,
                map: map
            });


            for (var i = 0; i < arrRoutes[intRoute].stops.length; i++) {
                arrRoutes[intRoute].markers.push(
                    new google.maps.Marker({ 
                        position: new google.maps.LatLng(
                            arrRoutes[intRoute].stops[i][0], 
                            arrRoutes[intRoute].stops[i][1]
                        ), 
                        map: map 
                    })
                );
            }
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>

</head>
<body>
<div id="map-canvas"></div>

<button id="Add-Bus-Route" onClick="BusRouteMarkers()">click me</button>

<select id="category">
</select>

</body>
</html>

谢谢你的回复。这是我到目前为止为我的js设置的。至于数组,我有一个包含所有点的json数组,这些点都属于这个类别。我正在为一个班级项目设计一条互动公交线路,我以我的大学为例。好的,我想说你想要一个大的数组用于所有的路由。使用此数组可以创建与用户选择的类别对应的多条公交线路。我会试着给你一些代码。邓肯-非常感谢你的帮助。你太棒了。我需要做什么才能创建一个只用于标记的数组?我声明了一个新的数组,只是不确定哪些项目应该进入itex。名称、颜色、路线等。。。。