簡體   English   中英

谷歌地圖 API 在信息窗口中顯示標記位置

[英]Google map API displaying marker location in an infowindow

我有一個 javascript 位置數組 (jarray),我正在循環並將每個位置的標記添加到谷歌地圖上。 除了這些標記,我想添加一個顯示標記位置的信息窗口。 下面的代碼在每個標記的信息窗口中顯示相同的緯度和經度位置。 我希望每個標記在其信息窗口中都有自己的地址。 我還嘗試將信息窗口的內容設置為 results[0].geometry.location、address 和 jarray[i],但都沒有成功。 任何想法都非常感謝。

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

我認為谷歌地圖 api 標記沒有位置屬性 - 您可以使用位置 - 方法是 marker.getPosition()

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

然后,您可以通過使用地理編碼(如果需要)將該位置用於地址

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

安裝了marker.location,使用jarray 位置本身。 您可以調整此代碼以適合您,因為我不知道您的 jarray 的內容。

 var infowindow = new google.maps.InfoWindow({
   infowindow.setContent(jarray[i]['lat'] +" " + jarray[i]['lat']);
   infowindow.open(map, marker);
                                  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM