Java实现表单数据的GIS地图展示与交互功能
Java实现表单数据的GIS地图展示与交互功能
引言:GIS(地理信息系统)技术在日常生活、城市规划、环境监测等领域扮演着重要的角色。而在GIS应用中,将表单数据与地图展示与交互相结合,可以更直观地呈现数据,并实现用户与地图的互动。本文将介绍如何使用Java语言实现表单数据的GIS地图展示与交互功能,并给出相关的代码示例。
一、环境配置:在开始之前,我们需要准备以下环境:
二、表单数据的导入:首先,我们需要将表单数据导入到数据库中。这里以MySQL为例,创建一个名为"gis_data"的数据库,并创建一个名为"form_data"的表,表结构如下:
CREATE TABLE form_data ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, address VARCHAR(100) NOT NULL, latitude DOUBLE NOT NULL, longitude DOUBLE NOT NULL );登录后复制
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class DataImporter { public static void importData(File file) throws IOException { try (FileInputStream fis = new FileInputStream(file); Workbook workbook = new XSSFWorkbook(fis); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/gis_data", "root", "password"); PreparedStatement statement = connection.prepareStatement("INSERT INTO form_data (name, address, latitude, longitude) VALUES (?, ?, ?, ?)")) { Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { if (row.getRowNum() == 0) { continue; // skip header row } Cell nameCell = row.getCell(0); Cell addressCell = row.getCell(1); Cell latitudeCell = row.getCell(2); Cell longitudeCell = row.getCell(3); String name = nameCell.getStringCellValue(); String address = addressCell.getStringCellValue(); double latitude = latitudeCell.getNumericCellValue(); double longitude = longitudeCell.getNumericCellValue(); statement.setString(1, name); statement.setString(2, address); statement.setDouble(3, latitude); statement.setDouble(4, longitude); statement.executeUpdate(); } } } }登录后复制
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/gis") public class GisController { @GetMapping("/formData") public List getFormData() { List formDataList = new ArrayList(); try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/gis_data", "root", "password"); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM form_data")) { while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); String address = resultSet.getString("address"); double latitude = resultSet.getDouble("latitude"); double longitude = resultSet.getDouble("longitude"); FormData formData = new FormData(id, name, address, latitude, longitude); formDataList.add(formData); } } catch (SQLException e) { e.printStackTrace(); } return formDataList; } }登录后复制
GIS Map #map { width: 100%; height: 400px; } $(function () { $.get("/gis/formData", function (data) { var features = []; data.forEach(function (formData) { var feature = new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([formData.longitude, formData.latitude])), name: formData.name, address: formData.address }); features.push(feature); }); var vectorSource = new ol.source.Vector({ features: features }); var vectorLayer = new ol.layer.Vector({ source: vectorSource, style: new ol.style.Style({ image: new ol.style.Circle({ radius: 6, fill: new ol.style.Fill({ color: 'blue' }) }) }) }); var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }), vectorLayer ], view: new ol.View({ center: ol.proj.fromLonLat([0, 0]), zoom: 2 }) }); }); }); 登录后复制
以上就是Java实现表单数据的GIS地图展示与交互功能的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!