使用 Google API 我在 div 中生成了国家名称。我无法在服务器端检索国家名称。我无法在字符串中检索国家名称请解决这个问题。
<script src="http://maps.google.com/maps?hl=it&file=api&v=2&sensor=true&" type="text/javascript"></script>
<script type="text/javascript">
var map;
var geocoder;
function addAddressToMap(response) {
if (!response || response.Status.code != 200) {
alert("Sorry, we were unable to geocode that address");
}
else {
place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
document.getElementById('address').innerHTML = place.address;
var str = place.address
var a = str.lastIndexOf(",")
a = a + 1
var country = str.substring(a)
document.getElementById('country').innerHTML = country
}
}
function searchGeolocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
geocoder = new GClientGeocoder();
document.getElementById('latitude').innerHTML = position.coords.latitude;
document.getElementById('longitude').innerHTML = position.coords.longitude;
coordinates = position.coords.latitude + "," + position.coords.longitude;
geocoder.getLocations(coordinates, addAddressToMap);
});
} else {
document.getElementById('latitude').innerHTML = "Unknown";
document.getElementById('longitude').innerHTML = "Unknown";
document.getElementById('address').innerHTML = "Unknown";
alert("I'm sorry, but geolocation services are not supported by your browser.");
}
}
</script>
<div id="latitude">loading...</div>
<script type="text/javascript">
document.getElementById('latitude').style.display = 'none';
</script>
<div id="longitude">loading...</div>
<script type="text/javascript">
document.getElementById('longitude').style.display = 'none';
</script>
<div id="address">loading...</div>
<script type="text/javascript">
document.getElementById('address').style.display = 'none';
</script>
<div id="country" runat ="server" >loading...<br />
<script type="text/javascript">
searchGeolocation();
</script>