为什么在我明确指定新值时,我的JSON结构不会更新?
items[0][i]['human_addressItem'] = address;
我是geo reversion纬度和经度采取人工地址,那部分工作正常,但我可以将其插入JSON,为什么?
这是运行的示例:
CODE:
HTML:
<script src="http://code.jquery.com/jquery-2.0.2.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<div class="container-fluid">
<!-- Tables -->
<section id="tables">
<table>
<thead>
<tr>
<th>[name]</th>
<th>[txtLat]</th>
<th>[txtLon]</th>
<th>[human_address]</th>
</tr>
</thead>
<tbody id="items">
<script id="tmpItems" type="text/html">
<tr>
<td><input value="${name}" type="text" name="[name]"></td>
<td><input value="${Latitude}" type="text" name="[txtLat]"></td>
<td><input value="${Longitude}" type="text" name="[txtLon]"></td>
<td><input value="${human_addressItem}" type="text" name="[human_address]"></td>
</tr>
</script>
</tbody>
</table>
</section>
</div>
JAVASCRIPT:
//GEOCORDER
geocoder = new google.maps.Geocoder();
items = [
[{
"Longitude": -73.929489,
"Latitude": 40.76079,
"name": "Electronics"
}, {
"Longitude": -73.761727,
"Latitude": 40.695817,
"name": "02 Dodge (PICS)"
}], {
"active": 0
}];
for (var i = 0; i < items[0].length; i++) {
var address = "";
var lat = parseFloat(items[0][i]['Latitude']);
var lng = parseFloat(items[0][i]['Longitude']);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({
'latLng': latlng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var address = results[1].formatted_address;
//alert(address);
console.log(address);
} else {
alert('No results found in: ' + items[0][i]['name']);
}
} else {
alert('Geocoder failed due to: ' + status + " in: " + items[0][i]['name']);
}
});
items[0][i]['human_addressItem'] = address;
}
var o = items;
$("#tmpItems").tmpl(items[0]).appendTo("#items");
答案 0 :(得分:1)
您需要将所有响应后代码放在回调函数中:
geocoder.geocode({'latLng': latlng},
function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var address = results[1].formatted_address;
alert (address);
items[0][i]['human_addressItem'] = address;
var o = items;
//items[i]['human_addressItem']) now holds the address
alert (items[i]['human_addressItem']);
} else {
alert('No results found in: ' + items[0][i]['name']);
}
} else {
alert('Geocoder failed due to: ' + status + " in: " + items[0][i]['name']);
}
});
//any code here will run before your function above
}
根据上面的评论,这是关于AJAX,因此是Async。 geocoder.geocode函数进行AJAX调用。
答案 1 :(得分:1)
代码中的一些错误:
1)var address =“”; &安培; var address = results [1] .formatted_address;不同范围的地址变量。
2)Asyc反应。尝试在回调中追加(“#items”)
更新了代码。签入JSFiddle
//GEOCORDER
geocoder = new google.maps.Geocoder();
items = [
[{
"Longitude": -73.929489,
"Latitude": 40.76079,
"name": "Electronics"
}, {
"Longitude": -73.761727,
"Latitude": 40.695817,
"name": "02 Dodge (PICS)"
}], {
"active": 0
}];
function updateAddress(i) {
geocoder.geocode({
'latLng': latlng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var address = results[1].formatted_address;
//alert(address);
console.log(address);
items[0][i]['human_addressItem'] = address;
var o = items;
$("#tmpItems").tmpl(items[0][i]).appendTo("#items");
} else {
alert('No results found in: ' + items[0][i]['name']);
}
} else {
alert('Geocoder failed due to: ' + status + " in: " + items[0][i]['name']);
}
});
}
for (var i = 0; i < items[0].length; i++) {
var address = "";
var lat = parseFloat(items[0][i]['Latitude']);
var lng = parseFloat(items[0][i]['Longitude']);
var latlng = new google.maps.LatLng(lat, lng);
updateAddress(i);
}