2015-10-06 34 views
0

我試圖將lat和long添加到提供的文本框中。 似乎有一個問題,因爲當我將地址硬編碼到變量中時,它會返回lat和long,但只要我通過文本框值檢索地址,它就不起作用。怎麼來的?爲什麼Javascript不通過id將值傳遞給文本框MVC?

這裏是代碼的觀點:

<div class="form-group"> 
    @Html.LabelFor(model => model.StreetNr, new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(model => model.StreetNr) 
     @Html.ValidationMessageFor(model => model.StreetNr) 
    </div> 
</div> 

<div class="form-group"> 
    @Html.LabelFor(model => model.StreetName, new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(model => model.StreetName) 
     @Html.ValidationMessageFor(model => model.StreetName) 
    </div> 
</div> 

<div class="form-group"> 
    @Html.LabelFor(model => model.Suburb, new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(model => model.Suburb) 
     @Html.ValidationMessageFor(model => model.Suburb) 
    </div> 
</div> 

<div class="form-group"> 
    @Html.LabelFor(model => model.Town, new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(model => model.Town) 
     @Html.ValidationMessageFor(model => model.Town) 
    </div> 
</div> 




    create_click = function() { 

     var geocoder = new google.maps.Geocoder(); 
     //var address = 'summerstrand'; 
     var address = '102 Prince Alfred North End Port Elizabeth' 
     //var address = document.getElementById('StreetNr').value + ' ' + document.getElementById('StreetName').value + ' ' + document.getElementById('Suburb') + ' ' + document.getElementById('Town'); 

     geocoder.geocode({ 'address': address }, function (results, status) { 

      if (status == google.maps.GeocoderStatus.OK) { 
       var latitude = results[0].geometry.location.lat(); 
       var longitude = results[0].geometry.location.lng(); 
       //alert('Lat' + " " + latitude + " " + 'long' + " " + longitude); 
       document.getElementById('EventLat').value = latitude; 
       document.getElementById('EventLong').value = longitude; 
      } 
      else { 
       alert("Location not found, Please enter a valid address"); 
       document.getElementById('StreetNr').value = " "; 
       document.getElementById('StreetName').value = " "; 
       document.getElementById('Suburb').value = " "; 
       document.getElementById('Town').value = " "; 
       document.getElementById('ZipCode').value = " "; 
      } 
     }); 

     document.getElementById('Get_Location').click(); 
    }; 
+0

你沒有提供完整的代碼 –

回答

0

這段簡單的代碼解決了這個問題。

它返回的錯誤是:值「[對象HTMLInputElement]」不是有效

這裏是解決我的問題:

 var address1 = document.getElementById("StreetNr").value + " " + document.getElementById("StreetName").value; 
     var address2 = document.getElementById("Suburb").value + " " + document.getElementById("Town").value; 

     var address = address1 + " " + address2; 
3

試試這個:

var geocoder = new google.maps.Geocoder(); 
     //var address = 'summerstrand'; 
     var address = document.getElementById('StreetNr').value + ' ' + document.getElementById('StreetName').value + ' ' + document.getElementById('Suburb').value + ' ' + document.getElementById('Town').value; 

     geocoder.geocode({ 'address': address }, function (results, status) { 

      if (status == google.maps.GeocoderStatus.OK) { 
       var latitude = results[0].geometry.location.lat(); 
       var longitude = results[0].geometry.location.lng(); 
       //alert('Lat' + " " + latitude + " " + 'long' + " " + longitude); 
       document.getElementById('EventLat').value = latitude; 
       document.getElementById('EventLong').value = longitude; 
      } 
      else { 
       alert("Location not found, Please enter a valid address"); 
       document.getElementById('StreetNr').value = " "; 
       document.getElementById('StreetName').value = " "; 
       document.getElementById('Suburb').value = " "; 
       document.getElementById('Town').value = " "; 
       document.getElementById('ZipCode').value = " "; 
      } 
     }); 

你忘了給.value的城郊和城市 也請檢查你從編輯器中正確的地址使用控制檯 Console.log(address);

+0

謝謝,這解決了問題。 – JohnWayne