0

我正在使用以下代码在地图上将矩形绘制到随机纬度。& 长。单击查找按钮时基于城市名称。问题是如果您没有输入不同的城市名称并单击查找按钮,程序将在城市原始纬度上绘制第二个矩形。& 长。我要么需要清除地图并重新绘制矩形,要么不太确定,因此无法绘制第二个矩形。这是我正在使用的代码。

    function codeAddress() {
        var address = document.getElementById("gadres").value;

        if(address=='') {
            alert("Address can not be empty!");
            return;

        }


        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {

                map.setCenter(results[0].geometry.location);
                document.getElementById('lat').value=results[0].geometry.location.lat().toFixed(6);
                document.getElementById('lng').value=results[0].geometry.location.lng().toFixed(6);
                var latlong = "(" + results[0].geometry.location.lat().toFixed(6) + " , " +
                + results[0].geometry.location.lng().toFixed(6) + ")";

                map.setZoom(15);

                var mapcenter = map.getCenter();
                var maplat = mapcenter.lat();
                var maplng = mapcenter.lng();

                var bounds = new google.maps.LatLngBounds(
                    new google.maps.LatLng(maplat - 0.002, maplng - 0.002),
                    new google.maps.LatLng(maplat + 0.002, maplng + 0.002)
                );

                var rectangle = new google.maps.Rectangle({
                    bounds: bounds,
                    draggable:true,
                    editable: true,
                    strokeColor: '#FF0000',
                    strokeOpacity: 0.8,
                    strokeWeight: 4,
                    fillColor: '#FF0000',
                    fillOpacity: 0.30,
                });

                rectangle.setMap(map);

                google.maps.event.addListener(rectangle, 'bounds_changed', function() {
                    document.getElementById('coords').value = rectangle.getBounds();

                });


            } else {
                alert("Lat and long cannot be found.");
            }
        }); 

    } 

非常感谢所有帮助。

4

1 回答 1

0

我还没有尝试过,但也许你可以将你的边界存储到一个数组中,然后使用该数组的循环语句来检查是否存在使用 equals() 方法的相等边界 https://developers.google.com/maps /documentation/javascript/reference#LatLngBounds

于 2013-04-16T15:45:11.763 回答