2011-06-01 142 views
0

我創建了一個嵌入Google map的網站,使用顯示我們的4個辦公室的api V3,然後輸入您的郵政編碼並檢查您與辦公室之間的距離,這是有效的!Google Maps API V3 - Javascript問題

我想現在創建一個徑向搜索,我發現如何在網上做到這一點,這是偉大的,並在最後的障礙,這是我需要你的幫助,如果可能的話。

我有以下幾點:

 geocoder = new google.maps.Geocoder(); // creating a new geocode object 

    // getting the two address values 
    address1 = document.getElementById("addresses1").value; 
    distance1 = document.getElementById("distance").value; 

    // finding out the coordinates 
    if (geocoder) 
    { 
     geocoder.geocode({ 'address': address1}, function(results, status) 
     { 
      if (status == google.maps.GeocoderStatus.OK) 
      { 
       //location of first address (latitude + longitude) 
       location1 = results[0].geometry.location; 

       // Location Marker 
       var marker = new google.maps.Marker({ 
        map: map, 
        position: location1, 
        title: 'Random text' 
       }); 
       // Add circle overlay and bind to marker 
       var circle = new google.maps.Circle({ 
        map: map, 
        radius: distance1, 
        strokeWeight: 0, 
        fillColor: '#AA0000' 
       }); 
       circle.bindTo('center', marker, 'position'); 


      } else 
      { 
       alert("Geocode was not successful for the following reason: " + status); 
      } 
     }); 
    } 

我設法得到的一切,除了$distance1工作。應該發生的事情是,用戶選擇與表單的距離:例如,當他們點擊提交時,10英里(Google地圖以米爲單位),繪製一個半徑爲10英里的圓圈。

由於代碼的立場,點繪製,但圓圈不出現。

例如,如果我將distance1 = document.getElementById("distance").value;替換爲distance1 = 16999;例如,那麼會出現一個圓並且它完美地工作。

對我來說,這意味着distance1從窗體中拉出時內容有問題。我已經完成了NaN測試,只是爲了確保它報告內容是一個數字。

我的問題是(以非常漫長的方式 - 對此感到抱歉)你有什麼想法如何讓distance1從表單中獲得價值。

// Add circle overlay and bind to marker var circle = new google.maps.Circle({ map: map, radius: distance1, strokeWeight: 0, fillColor: '#AA0000' });

有問題的部分是radius: distance1

+1

我想像這是因爲半徑期待一個數字,但距離1是一個字符串。你有沒有嘗試distance1 = parseInt(document.getElementById(「distance」).value); – Jasoon 2011-06-01 16:52:17

回答

1

可能有助於確保它是一個數字,而不是字符串。

distance1 = parseFloat(document.getElementById("distance").value); 

(parseInt函數可能會很好地工作爲好,因爲它是在米和米分數將沒有太大的差別)