试试这个从 lat, lng 获取地址。单击标记以显示地址。它是做你的工作的另一种方式。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing</title>
<script src="scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyA7IZt-36CgqSGDFK8pChUdQXFyKIhpMBY&sensor=true" type="text/javascript"></script>
<script type="text/javascript">
var map;
var geocoder;
var marker;
var latlng;
var infowindow;
$(document).ready(function() {
});
function initialize() {
var mapOptions = {
//center: new google.maps.LatLng(40.4230, 98.7372), // Coimbatore = (11.0168445, 76.9558321)
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
geocoder = new google.maps.Geocoder();
infowindow = new google.maps.InfoWindow();
var latlngStr = $('#address').val().split(",");
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
latlng = new google.maps.LatLng(lat, lng);
marker = new google.maps.Marker({
position: latlng,
map: map,
draggable: true
});
map.setCenter(latlng);
google.maps.event.addListener(marker, 'click', function(event) {
geocoder.geocode({ 'latLng': event.latLng }, function(results, status) {
infowindow.setContent(results[0].formatted_address);
});
infowindow.setContent(this.html);
infowindow.setPosition(event.latLng);
infowindow.open(map, this);
});
}
</script>
</head>
<body>
<label>
Lat, Lng:
</label>
<input id="address" type="text" placeholder="10.9435131, 76.9383790" />
<input type="button" value="Go" onclick="initialize()" />
<br />
<br />
<div id="map-canvas" style="width: 800px; height: 500px">
</div>
</body>
</html>