我正在使用 Google Maps Geocoding 在我的 JSP 页面上的表格行中搜索和显示地址。该地址是从会话密钥中检索的。每行都有一个地址和一个按钮,但目前该按钮仅适用于第一行中的地址。我对此并不陌生,因此如果术语不正确,我深表歉意。
会话密钥代码:
<tbody>
<c:forEach items="${SKALLUSERS}" var="tempuser">
<tr>
<td scope="row"><c:out value="${tempuser.getId()}"/></td>
<td>${tempuser.id}</td>
<td>${tempuser.email}</td>
<td>${tempuser.password}</td>
<td>${tempuser.firstName}</td>
<td>${tempuser.lastName}</td>
<td>${tempuser.userType}</td>
<td><input id="address" type="textbox"
value="${tempuser.address}"></td>
<td><a href="updateUser.jsp">Update</a></td>
<td><input id="submit" type="button" value="Geocode"></td>
</tr>
</c:forEach>
</tbody>
谷歌地图地理编码
<div id="floating-panel"></div>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map
(
document.getElementById('map'),
{
zoom: 8,
center: {lat: -34.397, lng: 150.644}
}
);
var geocoder = new google.maps.Geocoder();
document.getElementById('submit').addEventListener
(
'click', function()
{
geocodeAddress(geocoder, map);
}
);
}
function geocodeAddress(geocoder, resultsMap)
{
var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status)
{
if (status === 'OK')
{
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker
({
map: resultsMap,
position: results[0].geometry.location
});
}
else
{
alert('Geocode was not successful for the following reason: + status);
}
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCH2nCSs1XR37Q6HdyUpZZfgP5_uMsi-vA&callback=initMap">
</script>