在Google Maps JavaScript API中获取位置地址 [英] Getting location address in Google Maps JavaScript API

查看:141
本文介绍了在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>


这篇关于在Google Maps JavaScript API中获取位置地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆