Google map API displaying marker location in an infowindow

I have a javascript array of locations (jarray), which I am looping over and adding a marker of each location onto a google map. Along with those markers I would like to add an infowindow displaying the marker's location. The code below displays the same latitude and longitude location in each of marker's infowindows. I would like for each marker to have its own address in its infowindow. I have also tried setting the content of the infowindow to results[0].geometry.location, address, and jarray[i] all to no success. Any ideas are greatly appreciated.

    for(var i = 0; i < jarray.length; i++){
                            geocoder.geocode({'address': jarray[i]}, 
                            function(results, status) {
                              if (status == google.maps.GeocoderStatus.OK) {
                                if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {

                                  var marker = new google.maps.Marker({
                                    position: results[0].geometry.location,
                                    map: map,
                                    title: "Marker",
                                    icon: image
                                  });

                                  var infowindow = new google.maps.InfoWindow({
                                    content: "<p>"+marker.position+"</p>"
                                  });

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

I don't think a google maps api marker has a location property - you can use position - the method is marker.getPosition()

https://developers.google.com/maps/documentation/javascript/reference?hl=en#Marker

You can then use that position to an address by usign the geocoding (if you need that)

https://developers.google.com/maps/documentation/geocoding/intro


insted of marker.location, use the jarray location itself. You can adapt this code to suit you since I dont know the contents of your jarray.

 var infowindow = new google.maps.InfoWindow({
   infowindow.setContent(jarray[i]['lat'] +" " + jarray[i]['lat']);
   infowindow.open(map, marker);
                                  });
链接地址: http://www.djcxy.com/p/81552.html

上一篇: Google距离矩阵API替代品?

下一篇: Google地图API在infowindow中显示标记位置