2013-04-16 64 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."); 
      } 
     }); 

    } 

非常感謝。

回答

相關問題