简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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