一、测试openlayers
官网
1.加载地图,并添加鼠标移动位置坐标
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>map demo</title>
<link rel="stylesheet" href="lib/ol4.6.4/ol.css" type="text/css">
<style>
html,
body,
#map{
padding: 0;
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.ol-mouse-position{
position: absolute;
bottom: 10px;
right: 10px;
background: rgba(255,255,255,.6);
padding: 10px;
border: 1px solid #ccc;
z-index: 9;
top: auto;
display: none;
}
</style>
<script src="lib/ol4.6.4/ol.js"></script>
<!-- 引入jqury,处理地图事件 -->
<script src="https://code.jquery.com/jquery-3.6.4.js" integrity="sha256-a9jBBRygX1Bh5lt8GZjXDzyOB+bWve9EiO7tROUtj/E=" crossorigin="anonymous"></script>
</head>
<body>
<div id="map">
<!-- 鼠标位置坐标区 -->
<div id="mousepos" class="ol-mouse-position"></div>
</div>
<script>
var map = new ol.Map({
controls: ol.control.defaults({
attribution: false
}).extend([]),
target: 'map',
//图层
layers: [
new ol.layer.Tile({
source: new ol.source.OSM({
//使用高德瓦片
url:'http://wprd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7'
})
})
],
//视图
view: new ol.View({
center: ol.proj.transform([110,39],"EPSG:4326","EPSG:3857"),
zoom: 6,
// rotation:90
}),
});
//添加鼠标移动事件
map.on("pointermove",(evt)=>{
//获取经纬度,并且需要坐标转换
var lonlat = ol.proj.transform(evt.coordinate,"EPSG:3857","EPSG:4326");
$("#mousepos").html(lonlat[0].toFixed(3)+","+lonlat[1].toFixed(3)).show();
})
</script>
</body>
</html>
效果图
二、创建springweb项目
1.使用idea的maven构建webapp
生成的目录结构
2.配置tomcat启动
测试启动
3.配置jdbc
webapp配置servlet拦截
<servlet>
<servlet-name>lzugis</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
pom文件添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<springmvc.version>4.1.9.RELEASE</springmvc.version>
<jackson.version>2.5.0</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- jackson start -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- jackson end -->
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springmvc.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springmvc.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springmvc.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springmvc.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${springmvc.version}</version>
</dependency>
<!-- database-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
<build>
<finalName>demo</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
配置数据库信息
创建JDBC模板
4.展示地图
修改index.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>map demo</title>
<link rel="stylesheet" href="js/lib/ol4.6.4/ol.css" type="text/css">
<link rel="stylesheet" href="css/demo.css" type="text/css">
</head>
<body>
<div id="map">
<div class="mouse-position"></div>
</div>
<script src="js/lib/ol4.6.4/ol.js"></script>
<script src="js/lib/jquery/jquery-3.3.1.min.js"></script>
<script src="js/demo3-1.js"></script>
</body>
</html>
新建demo3-1.js的js文件
window.onload=initMap();
function initMap(){
var base = new ol.layer.Tile({
source: new ol.source.OSM({
//使用高德瓦片
url:'http://wprd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7'
})
});
var vector = new ol.layer.Vector({
source: null,
style: styleFunction
});
window.map = new ol.Map({
controls: ol.control.defaults({
attribution: false
}).extend([]),
target: 'map',
layers: [base, vector],
view: new ol.View({
zoom: 4,
center:ol.proj.transform([110, 39],"EPSG:4326", "EPSG:3857")
})
});
// $.get("test/capitals", function (result) {
// console.log(result);
// var features = [];
// for(var i=0,len=result.length;i<len;i++){
// var _r = result[i],
// coord = ol.proj.transform([_r.lon, _r.lat], "EPSG:4326", "EPSG:3857");
// var feature = new ol.Feature({
// geometry: new ol.geom.Point(coord),
// name:_r.name
// });
// features.push(feature);
// }
// var source = new ol.source.Vector({
// features: features
// });
// vector.setSource(source);
// });
}
function styleFunction(feature) {
var stroke = new ol.style.Stroke({
color: 'black',
width: 2
});
var fill = new ol.style.Fill({
color: 'red'
});
var _name = feature.get("name");
var _radius = 6,
_radius2 = 6;
if(_name==="北京"){
_radius = 8;
_radius2 = 4;
}
return new ol.style.Style({
stroke:stroke,
fill: fill,
image: new ol.style.RegularShape({
fill: fill,
stroke: stroke,
points: 5,
radius: _radius,
radius2: _radius2,
angle: 0
})
});
}
重启项目
三、使用postgresql空间型数据库
1.下载,安装
2.启动
3.右键数据库,新建数据库-lzugis,注意新建数据库时候需要使用系统自带的模板,可以使用数据库模板带的函数;
4.选择架构-public-数据表,新建表,建表和mysql基本一致,但是这边咱们直接导入省市等脚本文件,右键数据库,恢复,导入.back脚本文件;
5.使用idea链接数据库,方便写sql;
四、spingmvc项目调用数据库,使用三层架构
WebtestController代码
package com.lzugis.web.webtest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class WebtestController {
@Autowired
private WebtestService testService;
@RequestMapping(value = "test/capitals")
public List getCapitals(){
return testService.getCapitals();
}
}
WebtestDao代码
package com.lzugis.web.webtest;
import com.lzugis.helper.CommonDao;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class WebtestDao extends CommonDao {
public List getCapitals(){
String sql = "select id,name,lon,lat from capital";
return this.jdbcTemplate.queryForList(sql);
}
}
WebtestService代码
package com.lzugis.web.webtest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class WebtestService {
@Autowired
private WebtestDao testDao;
public List getCapitals(){
return testDao.getCapitals();
}
}
重启项目,成功。
五、使用geoserver
1.启动
2.访问localhost:8086,登录
3.新建工作区