代码之家  ›  专栏  ›  技术社区  ›  Jared Henderson

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

  •  -1
  • Jared Henderson  · 技术社区  · 7 年前

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

    我似乎不知道我错过了什么

    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 回复  |  直到 7 年前
        1
  •  0
  •   Emmanuel Delay    7 年前

    您缺少一个信息窗口。

    应该是这样的

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