Skip to content

Instantly share code, notes, and snippets.

@bluishoul
Created May 8, 2014 15:02
Show Gist options
  • Save bluishoul/1bcabb78f3c74b337723 to your computer and use it in GitHub Desktop.
Save bluishoul/1bcabb78f3c74b337723 to your computer and use it in GitHub Desktop.
var map = osc.addModule("map", {
mapid : 'map',
mp : null,
options : [ 'enableScrollWheelZoom' ],
position : null,
Events : {},
opts : {
info_window : false
},
init : function(opt) {
g_utils.binder.call(this);
if (opt && opt.id)
this.mapid = opt.id;
$.extend(this.opts, opt);
this.init_map();
},
init_map : function() {
this.mp = new BMap.Map(this.mapid);
this._init_options();
},
_init_options : function() {
for ( var opt in this.options) {
var method = this.options[opt];
this.mp[method].call(this.mp);
}
},
init_location : function(lng, lat, zoom, opt) {
this.position = new BMap.Point(lng, lat);
this.mp.centerAndZoom(this.position, zoom ? zoom : 15);
this._setOverlay(zoom ? zoom : 15, opt);
},
searchInEvent : function(no_spot) {
var prv = $("#addr_prv").val();
var city = $("#addr_city").val();
var spot = $("#spot").val();
var keys = [];
var zoom = 10;
if (prv)
keys.push(prv);
if (city)
keys.push(city);
if (!no_spot && $.trim(spot).length > 0 && spot != $("#spot").attr("placeholder")) {
keys = [ spot ];
zoom = 15;
}
this.search(keys.join(' '), zoom, {});
},
search : function(key, zoom) {
var self = this;
var search_ = new BMap.LocalSearch(self.mp);
search_.enableFirstResultSelection();
search_.search(key);
search_.setSearchCompleteCallback(function(rs) {
if (search_.getStatus() == BMAP_STATUS_SUCCESS) {
self.mp.removeOverlay(self.marker);
self.position = rs.getPoi(0).point;
self._setOverlay(zoom);
} else {
if (self.position) {
self._setOverlay(zoom);
} else {
self.searchInEvent(true);
}
}
});
},
_setOverlay : function(zoom, MarkerOptions) {
var self = this;
$("#location").val(self.position.lng + "," + self.position.lat);
self.mp.removeOverlay(self.marker);
self.mp.centerAndZoom(self.position, zoom ? zoom : 10);
self.mp.centerAndZoom(self.position, zoom ? zoom : 10);
var size = new BMap.Size(12, 27)
var icon = new BMap.Icon("/img/pin.png", size);
MarkerOptions = $.extend({
icon : icon,
enableDragging : true,
raiseOnDrag : true
}, MarkerOptions);
self.marker = new BMap.Marker(self.position, MarkerOptions);
self.marker.addEventListener("dragend", function(event) {
var point = event.point;
self.position = point;
$("#location").val(self.position.lng + "," + self.position.lat);
self.locToAddr(function(a) {
});
});
self.marker.addEventListener("click", function(event) {
self.openInfoWindow(self.position);
});
self.mp.addOverlay(self.marker);
this.opts.info_window && self._setInfoWindow({
content : MarkerOptions.addr
});
self.locToAddr(function(a) {
});
},
openInfoWindow : function(position) {
this.mp && this.mp.openInfoWindow(this.infoWindow, position || this.mp.getCenter());
},
_setInfoWindow : function(opt) {
opt = $.extend({
width : 80,
height : 60,
title : '在 Baidu 地图中查看:',
content : '当前位置'
}, opt);
this.infoWindow = new BMap.InfoWindow('<a href="http://map.baidu.com/?newmap=1&ie=utf-8&s=s%26wd%3D' + encodeURIComponent(opt.content) + '" target="_blank">' + opt.content + '</a>', opt);
this.openInfoWindow(this.position);
},
locToAddr : function(callback) {
var self = this;
if (callback) {
var geo = new BMap.Geocoder();
geo.getLocation(self.position, function(params) {
callback.call(self, params);
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment