[英]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()
,必须设置div
的left
/ 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.