我試圖將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();
};
你沒有提供完整的代碼 –