google-maps - Google Maps API V3 - 通过点击事件更改圆半径

标签 google-maps google-maps-api-3 dom-events mouseclick-event

我使用下面的代码通过点击事件显示圆圈,半径为 60.000m。

<html>
<head>
<div id="map"></div>
<style media="screen, projection" type="text/css">

map {
        width: 800px;
        height: 600px;
      }

</style>
</head>
<body>

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">

</script><script type="text/javascript">

        function initialize()   
        {
            var mapCenter = new google.maps.LatLng(40, -74);

            var map = new google.maps.Map(document.getElementById('map'), {
                'zoom': 7,
                'center': mapCenter,
                'mapTypeId': google.maps.MapTypeId.ROADMAP
            });

            // Add click event listener

            google.maps.event.addListener(map, "click", function (e) {

                var image = new google.maps.MarkerImage(  
                         'image/data/ConstBuilder/marker.png',
                          new google.maps.Size(40, 35),
                          new google.maps.Point(0, 0),
                          new google.maps.Point(20, 30)
                        );

                var shadow = new google.maps.MarkerImage(
                         'image/data/ConstBuilder/shadow.png',   
                          new google.maps.Size(62, 35),    
                          new google.maps.Point(0, 0),    
                          new google.maps.Point(0, 35)
                        );

                var marker = new google.maps.Marker({
                      draggable: true,
                      raiseOnDrag: false,
                      icon: image,
                      shadow: shadow,
                      map: map,
                      position: e.latLng
                });

                var circle = new google.maps.Circle({
                    map: map,
                    radius: 60000,
                    fillColor: '#AA0000'
                });

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

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

现在我想再添加一项功能 - 可以更改我的 Click 事件(显示圆圈)的半径值的单选按钮。 我知道我需要像新事件一样使用 getRadius() 方法,我尝试实现下面的代码但没有成功。

google.maps.event.addDomListener(

document.getElementById('circle_radius'), 'change', function() {

circle.setRadius(document.getElementById('circle_radius').value)

});

谁能帮我找到解决方案?

最佳答案

尝试用这个替换您的代码,更改部分在 body 标记之后。

<html>
<head>
<div id="map"></div>
<style media="screen, projection" type="text/css">

#map {
        width: 800px;
        height: 400px;
      }

</style>
</head>
<body>
<div>
30.000m <input type="radio" name="radiusBtns" id="radioBtn1" onclick="calcRadius(30000);" value="30" /><br />
60.000m<input type="radio" name="radiusBtns"  id="radioBtn2" onclick="calcRadius(60000);" value="60" /><br />
90.000m<input type="radio" name="radiusBtns"  id="radioBtn3" onclick="calcRadius(90000);" value="90" />
</div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">

</script><script type="text/javascript">
        var circle, map, pre_radius;
        function initialize()   
        {
            pre_radius = '60000';

            var mapCenter = new google.maps.LatLng(40, -74);

            map = new google.maps.Map(document.getElementById('map'), {
                'zoom': 7,
                'center': mapCenter,
                'mapTypeId': google.maps.MapTypeId.ROADMAP
            });

            // Add click event listenecal
            calcRadius(60000);

        };

        function calcRadius(radiusVal)
        {
            //console.log(document.getElementById("#radioBtn1").value);
            pre_radius = radiusVal;
            google.maps.event.addListener(map, "click", function (e) {

                var image = new google.maps.MarkerImage(  
                         'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png',
                          new google.maps.Size(40, 35),
                          new google.maps.Point(0, 0),
                          new google.maps.Point(20, 30)
                        );

                var shadow = new google.maps.MarkerImage(
                         'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png',   
                          new google.maps.Size(62, 35),    
                          new google.maps.Point(0, 0),    
                          new google.maps.Point(0, 35)
                        );

                var marker = new google.maps.Marker({
                      draggable: true,
                      raiseOnDrag: false,
                      icon: image,
                      map: map,
                      position: e.latLng
                });

                circle = new google.maps.Circle({
                    map: map,
                    radius: pre_radius,
                    fillColor: '#AA0000'
                });

                console.log(circle);

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

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

关于google-maps - Google Maps API V3 - 通过点击事件更改圆半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15111403/

相关文章:

javascript - 每个页面/用户的 Google map API 请求。如何最好地减少请求?

javascript - 为什么该脚本的位置会改变网页的行为?

Javascript document.form.submit 不工作

java - Android 上谷歌地图的动态 API key

javascript - 将 Google map 集中在数据库中的点上?

android - 更新 map 时 Android 谷歌地图上的 NullPointer 异常

html - 谷歌地图未通过 ajax 加载的内容显示在页面中

javascript - 如何在 google map api v3 javascript 中单击 map 时获取纬度、经度?

javascript - 为什么我的输入按钮的 onclick 事件不起作用?

javascript - Gmaps4Rails - 入门