【问题标题】:Is there a way to label my map markers with letters?有没有办法用字母标记我的地图标记?
【发布时间】:2016-04-16 03:00:57
【问题描述】:
function initialize() {
var map;
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
    mapTypeId: 'roadmap'
};

// Display a map on the page
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
map.setTilt(45);

// Multiple Markers

var markers = [
    ['Joe Brown Park, New Orleans',             30.030342, -89.966561],
    ['City Park, New Orleans',                  29.993514, -90.098118]
];

// Info Window Content
var infoWindowContent = [

    [
    '<h3>Joe Brown Park</h3>' +
    '<h3>Named after 1 of the states largest independent oil producers, this park offers year-round events.</h3>' +
    '</div>'],
    [
    '<h3>City Park </h3>' +
    '<h3>A 1,300 acre public park in New Orleans, Louisiana, is the 87th largest and 7th-most-visited urban public park in the United States.</h3>' +
    '</div>'],
];

// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow(), marker, i;

// Loop through our array of markers & place each one on the map  
for( i = 0; i < markers.length; i++ ) {
    var position = new google.maps.LatLng(
        markers[i][1], 
        markers[i][2],
        markers[i][3]);
    bounds.extend(position);
    marker = new google.maps.Marker({
        position: position,
        map: map,
        title: markers[i][0]
    });

    // Allow each marker to have an info window    
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infoWindow.setContent(infoWindowContent[i][0]);
            infoWindow.open(map, marker);
        }
    })(marker, i));

}

我可以在我的工作地图中添加一个 Icon 变量或一些简单的东西,还是我需要更改我的大部分代码来为我的标记添加字母?在此先感谢

【问题讨论】:

    标签: php google-maps google-maps-markers


    【解决方案1】:

    您可以在标记数组中添加另一列:

    var markers = [
        ['Joe Brown Park, New Orleans',             30.030342, -89.966561,'','PATHTOICON/icon1.png'],
        ['City Park, New Orleans',                  29.993514, -90.098118,'','PATHTOICON/icon2.jpg']
    ];
    

    然后在添加标记的循环中添加 icon: 并指向数组中的图标列

    for( i = 0; i < markers.length; i++ ) {
        var position = new google.maps.LatLng(
            markers[i][1], 
            markers[i][2],
            markers[i][3]);
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i][0],
            icon: markers[i][4]// <=== each marker can have its own icon
        });
    

    【讨论】:

    • 你为什么把答案记下来了?
    • 不知道这是怎么发生的。对不起@binaryghost
    【解决方案2】:

    Google Maps Javascript API v3 支持single character labels on markers,这将使用数组中标记的编号(从零开始)标记您的标记。

    marker = new google.maps.Marker({
      position: position,
      map: map,
      title: markers[i][0],
      label: ""+i
    });
    

    如果你希望它是一个字母,你可以将字母作为第四个元素添加到数组中,或者将数组索引转换为一个字母:

    var label = String.fromCharCode(65+i);
    marker = new google.maps.Marker({
      position: position,
      map: map,
      title: markers[i][0],
      label: label
    });
    

    proof of concept fiddle

    代码 sn-p:

    function initialize() {
      var map;
      var bounds = new google.maps.LatLngBounds();
      var mapOptions = {
        mapTypeId: 'roadmap'
      };
    
      // Display a map on the page
      map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
      map.setTilt(45);
    
      // Multiple Markers
      var markers = [
        ['Joe Brown Park, New Orleans', 30.030342, -89.966561],
        ['City Park, New Orleans', 29.993514, -90.098118]
      ];
    
      // Info Window Content
      var infoWindowContent = [
        [
          '<h3>Joe Brown Park</h3>' +
          'Named after 1 of the states largest independent oil producers, this park offers year-round events.' +
          '</div>'
        ],
        [
          '<h3>City Park </h3>' +
          'A 1,300 acre public park in New Orleans, Louisiana, is the 87th largest and 7th-most-visited urban public park in the United States.' +
          '</div>'
        ],
      ];
    
      // Display multiple markers on a map
      var infoWindow = new google.maps.InfoWindow(), marker, i;
      // Loop through our array of markers & place each one on the map  
      for (i = 0; i < markers.length; i++) {
        var position = new google.maps.LatLng(
          markers[i][1],
          markers[i][2]);
        bounds.extend(position);
        var label = String.fromCharCode(65 + i);
        marker = new google.maps.Marker({
          position: position,
          map: map,
          title: markers[i][0],
          label: label
        });
    
        // Allow each marker to have an info window    
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
          return function() {
            infoWindow.setContent(infoWindowContent[i][0]);
            infoWindow.open(map, marker);
          }
        })(marker, i));
      }
      map.fitBounds(bounds);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    html,
    body,
    #map_canvas {
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0;
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="map_canvas"></div>

    【讨论】:

    • 感谢您提供信息链接和快速回复。
    猜你喜欢
    • 1970-01-01
    • 2019-07-09
    • 2016-09-07
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多