此段代码是在java项目前台JS中引用百度地图进行固定点的定位以及位置的搜索功能:
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
地图展示String scname = request.getParameter("scname");
if (scname == null) {
scname = "";
} else {
scname = new String(scname.getBytes("iso-8859-1"), "gbk");
}
%>
(function () {
if (window != window.top) { window.top.location.replace(window.location); //或者干别的事情 }
})();
//新建三个地图上点
var points = [
{"lng":121.432148,"lat":31.215945,"url":"http://www.hyhouse.com/","id":1,"name":"华燕房盟"},
{"lng":121.45708,"lat":31.25203,"url":"https://shhgkj9863.cn.china.cn/","id":2,"name":"华盖科技"},
{"lng":121.409411,"lat":31.178121,"url":"http://www.zhongxicpa.com/","id":3,"name":"中喜"},
{"lng":121.328515,"lat":31.099366,"url":"http://shtianxin.com/","id":3,"name":"天信网络"}
];
//创建标注点并添加到地图中
function addMarker(points) {
//循环建立标注点
for(var i=0, pointsLen = points.length; i
var point = new BMap.Point(points[i].lng, points[i].lat); //将标注点转化成地图上的点
var marker = new BMap.Marker(point); //将点转化成标注点
map.addOverlay(marker); //将标注点添加到地图上
//添加监听事件
(function() {
var thePoint = points[i];
marker.addEventListener("click",
function() {
showInfo(this,thePoint);
});
})();
}
}
function showInfo(thisMarker,point) {
//获取点的信息
var sContent = '
- '
+'
'+'id:' + point.id + '
'+'
'+'名称:' + point.name + '
'+'
查看: 详情'+'';
var infoWindow = new BMap.InfoWindow(sContent); //创建信息窗口对象
thisMarker.openInfoWindow(infoWindow); //图片加载完后重绘infoWindow
}
// 百度地图API功能
var map = new BMap.Map("allmap"); // 创建Map实例
map.centerAndZoom(new BMap.Point(121.4, 31.2), 12); // 初始化地图,设置中心点坐标和地图级别
map.addControl(new BMap.MapTypeControl()); //添加地图类型控件
map.addControl(new BMap.NavigationControl()); //添加控件:缩放地图的
map.addControl(new BMap.ScaleControl()); //添加控件:地图显示比例的控件,默认在左下方;
map.addControl(new BMap.OverviewMapControl()); //添加控件:地图的缩略图的控件,默认在右下方; TrafficControl
map.setCurrentCity("上海"); // 设置地图显示的城市 此项是必须设置的
map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
addMarker(points);
//搜索
function load() {
var scname = document.forms[0].scname.value;
if (scname != null && scname.length > 0) {
var local = new BMap.LocalSearch("上海市", {
renderOptions : {
map : map,
panel : "results",
autoViewport : true,
selectFirstResult : false
}
});
local.search(scname);
}
}