google地图overlay

就是添加一个自定义的层,使用方法(初始化一个):
new FastMarker();

function FastMarker(latlng, options) {
	this.latlng = latlng;
	this.options = options || {};
}
FastMarker.prototype = new google.maps.OverlayView();
FastMarker.prototype.onAdd = function(map) {
	var div = document.createElement('div');
	if (this.options.className) div.className = this.options.className;
	if (this.options.html) div.innerHTML = this.options.html;
	if (this.options.dom) div.appendChild(this.options.dom);
	if (this.options.id) div.id = this.options.id;
	div.style.position = 'absolute';
	this.map = map;
	this.div = div;
	var events = ['click', 'dblclick', 'contextmenu', 'mousedown', 'mouseup', 'mouseover', 'mouseout', 'mousewheel', 'DOMMouseScroll'];
	for (var i = 0; i < events.length; i++)
	 google.maps.event.addDomListener(div, events[i], this.stop, false);
	var panes = this.getPanes();
	panes.overlayLayer.appendChild(div);
};
FastMarker.prototype.draw = function(force) {
	var point = this.getProjection().fromLatLngToDivPixel(this.latlng);
	this.div.style.top = (point.y) + 'px';
	this.div.style.left = (point.x) + 'px';
};
FastMarker.prototype.onRemove = function() {
	google.maps.event.clearInstanceListeners(this.div);
	this.div.parentNode.removeChild(this.div);
	this.div = null;
};
FastMarker.prototype.getPosition = function() {
	return this.latlng;
};
FastMarker.prototype.stop = function(e) {
	if (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && document.all) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else {
		e.stopPropagation();
	}
};

标签: none

仅有一条评论

  1. 这里的FastMarker类似于php里面的一个类+__construct方法,prototype的属性就相当于类里面的function

添加新评论