简体   繁体   English

如何设置单独的 Google 地图标记图标

[英]How to set individual Google Maps Marker Icons

I know this question was asked more often.我知道这个问题被问得更频繁了。 But for my code, unfortunately, none of the answers work for me.但不幸的是,对于我的代码,没有一个答案对我有用。 I have the following Google Maps code and would like to set my own google maps marker icon.我有以下谷歌地图代码,想设置我自己的谷歌地图标记图标。

For this I have tried the following code.为此,我尝试了以下代码。 Unfortunately without success.可惜没有成功。

<script type="text/javascript" src="//maps.googleapis.com/maps/api/js? 
key=key"></script>


<div id="map_canvas"></div>

  <script>
  var locations = [
  ['title',51.0, 11.0, 'https://www.domain.de/, 4],
   ['title',51.0, 11.0, 'https://www.domain.de/, 4],
  ['title',51.0, 11.0, 'https://www.domain.de/, 4],
  ['title',51.0, 11.0, 'https://www.domain.de/, 4],
  ['title',51.0, 11.0, 'https://www.domain.de/, 4],
  ['title',51.0, 11.0, 'https://www.domain.de/, 4]
];
var map;
var markers = [];
function init(){    
  map = new google.maps.Map(document.getElementById('map_canvas'), {
    zoom: 7,
    center: new google.maps.LatLng(51.5151, 10.4545),
     mapTypeId: google.maps.MapTypeId.ROADMAP,
    styles: 
    [
]
  });
function setMarkers(map) {
  var image = {
   url: 'marker-path/marker.jpg',
    size: new google.maps.Size(20, 32),
    origin: new google.maps.Point(0, 0),
    anchor: new google.maps.Point(0, 32)
  };
  var shape = {
    coords: [1, 1, 1, 20, 18, 20, 18, 1],
    type: 'poly'
  };
  var num_markers = locations.length;
  for (var i = 0; i < num_markers; i++) {  
    markers[i] = new google.maps.Marker({
      position: {lat:locations[i][1], lng:locations[i][2]},
      map: map,
icon: image,
      html: locations[i][0],
      id: i,
    });   
    google.maps.event.addListener(markers[i], 'click', function(){
      var infowindow = new google.maps.InfoWindow({
        id: this.id,
        content:this.html,
        position:this.getPosition()
      });
      google.maps.event.addListenerOnce(infowindow, 'closeclick', function(){
          markers[this.id].setVisible(true);
      });
  this.setVisible(false);
  infowindow.open(map);
  });
 }
}}
init();
</script>

Unfortunately, no marker is displayed.不幸的是,没有显示标记。 Where is the problem?问题出在哪儿?

First of all, make sure that (1) the path to your image is correct, and (2) your image's size is in fact 20 x 32 (otherwise change Size accordingly).首先,确保 (1) 图像的路径正确,以及 (2) 图像的大小实际上是 20 x 32(否则相应地更改大小)。

Your code snippet has lots of issues, and I have amended it as follows to be able to reproduce it from my side:您的代码片段有很多问题,我对其进行了如下修改,以便能够从我这边重现它:

<script>
    var locations = [
        ['title', 41.38879, 2.15899, 'https://www.domain.de/', 4],
        ['title', 48.85341, 2.3488, 'https://www.domain.de/', 4],
        ['title', 52.52437, 13.41053, 'https://www.domain.de/', 4]
    ];

    var map;
    var markers = [];

    function init() {
        map = new google.maps.Map(document.getElementById('map_canvas'), {
            zoom: 5,
            center: new google.maps.LatLng(51.5151, 10.4545),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            styles: []
        });

        setMarkers(map);
    }


    function setMarkers(map) {
        var image = {
            url: 'maker-path/maker.jpg',
            size: new google.maps.Size(20, 32),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(0, 32)
        };

        var num_markers = locations.length;

        for (var i = 0; i < num_markers; i++) {
            markers[i] = new google.maps.Marker({
                position: { lat: locations[i][1], lng: locations[i][2] },
                map: map,
                icon: image,
                html: locations[i][0],
                id: i
            });
            google.maps.event.addListener(markers[i], 'click', function() {
                var infowindow = new google.maps.InfoWindow({
                    id: this.id,
                    content: this.html,
                    position: this.getPosition()
                });
                google.maps.event.addListenerOnce(infowindow, 'closeclick', function() {
                    markers[this.id].setVisible(true);
                });
                this.setVisible(false);
                infowindow.open(map);
            });
        }
    }
</script>

You can also test this first by replacing "maker-path/maker.jpg" with eg https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png .您还可以通过将“maker-path/maker.jpg”替换为例如https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png来测试这一点。

You'll see how the icon from Google's documentation is indeed displayed.您将看到Google 文档中的图标确实是如何显示的。

If your own icon is not, then that means that you need to double check pointers (1) and/or (2) I stated above.如果您自己的图标不是,那么这意味着您需要仔细检查我上面提到的指针 (1) 和/或 (2)。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM