Namespace.register("U.Map"); /* 12软件林志雄*/
/*
需要的变量 :城市名 详细地址 地图级别 地图的宽高 父亲节点
*/
U.Map.defaults = { //默认初始化
"city": "深圳市", "width": "1000", "height": "500", "em": "body", "add": "深圳市罗湖区人民北路金溪大厦"
}
U.Map.defaults.init = function (obj) { //传进来的json值进行覆盖
for (var i in obj) { U.Map.defaults[i] = obj[i]; }
return U.Map.defaults;
}
U.Map.Qs = function (info, _L, _T) {//起始 @parm add 详细地址 @parm city 填城市名 @parm width 元素的宽 @parm height 元素的高 @parm em 父级
info = U.Map.defaults.init(info);
//U.Map.ceateEle(info); //创建元素
var Total = info.em;
var mapTotal = $$("div", { "id": "U_M_Total", style: { "position": "relative", "width": info.width + "px"} }, Total); //地图创建
var result = $$("div", { "id": "map-result", style: { "margin-top": 10 + "px", "position": "absolute", "top": 10 + "px", "right": 10 + "px", "z-index": "99"} }, mapTotal); //搜索框创建
$$("input", { "id": "suggestId", "type": "text", style: { "width": 260 + "px", "padding-top": 9 + "px", "padding-bottom": 9 + "px", "padding-left": 10 + "px", "font-size": 14 + "px", "color": "#333" }, "placeholder": "请输入地址" }, result);
$$("div", { "id": "searchBtn", style: { "width": 43 + "px", "height": "38" + "px", "float": "right", "background": "url('http://d.1473.cn/controls/pc/Map/searchicon.png')", "cursor": "pointer"} }, result);
$$("div", { "id": "l-map", style: { "height": info.height + "px", "width": info.width + "px"} }, mapTotal); //地图创建
//var ss = U.Map.defaults();
//var s = U.Map.defaults.apply(U.Map.defaults, ([""]));
cityinfo = info.add;
city = "深圳市";
var map = new BMap.Map("l-map");
var myGeo = new BMap.Geocoder();
var top_left_control = new BMap.ScaleControl({ anchor: BMAP_ANCHOR_TOP_LEFT }); // 左上角,添加比例尺
var top_left_navigation = new BMap.NavigationControl(); //左上角,添加默认缩放平移控件
map.addControl(top_left_control);
map.addControl(top_left_navigation);
myGeo.getPoint(cityinfo, function (point) { // 将地址解析结果显示在地图上,并调整地图视野
if (point) {
map.centerAndZoom(point, 20); // 初始化地图,设置城市和地图级别。
map.addOverlay(new BMap.Marker(point));
info.em[0].contentWindow.document.body.appendChild(mapTotal);
$(mapTotal).addClass("UCD_IF_CT"); U.CD.L.AppendIF(mapTotal, info.em, _L, _T);
// document.body.appendChild($("#tangram-suggestion--TANGRAM__1x-main")[0])
sss();
} else {
alert("您选择地址没有解析到结果!");
}
}, city);
// map.centerAndZoom("深圳", 20);
function sss() {
var top_left_control = new BMap.ScaleControl({ anchor: BMAP_ANCHOR_TOP_LEFT }); // 左上角,添加比例尺
var top_left_navigation = new BMap.NavigationControl(); //左上角,添加默认缩放平移控件
map.addControl(top_left_control);
map.addControl(top_left_navigation);
var ac = new BMap.Autocomplete( //建立一个自动完成的对象
{"input": "suggestId"
, "location": map
});
ac.addEventListener("onhighlight", function (e) { //鼠标放在下拉列表上的事件
var str = "";
var _value = e.fromitem.value;
var value = "";
if (e.fromitem.index > -1) {
value = _value.province + _value.city + _value.district + _value.street + _value.business;
}
str = "FromItem
index = " + e.fromitem.index + "
value = " + value;
value = "";
if (e.toitem.index > -1) {
_value = e.toitem.value;
value = _value.province + _value.city + _value.district + _value.street + _value.business;
}
str += "
ToItem
index = " + e.toitem.index + "
value = " + value;
$("#searchResultPanel").innerHTML = str;
});
var myValue;
ac.addEventListener("onconfirm", function (e) { //鼠标点击下拉列表后的事件
var _value = e.item.value;
myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
$("#searchResultPanel").innerHTML = "onconfirm
index = " + e.item.index + "
myValue = " + myValue;
U.Map.setPlace();
});
U.Map.setPlace = function () {//搜索函数
map.clearOverlays(); //清除地图上所有覆盖物
U.Map.fun = function () {
var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
map.centerAndZoom(pp, 20);
map.addOverlay(new BMap.Marker(pp)); //添加标注
}
var local = new BMap.LocalSearch(map, { //智能搜索
onSearchComplete: U.Map.fun
});
local.search(myValue);
}
}
}
U.Map.ceateEle = function (info) { //创建地图元素 @param
//var Total = $("#" + info.em)[0];
var Total = info.em;
var mapTotal = $$("div", { "id": "U_M_Total", style: { "position": "relative", "width": info.width + "px"} }, Total); //地图创建
var result = $$("div", { "id": "map-result", style: { "margin-top": 10 + "px", "position": "absolute", "top": 10 + "px", "right": 10 + "px", "z-index": "99"} }, mapTotal); //搜索框创建
$$("input", { "id": "suggestId", "type": "text", style: { "width": 260 + "px", "padding-top": 9 + "px", "padding-bottom": 9 + "px", "padding-left": 10 + "px", "font-size": 14 + "px", "color": "#333" }, "placeholder": "请输入地址" }, result);
$$("div", { "id": "searchBtn", style: { "width": 43 + "px", "height": "38" + "px", "float": "right", "background": "url('searchicon.png')", "cursor": "pointer"} }, result);
$$("div", { "id": "l-map", style: { "height": info.height + "px", "width": info.width + "px"} }, Total); //地图创建
}
U.Map.drapMap = function (obj) {
return U.Map.Qs(obj);
}