2015-10-06 40 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

相关问题