2013-04-16 44 views
0

我正在使用以下代码在地图上将矩形绘制到随机拉特。 &长。基于点击查找按钮时的城市名称。问题是如果你没有输入不同的城市名称,点击查找按钮,程序将在城市原始经纬度上绘制第二个矩形。 &长。我要么需要清除地图并重新绘制矩形或者不太确定的东西,所以不能绘制第二个矩形。这是我正在使用的代码。单击按钮后如何停止绘制第二个矩形的Google地图

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."); 
      } 
     }); 

    } 

非常感谢。

回答

相关问题