简体   繁体   English

如何在Google Maps API中添加多个叠加标记

[英]How do I add multiple overlay markers in google maps api

I was thankfully able to one marker to show on the screen using this fiddle, however, when I created another marker or any after that, it seems that the markers append&stacked on themselves. 幸运的是,我能够使用这种小提琴在屏幕上显示一个标记,但是,当我创建另一个标记或之后的任何标记时,这些标记似乎会自己追加和堆叠。

Is there a way to have each marker in it's own location? 有没有办法将每个标记放置在自己的位置?

The Fiddle: http://jsfiddle.net/syrtrjsa/5/ 小提琴: http//jsfiddle.net/syrtrjsa/5/

And it's code: 它是代码:

var overlay;

function initialize() {
    var myLatLng = new google.maps.LatLng(62.323907, -150.109291);
    var mapOptions = {
        zoom: 11,
        center: myLatLng,
        mapTypeId: google.maps.MapTypeId.SATELLITE
    };

    var gmap = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

    function HTMLMarker(lat, lng) {
        this.lat = lat;
        this.lng = lng;
        this.pos = new google.maps.LatLng(lat, lng);
    }

    HTMLMarker.prototype = new google.maps.OverlayView();
    HTMLMarker.prototype.onRemove = function () {}

    //init your html element here
    HTMLMarker.prototype.onAdd = function () {
        div = document.createElement('DIV');
        div.className = "htmlMarker";
        div.innerHTML = '<img src="http://www.w3schools.com/html/pic_mountain.jpg" alt="Mountain View" style="width:30px;height:22px">' + 21;
        var panes = this.getPanes();
        panes.overlayImage.appendChild(div);
    }

    HTMLMarker.prototype.draw = function () {
        var overlayProjection = this.getProjection();
        var position = overlayProjection.fromLatLngToDivPixel(this.pos);
        var panes = this.getPanes();
        panes.overlayImage.style.left = position.x + 'px';
        panes.overlayImage.style.top = position.y - 30 + 'px';
    }

    //to use it
    var htmlMarker = new HTMLMarker(62.323907, -150.109291);
    htmlMarker.setMap(gmap);
    var htmlMarker = new HTMLMarker(62.323907, -151.109291);
    htmlMarker.setMap(gmap);
}

Your implementation of OverlayView is wrong. 您对OverlayView的实现是错误的。

In draw() you must set the left / top of the div , not of the pane. draw() ,必须设置divleft / top ,而不是窗格的left / top Additionally the div must have the position set to absolute 另外, div必须将位置设置为absolute位置

Fixed version: 固定版本:

http://jsfiddle.net/syrtrjsa/15/ http://jsfiddle.net/syrtrjsa/15/

You can try this :)... store array HTMLMarker :) 您可以尝试这个:)...存储数组HTMLMarker :)

var htmlMarker[];
for(var i = 0; i < 10; i++){
    (function(i){            
        htmlMarker[i] = new HTMLMarker(items[i].lat, items[i].lng);
        htmlMarker[i].setMap(gmap);
     })(i);
}

in this code: items is array of location :) ... 在此代码中: items是位置数组:) ...

暂无
暂无

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

相关问题 如何以编程方式在Google地图中添加多个标记? - How do I add multiple markers in Google Maps programmatically? 如何在多个标记中添加侦听器,这些标记调用相同的函数以在Google Maps API v3中单击时显示方向 - How do I add a listener to multiple markers which call the same function to display directions on click in google maps api v3 Google Maps API,如何为多个自定义标记添加标记阴影 - Google maps api, how to add marker shadows for multiple custom markers 在Google Maps API中,如何插入带有多个信息窗口的多个标记? - In the Google Maps API, how do I insert multiple markers with multiple info windows? 如何将位智事件标记添加到带有流量的Google Maps javascript API生成的地图中? - How do I add waze incident markers to a google maps javascript API-generated map with traffic? 如何向多个标记添加相同的事件侦听器,然后区分 Google Maps API v3 中侦听器中的标记? - How do I add same event listener to many markers and then differentiate between the markers in the listeners in Google Maps API v3? 如何将标记添加到带有延迟的Google地图 - How do I add the markers to the google maps wth delays 如何使用 Google Maps API 自定义不同的标记 CSS? - How do I customize different markers CSS with Google Maps API? 如何更新谷歌地图中多个标记的位置 - How do I update the locations of multiple markers in google maps 如何使用 react-google-maps 显示多个标记 - How do I display multiple markers with react-google-maps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM