谷歌地图信息窗口位置

Google Maps Infowindow Position

本文关键字:位置 窗口 信息窗 信息 谷歌地图      更新时间:2023-09-26

我有一个问题,这段代码,当我点击一个标记,我希望信息窗口在该标记的位置打开。

当我点击每个标记时,它们都在相同的位置打开。

代码:

App.map = function (data, cb) {
  App.getData(App.config.LFMurl(), function (data) {
    App.processData(data, function(arr){      
    var mapOptions = {
      zoom: 12,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions)
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude)
      var marker
      $.each(arr, function (index, item) {            
        marker = new google.maps.Marker({
          position: new google.maps.LatLng(item.lat, item.long),
          map: map,              
          animation: google.maps.Animation.DROP        
        })
        google.maps.event.addListener(marker, 'click', function(){                            
          var infowindow = new google.maps.InfoWindow({                
            map: map,
            position: new google.maps.LatLng(item.lat, item.long),                                
            content: item.title
          })
          infowindow.open(map, this)
          console.log(item.artist)              
        })
      }) 
      map.setCenter(pos)
    }, function() {
      handleNoGeolocation(true)          
    })
  } else {      
    handleNoGeolocation(false) // Browser som inte stödjer geolocation
  }
})
})
}

get it work…

底部我这样做了:

infowindow.open(map, this)

将'this'改为'marker'