将纬度和经度附加到谷歌地图中的标记

时间:2018-01-11 02:45:41

标签: google-maps geolocation

我正在尝试将lat和long附加到googlemaps中的标记,以便我可以为每个标记添加标题.....

我似乎无法弄清楚我缺少什么

function getPlaces(index, placesArray)
    {
        placesSearch.textSearch({query: placesArray[index]}, function (placesResult)
        {
            if(index === 0){
                markerArray.forEach(marker =>
                {
                    marker.setMap(null);
                });
                map.setCenter(placesResult[0].geometry.location);
            }
            if(placesResult && placesResult[0])
            {
                markerArray.push(new google.maps.Marker({
                    position: placesResult[0].geometry.location.LatLng,
                    map: map,
                    clickable: true,
                    title: "Click for more details"

                }));
            }

            if(index < placesArray.length - 2)
            {
                getPlaces(++index, placesArray);
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

你错过了一个信息窗口。

它应该是这样的

...
if(placesResult && placesResult[0])
{
    var marker = new google.maps.Marker({
        position: placesResult[0].geometry.location.LatLng,
        map: map,
        clickable: true,
        title: "Click for more details"

    });
    markerArray.push(marker);
    // attach an onClick event
    marker.addListener('click', function() {
      // first we need to know which marker was clicked on
      var index = markerArray.indexOf(this);
      var position = markerArray[index].getPosition();
      var contentString = 'marker position: ' + position.lat() + ',' + position.lng();
      var infowindow = new google.maps.InfoWindow({
        content: contentString,
        position: position,
        map: map
      });

    })

}
...