点击地图div上的按钮实现对地图数据的入库操作
程序员文章站
2023-11-29 20:33:22
在地图div上添加一个按钮,单击按钮后弹出一个弹出层,在弹出层的输入框内输入数据后点击提交按钮将数据提交至数据库。 mytest_map.jsp的代码如下: 复制代码 代码...
在地图div上添加一个按钮,单击按钮后弹出一个弹出层,在弹出层的输入框内输入数据后点击提交按钮将数据提交至数据库。
mytest_map.jsp的代码如下:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3c.org/tr/1999/rec-html401-19991224/loose.dtd">
<html lang='zh-cn' xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset='utf-8' />
<title>my openlayers map</title>
<link rel="stylesheet" type="text/css" href="openlayers/theme/default/style.css"/>
<style type="text/css">
#map {
clear: both;
position: relative;
width: 1000px;
height: 600px;
border: 1px solid black;
}
#operator {
position: absolute;
top:10px;
left:40px;
z-index: 3000;
}
#popdiv {
position: absolute;
visibility: hidden;
overflow: hidden;
border: 2px solid #aebbca;
background-color: #eef1f8;
cursor: move;
padding: 1px;
z-index:4000;
}
#poptitle {
background: #9dacbf;
height: 20px;
line-height: 20px;
padding: 1px;
}
#popform {
padding: 2px;
padding-left: 15px;
}
.title_left {
font-weight: bold;
padding-left: 5px;
float: left;
}
.title_right {
float: right;
}
#poptitle .title_right a {
color: #000;
text-decoration: none;
}
#poptitle .title_right a:hover {
text-decoration: underline;
color: #ff0000;
}
</style>
<script type='text/javascript' src='openlayers/openlayers.js'></script>
<script type='text/javascript'>
function init() {
format = 'image/png';
var bounds = new openlayers.bounds(
121.120606, 28.169589,
121.871688, 28.525587
);
var options = {
controls: [],
maxextent: bounds,
maxresolution: 0.0013726171875,
projection: "epsg:4326",
units: 'degrees'
};
map = new openlayers.map('map', options);
//基础图层,可以用一个图层组做基础图层
var baselayer = new openlayers.layer.wms(
"底图",
"http://localhost:8080/geoserver/wenling/wms",
{
layers:'wenling:xianjie_region',
format:format,
},
{
isbaselayer:true,//设置本层为基底图层
yx : {'epsg:4326' : true}
}
);
//县界
var xianjie = new openlayers.layer.wms(
"县界",
"http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:xianjie_region',
transparent:true, //设置从服务器返回的地图图像是透明的
format: format
},
{
displayinlayerswitcher:false, //使该图层不显示在switcher中
//minscale:xxx , //只有达到一定缩放比例该图层时才显示
opacity:0.5 //设置客户端图层半透明
}
);
//水系
var waters = new openlayers.layer.wms(
"水系", "http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:shuixi_region',
transparent:true,
format: format
},
{
opacity:1 //设置客户端图层不透明
}
);
//岛屿
var islands = new openlayers.layer.wms(
"岛", "http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:dao_region',
transparent:true,
format: format
},
{
opacity:0.5
}
);
//铁路
var tielu = new openlayers.layer.wms(
"铁路", "http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:tielu_polyline',
transparent:true,
format: format
},
{
transitioneffect:"resize", //使该图层放大或缩小时产生调整大小的动画
opacity:0.5
}
);
//县
var xian = new openlayers.layer.wms(
"县", "http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:xian_point',
transparent:true,
format: format
},
{
//minscale:xxx , //只有达到一定缩放比例该图层时才显示
opacity:0.5
}
);
//变电站
biandianzhan = new openlayers.layer.wms(
"变电站", "http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:biandianzhan_point',
transparent:true,
format: format
},
{
opacity:0.5
}
);
guangxianwang = new openlayers.layer.wms(
"光纤网", "http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:guangxianwang_polyline',
transparent:true,
format: format
},
{
transitioneffect:"resize", //使该图层放大或缩小时产生调整大小的动画
opacity:0.5
}
);
map.addlayers([baselayer, xianjie, waters, islands, tielu, xian, biandianzhan, guangxianwang]);
map.setcenter(map.getcenter());//设置地图中心点
map.addcontrol(new openlayers.control.panzoombar({
position: new openlayers.pixel(2, 15)
}));
map.addcontrol(new openlayers.control.navigation());
map.addcontrol(new openlayers.control.layerswitcher());
map.addcontrol(new openlayers.control.scale($('scale')));
map.addcontrol(new openlayers.control.mouseposition());
map.zoomtoextent(bounds);
if (!map.getcenter()) {
map.zoomtomaxextent();
}
}
function showpopup() {//弹出层
var objdiv = document.getelementbyid("popdiv");
objdiv.style.top = "50px";//设置弹出层距离上边界的距离
objdiv.style.left = "200px";//设置弹出层距离左边界的距离
objdiv.style.width = "300px";//设置弹出层的宽度
objdiv.style.height = "220px";//设置弹出层的高度
//objdiv.style.display = "block";
objdiv.style.visibility = "visible";
}
function hidepopup() {//关闭层
var objdiv = document.getelementbyid("popdiv");
objdiv.style.visibility = "hidden";
}
</script>
</head>
<body onload='init();'>
<div id='map'>
<div id="operator"><!--按钮所在div-->
<input name="" type="button" onclick="showpopup()" value="操作" />
</div>
<div id="popdiv"><!--弹出层-->
<div id="poptitle"> <!-- 标题div -->
<span class="title_left">修改操作</span> <span class="title_right">
<a href="#" onclick="hidepopup();">关闭</a> </span>
</div>
<div id="popform"> <!-- 表单div -->
<form action="insert_map.jsp" method="post">
<p>
id :<input type="text" name="id" value="0" /> </br>
名 称 :<input type="text" name="name" value="aaa" /> </br>
电压等级 :<input type="text" name="voltage_level" value="110kv" /> </br>
经 度 :<input type="text" name="lon" value="121." /> </br>
纬 度 :<input type="text" name="lat" value="28." /> </br>
</p>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
<input type="reset" value="取消" onclick="hidepopup()" />
</form>
</div>
</div>
</div>
<script type="text/javascript">
/*-------------------------鼠标左键拖动---------------------*/
/*--------当不需要实现此功能时,可以将这一部分代码删除------------*/
var objdiv = document.getelementbyid("popdiv");
var isie = document.all ? true : false;//判断浏览器类型
document.onmousedown = function(evnt) {//当鼠标左键按下后执行此函数
var evnt = evnt ? evnt : event;
if (evnt.button == (document.all ? 1 : 0)) {
moused = true;//moused为鼠标左键状态标志,为true时表示左键被按下
}
}
objdiv.onmousedown = function(evnt) {
objdrag = this;//objdrag为拖动的对象
var evnt = evnt ? evnt : event;
if (evnt.button == (document.all ? 1 : 0)) {
mx = evnt.clientx;
my = evnt.clienty;
objdiv.style.left = objdiv.offsetleft + "px";
objdiv.style.top = objdiv.offsettop + "px";
if (isie) {
objdiv.setcapture();
//objdiv.filters.alpha.opacity = 50;//当鼠标按下后透明度改变
} else {
window.captureevents(event.mousemove);//捕获鼠标拖动事件
//objdiv.style.opacity = 0.5;//当鼠标按下后透明度改变
}
}
}
document.onmouseup = function() {
moused = false;//左键松开
objdrag = "";
if (isie) {
objdiv.releasecapture();
//objdiv.filters.alpha.opacity = 100;//当鼠标左键松开后透明度改变
} else {
window.releaseevents(objdiv.mousemove);//释放鼠标拖动事件
//objdiv.style.opacity = 1;//当鼠标左键松开后透明度改变
}
}
document.onmousemove = function(evnt) {
var evnt = evnt ? evnt : event;
if (moused == true && objdrag) {
var mrx = evnt.clientx - mx;
var mry = evnt.clienty - my;
objdiv.style.left = parseint(objdiv.style.left) + mrx + "px";
objdiv.style.top = parseint(objdiv.style.top) + mry + "px";
mx = evnt.clientx;
my = evnt.clienty;
}
}
</script>
</body>
</html>
在浏览器输入:http://localhost:8080/myapp/mytestmap.html
访问到mytestmap.jsp页面,单击左上角的“操作”按钮,结果如下:
在输入框中输入测试数据,点击提交,将会提交至insert_map.jsp页面,并在insert_map.jsp页面上显示出数据库中所有的点(包括刚刚添加的这个点),效果如下图:
再次在浏览器中访问地图页面,可以看到地图上已经多出了刚刚添加的那个点,如下图:
至此,基本操作算是完成。
后续需要做的工作有:
1,使点击提交后仍然显示在当前页面,且对地图进行刷新。
2,完善delete功能、修改功能
3,点击“操作”按钮后可以选择添加、删除或修改。
mytest_map.jsp的代码如下:
复制代码 代码如下:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3c.org/tr/1999/rec-html401-19991224/loose.dtd">
<html lang='zh-cn' xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset='utf-8' />
<title>my openlayers map</title>
<link rel="stylesheet" type="text/css" href="openlayers/theme/default/style.css"/>
<style type="text/css">
#map {
clear: both;
position: relative;
width: 1000px;
height: 600px;
border: 1px solid black;
}
#operator {
position: absolute;
top:10px;
left:40px;
z-index: 3000;
}
#popdiv {
position: absolute;
visibility: hidden;
overflow: hidden;
border: 2px solid #aebbca;
background-color: #eef1f8;
cursor: move;
padding: 1px;
z-index:4000;
}
#poptitle {
background: #9dacbf;
height: 20px;
line-height: 20px;
padding: 1px;
}
#popform {
padding: 2px;
padding-left: 15px;
}
.title_left {
font-weight: bold;
padding-left: 5px;
float: left;
}
.title_right {
float: right;
}
#poptitle .title_right a {
color: #000;
text-decoration: none;
}
#poptitle .title_right a:hover {
text-decoration: underline;
color: #ff0000;
}
</style>
<script type='text/javascript' src='openlayers/openlayers.js'></script>
<script type='text/javascript'>
function init() {
format = 'image/png';
var bounds = new openlayers.bounds(
121.120606, 28.169589,
121.871688, 28.525587
);
var options = {
controls: [],
maxextent: bounds,
maxresolution: 0.0013726171875,
projection: "epsg:4326",
units: 'degrees'
};
map = new openlayers.map('map', options);
//基础图层,可以用一个图层组做基础图层
var baselayer = new openlayers.layer.wms(
"底图",
"http://localhost:8080/geoserver/wenling/wms",
{
layers:'wenling:xianjie_region',
format:format,
},
{
isbaselayer:true,//设置本层为基底图层
yx : {'epsg:4326' : true}
}
);
//县界
var xianjie = new openlayers.layer.wms(
"县界",
"http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:xianjie_region',
transparent:true, //设置从服务器返回的地图图像是透明的
format: format
},
{
displayinlayerswitcher:false, //使该图层不显示在switcher中
//minscale:xxx , //只有达到一定缩放比例该图层时才显示
opacity:0.5 //设置客户端图层半透明
}
);
//水系
var waters = new openlayers.layer.wms(
"水系", "http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:shuixi_region',
transparent:true,
format: format
},
{
opacity:1 //设置客户端图层不透明
}
);
//岛屿
var islands = new openlayers.layer.wms(
"岛", "http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:dao_region',
transparent:true,
format: format
},
{
opacity:0.5
}
);
//铁路
var tielu = new openlayers.layer.wms(
"铁路", "http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:tielu_polyline',
transparent:true,
format: format
},
{
transitioneffect:"resize", //使该图层放大或缩小时产生调整大小的动画
opacity:0.5
}
);
//县
var xian = new openlayers.layer.wms(
"县", "http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:xian_point',
transparent:true,
format: format
},
{
//minscale:xxx , //只有达到一定缩放比例该图层时才显示
opacity:0.5
}
);
//变电站
biandianzhan = new openlayers.layer.wms(
"变电站", "http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:biandianzhan_point',
transparent:true,
format: format
},
{
opacity:0.5
}
);
guangxianwang = new openlayers.layer.wms(
"光纤网", "http://localhost:8080/geoserver/wenling/wms",
{
layers: 'wenling:guangxianwang_polyline',
transparent:true,
format: format
},
{
transitioneffect:"resize", //使该图层放大或缩小时产生调整大小的动画
opacity:0.5
}
);
map.addlayers([baselayer, xianjie, waters, islands, tielu, xian, biandianzhan, guangxianwang]);
map.setcenter(map.getcenter());//设置地图中心点
map.addcontrol(new openlayers.control.panzoombar({
position: new openlayers.pixel(2, 15)
}));
map.addcontrol(new openlayers.control.navigation());
map.addcontrol(new openlayers.control.layerswitcher());
map.addcontrol(new openlayers.control.scale($('scale')));
map.addcontrol(new openlayers.control.mouseposition());
map.zoomtoextent(bounds);
if (!map.getcenter()) {
map.zoomtomaxextent();
}
}
function showpopup() {//弹出层
var objdiv = document.getelementbyid("popdiv");
objdiv.style.top = "50px";//设置弹出层距离上边界的距离
objdiv.style.left = "200px";//设置弹出层距离左边界的距离
objdiv.style.width = "300px";//设置弹出层的宽度
objdiv.style.height = "220px";//设置弹出层的高度
//objdiv.style.display = "block";
objdiv.style.visibility = "visible";
}
function hidepopup() {//关闭层
var objdiv = document.getelementbyid("popdiv");
objdiv.style.visibility = "hidden";
}
</script>
</head>
<body onload='init();'>
<div id='map'>
<div id="operator"><!--按钮所在div-->
<input name="" type="button" onclick="showpopup()" value="操作" />
</div>
<div id="popdiv"><!--弹出层-->
<div id="poptitle"> <!-- 标题div -->
<span class="title_left">修改操作</span> <span class="title_right">
<a href="#" onclick="hidepopup();">关闭</a> </span>
</div>
<div id="popform"> <!-- 表单div -->
<form action="insert_map.jsp" method="post">
<p>
id :<input type="text" name="id" value="0" /> </br>
名 称 :<input type="text" name="name" value="aaa" /> </br>
电压等级 :<input type="text" name="voltage_level" value="110kv" /> </br>
经 度 :<input type="text" name="lon" value="121." /> </br>
纬 度 :<input type="text" name="lat" value="28." /> </br>
</p>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
<input type="reset" value="取消" onclick="hidepopup()" />
</form>
</div>
</div>
</div>
<script type="text/javascript">
/*-------------------------鼠标左键拖动---------------------*/
/*--------当不需要实现此功能时,可以将这一部分代码删除------------*/
var objdiv = document.getelementbyid("popdiv");
var isie = document.all ? true : false;//判断浏览器类型
document.onmousedown = function(evnt) {//当鼠标左键按下后执行此函数
var evnt = evnt ? evnt : event;
if (evnt.button == (document.all ? 1 : 0)) {
moused = true;//moused为鼠标左键状态标志,为true时表示左键被按下
}
}
objdiv.onmousedown = function(evnt) {
objdrag = this;//objdrag为拖动的对象
var evnt = evnt ? evnt : event;
if (evnt.button == (document.all ? 1 : 0)) {
mx = evnt.clientx;
my = evnt.clienty;
objdiv.style.left = objdiv.offsetleft + "px";
objdiv.style.top = objdiv.offsettop + "px";
if (isie) {
objdiv.setcapture();
//objdiv.filters.alpha.opacity = 50;//当鼠标按下后透明度改变
} else {
window.captureevents(event.mousemove);//捕获鼠标拖动事件
//objdiv.style.opacity = 0.5;//当鼠标按下后透明度改变
}
}
}
document.onmouseup = function() {
moused = false;//左键松开
objdrag = "";
if (isie) {
objdiv.releasecapture();
//objdiv.filters.alpha.opacity = 100;//当鼠标左键松开后透明度改变
} else {
window.releaseevents(objdiv.mousemove);//释放鼠标拖动事件
//objdiv.style.opacity = 1;//当鼠标左键松开后透明度改变
}
}
document.onmousemove = function(evnt) {
var evnt = evnt ? evnt : event;
if (moused == true && objdrag) {
var mrx = evnt.clientx - mx;
var mry = evnt.clienty - my;
objdiv.style.left = parseint(objdiv.style.left) + mrx + "px";
objdiv.style.top = parseint(objdiv.style.top) + mry + "px";
mx = evnt.clientx;
my = evnt.clienty;
}
}
</script>
</body>
</html>
在浏览器输入:http://localhost:8080/myapp/mytestmap.html
访问到mytestmap.jsp页面,单击左上角的“操作”按钮,结果如下:
在输入框中输入测试数据,点击提交,将会提交至insert_map.jsp页面,并在insert_map.jsp页面上显示出数据库中所有的点(包括刚刚添加的这个点),效果如下图:
再次在浏览器中访问地图页面,可以看到地图上已经多出了刚刚添加的那个点,如下图:
至此,基本操作算是完成。
后续需要做的工作有:
1,使点击提交后仍然显示在当前页面,且对地图进行刷新。
2,完善delete功能、修改功能
3,点击“操作”按钮后可以选择添加、删除或修改。