2012-07-26 46 views
0

我想让这个有人点击地图,并且一个infoWindow弹出告诉他们latlng。现在,试图找出如何让infoWindow出现。我在这里用标记发现了一个类似的问题,并且编辑了一些函数,但是我错过了一些我很确定很简单的东西,但我不能指出。这里的代码:InfoWindow not Displaying

 function InfoWindow(location) { 
      if (marker) { 
      marker.setPosition(location); 
      } else { 
      marker = new google.maps.infoWindow({ 
       position: location, 
       content: "sam" 
      }); 
      } 
     } 

     google.maps.event.addListener(map, 'click', function(event) { 
      InfoWindow(event.latLng); 
     }); 
       } 

帮助非常感谢! 谢谢

+0

见我的回答你的其他职位 http://stackoverflow.com/questions/11671571/google-:

粘贴此到地址栏,然后点击地图上的测试上this map maps-api-v3-info-bubble/11671897#11671897 – puckhead 2012-07-26 14:54:42

回答

0

我使用的是InfoBubble,它比InfoWindow好看,它也可以更容易地处理。尝试一下。

0

事件是一个经纬度

google.maps.event.addListener(map, 'click', function(event) { 
     InfoWindow(event); 
    }); 
+0

谢谢,请试试这个 编辑:这个,谢谢你的提高。尽管 – 2012-07-26 14:10:14

+0

仍然没有infowindow弹出,但您需要调用InfoWindows的开放方法 marker.open(map); – cadetill 2012-07-26 14:50:39

+1

事件不是LatLng,但它具有latLng属性。 – geocodezip 2012-07-26 15:00:03

0

你是在传递事件的经纬度(event.latLng),而不是整个事件的正确。为了帮助输出,我提供了将lat和lng分解为字符串的代码。我修正了一些其他的事情:需要在google.maps.InfoWindow中成为大写字母“I”;如果窗口已经存在,则将内容设置为新的latLng;添加了代码,最终实际打开窗口。这应该照顾一切雅

function InfoWindow(location) { 
    var lat = location.lat().toString(); 
    var lng = location.lng().toString(); 
    var new_content = "Lat: " + lat + "<br />Long: " + lng 
    if (marker) { 
    marker.setPosition(location); 
    marker.setContent(new_content); 
    } else { 
     marker = new google.maps.InfoWindow({ 
     position: location, 
     content: new_content 
    }); 
    } 
    marker.open(map); 
} 

google.maps.event.addListener(map, 'click', function(event) { 
    InfoWindow(event.latLng); 
}); 
0

Map MouseEvent具有latLng属性,

google.maps.event.addListener(map, 'click', function(event) 
{ 
    new google.maps.InfoWindow({ 
       map:map, 
       content:"coordinates:"+event.latLng.toUrlValue(), 
       position:event.latLng});}); 
} 

“地图”是你的google.maps.Map参考。

javascript:google.maps.event.addListener(map, 'click', function(event) {new google.maps.InfoWindow({map:map, content:"coordinates:"+event.latLng.toUrlValue(),position:event.latLng});});