这里用的高德地图.
先说思路:jsp页面展示点线面的坐标,点,线,面,新增,修改,展示,给每个图形绑定左右键事件,都在一个页面上了.把数据放到数据库.
有几点要注意的:
1,在新增和修改坐标的时候因为用的是高德地图,所以获取的是高德坐标-GCJ02形式,但是为了可扩展性,需要转成通用坐标-WGS84
存储到数据库,方便日后在高德和百度地图之间来回折腾,至少以后不用动数据库了.
2,高德地图是支持直接获取到geometry格式的数据的(别问geometry是啥,百度去!),为了以后能用mysql的空间查询.所以前端获取到geo的时候需要转成geojson,这样才能平平安安的传到controller里去.
3,geometry是对象格式.但是在数据库中建的是个字段.在controller里需要用到转换器,把geojson再转成geometry格式,并放到相应的实体类对象的属性中.
4,mymatis不知道这是啥,需要写个工具类让mybatis知道这个类型是可以放到数据库中geometry类型的字段中的.
下面代码:
jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ include file="/common.jsp" %>
显示多个多边形用geo传递
转换工具类:
含geojson转geometry 和 geometry转geojson
package com.bike.park.util;
import cn.hutool.core.date.DateUtil;
import org.locationtech.jts.geom.Geometry;
import org.wololo.geojson.Feature;
import org.wololo.geojson.FeatureCollection;
import org.wololo.geojson.GeoJSON;
import org.wololo.geojson.GeoJSONFactory;
import org.wololo.jts2geojson.GeoJSONReader;
import org.wololo.jts2geojson.GeoJSONWriter;
import com.bike.park.domain.Coor;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
public final class GeoUtils{
//json转geometry对象
public static List geoJsonToBeans(String geoJson , Class instance) throws IOException, IllegalAccessException, InstantiationException {
// InputStream is = new ByteArrayInputStream(geoJson.getBytes());
// org.geotools.feature.FeatureCollection featureCollection = fjson.readFeatureCollection(is);
// org.opengis.feature.Feature[] features = new org.opengis.feature.Feature[featureCollection.size()];
// featureCollection.toArray(features);
GeoJSON geoJSON = GeoJSONFactory.create(geoJson);
Feature[] features = null;
if(geoJSON instanceof Feature){
features = new Feature[1];
features[0] = (Feature)geoJSON;
}else {
FeatureCollection featureCollection = (FeatureCollection)geoJSON;
features = featureCollection.getFeatures();
}
List result = new ArrayList<>(features.length);
Field[] fields = instance.getDeclaredFields();
GeoJSONReader reader = new GeoJSONReader();
for (int i = 0; i < features.length; i++) {
T data = instance.newInstance();
for (Field field : fields) {
field.setAccessible(true);
try {
if (field.getType().equals(Geometry.class)) {
org.wololo.geojson.Geometry geoTemp = features[i].getGeometry();
Geometry geometry = reader.read(geoTemp);
if(null != geometry) {
field.set(data ,geometry);
}
} else if(field.getType().equals(Date.class)) {
Object val = features[i].getProperties().get(field.getName());
if(null != val){
field.set(data, DateUtil.parse(val.toString()));
}
}else if(field.getType().equals(int.class)|| field.getType().equals(Integer.class)) {
Object val = features[i].getProperties().get(field.getName());
if(null != val){
field.set(data, Integer.valueOf(val.toString()));
}
}else if(field.getType().equals(float.class) || field.getType().equals(Float.class)) {
Object val = features[i].getProperties().get(field.getName());
if(null != val){
field.set(data, Float.valueOf(val.toString()));
}
}else {
Object val = features[i].getProperties().get(field.getName());
if(null != val){
field.set(data, val);
}
}
System.out.println(field.getGenericType());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
result.add(data);
}
return result;
}
//geometry对象转json
public static String beanToGeoJson(T data) throws IllegalAccessException {
List features = new ArrayList<>();
if(data instanceof List){
features = beansToFeatrueList((List>) data);
}else {
features.add(beanToFeature(data));
}
GeoJSONWriter writer = new GeoJSONWriter();
GeoJSON json = writer.write(features);
return json.toString();
}
private static Feature beanToFeature(T data) throws IllegalAccessException {
Map properties = new HashMap<>();
Geometry geometry = null;
Field[] fields = data.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object fd = field.get(data);
if(fd instanceof Geometry){
geometry = (Geometry) fd;
}else {
properties.put(field.getName(), field.get(data));
}
}
GeoJSONWriter writer = new GeoJSONWriter();
Feature feature = new Feature(writer.write(geometry), properties);
return feature;
}
private static List beansToFeatrueList(List> data) throws IllegalAccessException {
List featureList = new ArrayList<>();
for (Object datum : data) {
Feature feature = beanToFeature(datum);
featureList.add(feature);
}
return featureList;
}
public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException {
// String wkt = "{\n" +
// " \"type\": \"FeatureCollection\",\n" +
// " \"features\": [\n" +
// " {\n" +
// " \"type\": \"Feature\",\n" +
// " \"properties\": {\n" +
// " \"bmapId\": \"feature__3\",\n" +
// " \"name\": \"三角形\",\n" +
// " \"createtime\": \"2018-05-03\",\n" +
// " \"id\": 1\n" +
// " },\n" +
// " \"geometry\": {\n" +
// " \"type\": \"Polygon\",\n" +
// " \"coordinates\": [\n" +
// " [\n" +
// " [\n" +
// " 39.9689,\n" +
// " 116.461105\n" +
// " ],\n" +
// " [\n" +
// " 39.906939,\n" +
// " 116.377742\n" +
// " ],\n" +
// " [\n" +
// " 39.894097,\n" +
// " 116.466279\n" +
// " ],\n" +
// " [\n" +
// " 39.9689,\n" +
// " 116.461105\n" +
// " ]\n" +
// " ]\n" +
// " ]\n" +
// " }\n" +
// " },\n" +
// " {\n" +
// " \"type\": \"Feature\",\n" +
// " \"properties\": {\n" +
// " \"bmapId\": \"feature__4\",\n" +
// " \"name\": \"多边形\",\n" +
// " \"createtime\": \"20180503\",\n" +
// " \"id\": 2\n" +
// " },\n" +
// " \"geometry\": {\n" +
// " \"type\": \"Polygon\",\n" +
// " \"coordinates\": [\n" +
// " [\n" +
// " [\n" +
// " 39.94899,\n" +
// " 116.513422\n" +
// " ],\n" +
// " [\n" +
// " 39.899412,\n" +
// " 116.567464\n" +
// " ],\n" +
// " [\n" +
// " 39.944122,\n" +
// " 116.601384\n" +
// " ],\n" +
// " [\n" +
// " 39.971997,\n" +
// " 116.585862\n" +
// " ],\n" +
// " [\n" +
// " 39.975978,\n" +
// " 116.573788\n" +
// " ],\n" +
// " [\n" +
// " 39.974208,\n" +
// " 116.56229\n" +
// " ],\n" +
// " [\n" +
// " 39.970227,\n" +
// " 116.552517\n" +
// " ],\n" +
// " [\n" +
// " 39.9689,\n" +
// " 116.541018\n" +
// " ],\n" +
// " [\n" +
// " 39.94899,\n" +
// " 116.513422\n" +
// " ]\n" +
// " ]\n" +
// " ]\n" +
// " }\n" +
// " }\n" +
// " ]\n" +
// "}";
//String wkt = "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"bmapId\":\"feature__1\"},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[[39.920665,116.466276],[39.897198,116.43983],[39.897198,116.493297]]]]}},{\"type\":\"Feature\",\"properties\":{\"bmapId\":\"feature__2\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[39.947221,116.469151]}},{\"type\":\"Feature\",\"properties\":{\"bmapId\":\"feature__3\"},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[[39.955627,116.300704],[39.902511,116.301853],[39.932615,116.328874]]]]}}]}";
//String wkt = "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"bmapId\":\"feature__1\"},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[39.9689,116.461105]]]}},{\"type\":\"Feature\",\"properties\":{\"bmapId\":\"feature__2\"},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[39.94899,116.513422]]]}},{\"type\":\"Feature\",\"properties\":{\"bmapId\":\"feature__3\"},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[[39.914466,116.567464],[39.97642,116.518597],[39.920221,116.479502]]]]}}]}";
//String wkt = "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[255351.0429,474966.9279],[255529.2966,474272.46]]},\"properties\":{\"gid\":\"123456\",\"direction\":2,\"orientation\":1},\"id\":\"fid-50149944_164d5c515c0_-8000\"},{\"type\":\"Feature\",\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[255529.2966,474272.46],[256166.0583,473979.4492]]},\"properties\":{\"gid\":\"123456\",\"direction\":2,\"orientation\":1},\"id\":\"fid-50149944_164d5c515c0_-7fff\"}]}";
//String wkt = "{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[116.34581543862816,39.9239638666205],[116.34924866616723,39.89038576444518],[116.42168976724145,39.88841006945528],[116.41756989419457,39.92686002969596]]]}}";
String wkt = "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"name\":\"金广通\",\"id\":1},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[39.90764094871865,116.56155784524113],[39.970225890953586,116.51250005513577],[39.91346215335588,116.47360443467369],[39.90764094871865,116.56155784524113]]]}},{\"type\":\"Feature\",\"properties\":{\"name\":\"金广通\",\"id\":2},\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[39.90764094871865,116.56155784524113],[39.970225890953586,116.51250005513577],[39.91346215335588,116.47360443467369],[39.90764094871865,116.56155784524113]]}},{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Point\",\"coordinates\":[39.90764094871865,116.56155784524113]}}]}";
List geoBeans = GeoUtils.geoJsonToBeans(wkt , Coor.class);
String s = GeoUtils.beanToGeoJson(geoBeans);
String dd = GeoUtils.beanToGeoJson(geoBeans.get(0));
// GeometryCollection geometryCollection = jsonToGeos(wkt);
// String json = geoToJson(wkt,map);
System.out.println(s);
}
}
mybatis的工具类:
package com.bike.park.mybatis.handler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* mybatis类型转换
* @author Administrator
*
*/
@MappedTypes(Geometry.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class GeoToStringTypeHandler implements TypeHandler {
@Override
public void setParameter(PreparedStatement preparedStatement , int i , Geometry geometry , JdbcType jdbcType) throws SQLException {
preparedStatement.setString(i, geometry.toString());
}
@Override
public Geometry getResult(ResultSet resultSet , String s) throws SQLException {
String wkt = resultSet.getString(s);
return convertGeo(wkt);
}
@Override
public Geometry getResult(ResultSet resultSet , int i) throws SQLException {
String wkt = resultSet.getString(i);
return convertGeo(wkt);
}
@Override
public Geometry getResult(CallableStatement callableStatement , int i) throws SQLException {
String wkt = callableStatement.getString(i);
return convertGeo(wkt);
}
private Geometry convertGeo(String wkt){
if(null == wkt || wkt.isEmpty()){
return null;
}
WKTReader reader = new WKTReader();
Geometry geometry = null;
try {
geometry = reader.read(wkt);
} catch (ParseException e) {
e.printStackTrace();
}
return geometry;
}
}
这个工具类需要在mybatis配置文件中配下路径,我用的springboot所以比较简单
mybatis.type-handlers-package: com.bike.park.mybatis.handler
xml贴出来看下保险点:
REPLACE into test_coor
(
id,
coor,
name,
address,
information,
remark,
geo
)
values
(
#{item.id},
#{item.coor},
#{item.name},
#{item.address},
#{item.information},
#{item.remark},
GeomFromText(#{item.geo})
)
有几个关键字必须要用,至于为什么去查百度吧~!
依赖:
cn.hutool
hutool-all
4.1.3
org.wololo
jts2geojson
0.12.0
net.sf.json-lib
json-lib
2.4
jdk15
左键修改,点谁谁进入修改,右键点击弹框显示本图形信息,每次保存获取地图所有的对象,到数据库,有id的修改,没id新增.