简体   繁体   English

Google Maps Infowindow职位

[英]Google Maps Infowindow Position

I have a problem with this code, when i click on a marker I want the infowindow to open up at that marker's position. 我有这个代码的问题,当我点击一个标记我希望infowindow打开该标记的位置。

When i click on every marker they all open up on the same position. 当我点击每个标记时,它们都会在同一个位置打开。

code: 码:

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
  }
})
})
}

Got it to work... 有它工作......

at the bottom i did this: 在底部,我这样做:

infowindow.open(map, this)

changed 'this' with 'marker' 用'标记'改变'this'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM