在Google Maps JavaScript API中获取位置地址
问题描述:
单击指向当前位置的标记时,我试图在infoWindow中获取当前位置地址.我想我需要JavaScript才能传递到infoWindow的content字段,但我无法弄清楚.
Hi,
I am trying to get the current location address in my infoWindow when I click on the marker that points to the current location. I think I need JavaScript to pass to the content field of the infoWindow but I cannot figure it out.
var infoWindow = new google.maps.InfoWindow({
content: 'You are here'
});
google.maps.event.addListener(marker, 'click', info);
function info() {
infoWindow.open(map, marker);
}
问候!
Regards!
答
尝试一下.
Try this..
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
var infowindow = new google.maps.InfoWindow();
var latlang=new google.maps.LatLng(geoip_latitude(), geoip_longitude());
function initialize()
{
var myOptions = {
center: latlang,
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), myOptions);
geocoder.geocode({ 'latLng': latlang }, function (items, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setZoom(8);
var marker = new google.maps.Marker({
position: latlang ,
map: map
});
infowindow.setContent(items[1].formatted_address);
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, this);
map.setZoom(8);
});
}
});
}
window.onload=initialize;
</script>
</head>
<body>
<div id="map" align="center" style="height: 420px; width:500px; top:0px; border: 1px solid black;"></div>
</body>
</html>