Google map 上的默认标记非得是一个小图标,无法再标记上加入文字,这在很多时候不能满足需要,比如查询酒店的时候,我希望标记能直接把价格显示出来。我做了一个自定义的类,来满足这种需求。这个类继承自GOverlay,能实现如下图的效果:

下面是这个类的实现代码。
- var _index = 1;
- function KMarker(latlng, tip, markerContent, clickParamer)
- {
- this._latlng = latlng;
- this._tip = tip;
- this._markerContent = markerContent;
- this._clickParamer = clickParamer;
- }
- KMarker.prototype = new GOverlay();
- KMarker.prototype.initialize = function(map_) {
- var markerHtml = document.createElement('div');
- markerHtml.setAttribute("index", this._clickParamer["index"]);
- markerHtml.innerHTML = this._markerContent;
- markerHtml.style.position = "absolute";
- map_.getPane(G_MAP_MARKER_PANE).appendChild(markerHtml);
- var markerTip = document.createElement("div");
- markerTip.innerHTML = this._tip;
- markerTip.style.position = "absolute";
- markerTip.style.display = "none";
- this._map = map_;
- map_.getPane(G_MAP_MARKER_PANE).appendChild(markerTip);
- GEvent.addDomListener(markerHtml, "click", function() {
- if (markerTip.style.display == "none") {
- _mapPage.viewHotelMarker(markerHtml.getAttribute("index"));
- } else {
- _mapPage.hiddenHotelMarker(markerHtml.getAttribute("index"));
- }
- });
- GEvent.addDomListener(markerTip, "click", function() {
- if (markerTip.style.display == "none") {
- _mapPage.viewHotelMarker(markerHtml.getAttribute("index"));
- } else {
- _mapPage.hiddenHotelMarker(markerHtml.getAttribute("index"));
- }
- });
- GEvent.addDomListener(markerHtml, "mouseover", function() {
- _index++;
- markerTip.style.zIndex = _index;
- markerHtml.style.zIndex = _index;
- });
- GEvent.addDomListener(markerTip, "mouseover", function() {
- _index++;
- markerTip.style.zIndex = _index;
- markerHtml.style.zIndex = _index;
- });
- this._markerHtml = markerHtml;
- this._markerTip = markerTip;
- };
- KMarker.prototype.remove = function() {
- this._markerHtml.parentNode.removeChild(this._markerHtml);
- this._markerTip.parentNode.removeChild(this._markerTip);
- };
- KMarker.prototype.copy = function() {
- return new (this._latlng,this._tip,this._markerContent,this._clickParamer);
- };
- KMarker.prototype.redraw = function(force) {
- if (force)
- {
- var p = this._map.fromLatLngToDivPixel(this._latlng);
- this._markerHtml.style.left = (p.x) + "px";
- this._markerHtml.style.top = (p.y) - 25 + "px";
- this._markerTip.style.left = (p.x) + 16 + "px";
- this._markerTip.style.top = (p.y) - 24 + "px";
- }
- };
- KMarker.prototype.getMarkerHtml = function() {
- return this._markerHtml;
- }
- KMarker.prototype.getMarkerTip = function() {
- return this._markerTip;
使用方式:
var marker=new KMarker(new GLatLng(Number(hotel.latitude), Number(hotel.longitude)), tipTemp, markerTemp, {"index": index});
map.addOverlay(marker);
第一个参数是GLatLng类的一个对象,也就是该标记所在经纬度位置。tipTemp是标记的打开后的HTML模板,markerTemp是标记的HTML模板。{"index": index}是标记的编号。
我在上图效果中的模板也发上来,其中id=markerTemp是标记的HTML,id=hotelTemplate的是标记弹出后的HTML。
- <div id="hotelTemplate" style="display:none;">
- <ul class="list clearfix" id="listItem__hotel_passport_" index="_hotel_index_">
- <li class="name">
- <h4>
- <a href="javascript:" onclick="_mapPage.viewHotelDetail('_hotel_passport_','_hotel_category_name_');">
- _hotel_name_
- </a> _star_desc_</h4>
- <span>地址:_hotel_address_</span>
- </li>
- <li class="operate">
- <ul>
- <li class="markerIndex">_hotel_index_</li>
- <li class="price">
- 参考价:<em>_hotel_price_</em>
- </li>
- <li class="tip">
- _last_update_time_前
- </li>
- <li>
- <span class="square_button"
- onclick="_mapPage.viewHotelDetail('_hotel_passport_','_hotel_category_name_');">
- <b>预订</b>
- </span>
- </li>
- </ul>
- </li>
- </ul>
- </div>
- <div id="markerTemp" style="display:none;">
- <div class="mapMaker _COLOR_">
- <div class="left">_LISTINDEX_</div><div class="right"><span class="inner">_HOTELPRICE_</span></div>
- </div>
- </div>
可以通过下面的链接看看实际的效果:
http://t2.shuihuan.com 输入城市,选择地图搜索会出来。
