是否有一种方法可以将位置添加到带有JavaScript的Google地图中的标记标签中



是否可以将latlng添加到随机放置在标记的标签中以显示标记在哪里?还考虑了Infowdow的选项,但尚未成功。

var map;
var markers = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var markersIndex = 0;
function initialize() {
    var Wilm = new google.maps.LatLng(34.2257,-77.9447);
    var mapOptions = {
        zoom: 12,
        center: Wilm,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    }
    map = new google.maps.Map(document.getElementById('map-
    canvas'),mapOptions);

    google.maps.event.addListener(map, 'click', function(event) {
            addMarker(event.latLng);
    });

}
function addMarker(location) {
        var marker = new google.maps.Marker({
        position: location,
        label: markers[markersIndex++ % markers.length],
        map: map
    });
    google.maps.event.addListener(marker, 'rightclick', function(event) {
            marker.setMap(null);
    });
    markers.push(marker);
}
google.maps.event.addDomListener(window, 'load', initialize);

不确定标签的含义。

这将带有带有latlng的信息窗口到地图上添加标记:

    function initMap() {
  var Wilm = new google.maps.LatLng(34.2257, -77.9447);
  var mapOptions = {
    zoom: 12,
    center: Wilm,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  }
  map = new google.maps.Map(document.getElementById('map'), mapOptions);
  google.maps.event.addListener(map, 'click', function(event) {
    placeMarker(event.latLng);
  });
}
function placeMarker(location) {

  var marker = new google.maps.Marker({
    position: location,
    map: map
  });
  var contentString = 'Lat' + marker.getPosition().lat() + ', lng:  ' + marker.getPosition().lng();
  var infowindow = new google.maps.InfoWindow({
    content: contentString
  });
  marker.addListener('click', function() {
    infowindow.open(map, marker);
  });
}

function initMap() {
  var Wilm = new google.maps.LatLng(34.2257, -77.9447);
  var mapOptions = {
    zoom: 12,
    center: Wilm,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  }
  map = new google.maps.Map(document.getElementById('map'), mapOptions);
  google.maps.event.addListener(map, 'click', function(event) {
    placeMarker(event.latLng);
  });
}
function placeMarker(location) {
  var marker = new google.maps.Marker({
    position: location,
    map: map
  });
  var contentString = 'Lat' + marker.getPosition().lat() + ', lng:  ' + marker.getPosition().lng();
  var infowindow = new google.maps.InfoWindow({
    content: contentString
  });
  marker.addListener('click', function() {
    infowindow.open(map, marker);
  });
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=KEY&callback=initMap">
</script>

相关内容

  • 没有找到相关文章

最新更新