信息窗口未显示反向地理编码地址Google Maps V3

时间:2013-07-27 18:18:03

标签: google-maps reverse-geocoding

我正在编写一个脚本,允许您根据鼠标点击的位置放置标记。我明白了。但是,当我通过反向地理编码的lat和lng时,我似乎无法弹出信息窗口。有人能帮助我吗?谢谢!

这是我的代码:

var geocoder;

function initialize() 
{

  geocoder = new google.maps.Geocoder();
  var event = google.maps.event.addListener;

  // intializing and creating the map.
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(-25.363882, 131.044922),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById('map'),
      mapOptions);

   event(map,'click',function(e){
    var marker = placeMarker_and_attachMessage(e.latLng,map,count);
  });

} // end of initalize.

/* place down marker based on where you click the mouse over a certain area on the map.
   An infowindow should appear with the location of your marker.
*/

function placeMarker_and_attachMessage(position,map,num) 
{
    var event = google.maps.event.addListener;

    var marker = new google.maps.Marker({
        position: position,
        map: map
    }); 

        var infowindow = new google.maps.InfoWindow({
            content: info_text(position)
        });

        event(marker,'mouseover',function(){
            infowindow.open(map,this);
        });

        event(marker,'mouseout',function(){
            infowindow.close();
        });
} // end of function.

// Takes in the position passed by the 'placeMarker_and_attachMessage' function and returns a human readable string 
function info_text(position)
{
    var location = position;
    var latlngStr = location.split(',',2);
    var lat = parseFloat(latlngStr[0]);
    var lng = parseFloat(latlngStr[1]);
    var latlng = new google.maps.LatLng(lat,lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) 
    {
        if (status == google.maps.GeocoderStatus.OK)
        {
            return results;
        }
        else
        {
            alert('could not pinpoint location');
        }

    });
}

google.maps.event.addDomListener(window, 'load', initialize);

1 个答案:

答案 0 :(得分:0)

在您发布的代码中,count未在

中定义
var marker = placeMarker_and_attachMessage(e.latLng,map,count);