代码改变世界

一个简单的marker和infowindow

2012-09-26 20:26  youxin  阅读(667)  评论(0编辑  收藏  举报

以下代码在map上创建了一个Marker,然后在Marker上注册了事件监听器,点击后显示了InfoWindow.

// Creating an option object for the map
var options = {  
    zoom: 7,
    center: new google.maps.LatLng(56.83, 15.16),
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Initializing the map
var map = new google.maps.Map(document.getElementById('map'), options); 
// Creating a marker
var marker = new google.maps.Marker({
  position: new google.maps.LatLng(56.8848, 14.7730), 
  map: map,
  title: 'My workplace'
});

// Creating an InfoWindow object
var infowindow = new google.maps.InfoWindow({
  content: 'Hello world'
});

google.maps.event.addListener(marker, 'click', function() {
  infowindow.open(map, marker);
});

via:http://www.svennerberg.com/2009/09/google-maps-api-3-infowindows/