我试图将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();
};
你没有提供完整的代码 –