簡體   English   中英

Google Maps API-圍繞標記切換圈子嗎?

[英]Google Maps API - Toggle Circles around Markers?

我試圖顯示一個標記數組,當單擊該標記時,將顯示一個信息窗口以及一個半徑為半徑的圓,其大小也是該數組的一部分。

我已經盡力顯示了標記,信息框和圓圈,但正在努力切換圓圈。

如果我想讓所有圈子開始或消失,我還沒有下定決心,我想這取決於我最終有多少...

這是我到目前為止的內容:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>Google Maps API Tests</title>
<style type="text/css">
html {font-family:Tahoma,verdana,sans-serif; font-size:0.75em;}
p {font-weight:bold;}
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.6&amp;sensor=false"></script>
<script type="text/javascript">
//<![CDATA[
  function initialize() {

    var myLatlng = new google.maps.LatLng(36.637484, -121.51057);
    var myOptions = {
      zoom: 6,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      streetViewControl: true
    }

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    setMarkers(map, sites);
    infowindow = new google.maps.InfoWindow({
            content: "loading..."
        });

  }


    var sites = [
        ['Google', 37.422117, -122.083856, 'http://www.google.com/', 402336],
        ['Yahoo', 34.030194, -118.47479, 'http://www.yahoo.com/', 201168]
    ];



    function setMarkers(map, markers) {

        for (var i = 0; i < markers.length; i++) {
            var sites = markers[i];
            var siteLatLng = new google.maps.LatLng(sites[1], sites[2]);
            var marker = new google.maps.Marker({
                position: siteLatLng,
                map: map,
                title: sites[0],
                html: '<p>'+sites[0]+'</p><a href="'+sites[3]+'">Web Link</a>'
            });

            var circle = new google.maps.Circle({
                map: map,
                radius: sites[4],
                fillColor: '#12a3eb',
                fillOpacity: 0.15,
                strokeColor: '#0177b3'
            });

            circle.bindTo('center', marker, 'position'); 


            google.maps.event.addListener(marker, "click", function () {
                infowindow.setContent(this.html);
                infowindow.open(map, this);
            });
        }
    }
//]]>
</script>
</head>
<body style="margin:0px; padding:0px;" onload="initialize()">
  <div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>

任何幫助將非常感激!

setMap(null)將刪除任何Google Map控件的顯示。 要從地圖上刪除圓圈,只需使用

   circle.setMap(null)

並再次以不同的半徑重新渲染圓

  cirlce.setMap(map);
  circle.set('radius', new_radius_value * 1000);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM