我正在尝试生成一个GeoJson以发送LineString
类型的Google Maps(Google API)。我正在使用Spring Boot。
现在,我可以生成一个Json,但是找不到找到“转换”或“转换”该Json的方法。
我使用Model类从查询中发送数据(我使用MySQL)。因此,我的想法是通过相应的坐标传递讲义(扫描仪名称)。
这是我的模特
package com.geologistic.model;
public class PaqueteJson {
private String nombreEscaneo;
private String latitud;
private String longitud;
public String getNombreEscaneo() {
return nombreEscaneo;
}
public void setNombreEscaneo(String nombreEscaneo) {
this.nombreEscaneo = nombreEscaneo;
}
public String getLatitud() {
return latitud;
}
public void setLatitud(String latitud) {
this.latitud = latitud;
}
public String getLongitud() {
return longitud;
}
public void setLongitud(String longitud) {
this.longitud = longitud;
}
public PaqueteJson() {
super();
// TODO Auto-generated constructor stub
}
public PaqueteJson(String nombreEscaneo, String latitud, String longitud) {
super();
this.nombreEscaneo = nombreEscaneo;
this.latitud = latitud;
this.longitud = longitud;
}
@Override
public String toString() {
return "PaqueteJson [nombreEscaneo=" + nombreEscaneo + ", latitud=" + latitud + ", longitud=" + longitud + "]";
}
}
这是我的查询,用于生成我的Consult和Json。
public List<PaqueteJson> findJson()
{
List<PaqueteJson> paquetes= jdbcTemplate.query("select * from agencia ", new RowMapper<PaqueteJson>() {
public PaqueteJson mapRow (ResultSet rs, int argl) throws SQLException{
PaqueteJson paquete = new PaqueteJson (rs.getString("nombreEscaneo"),rs.getString("latitud"),
rs.getString("longitud"));
return paquete;
}
});
return paquetes;
}
这是我用来从控制器调用查询的方式。
@RequestMapping(value = "/test", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<PaqueteJson>> getTimelineProjectCaptions() {
return new ResponseEntity<List<PaqueteJson>>(paqueteService.findJson(), HttpStatus.OK);
}
我实际的Json生成如下:
[{"nombreEscaneo":"SALIDAS ALBACETE","latitud":"39.018922","longitud":"-1.875926"},{"nombreEscaneo":"SALIDAS PLATAFORMA BAILEN","latitud":"38.085772","longitud":"-3.773147"}, ....
因此,我想返回一个GeoJson将该GeoJson发送到Google API,但是我找不到解决方法,我已经搜索了很多东西,但是找到了一种方法,但是使用Mongo DB(我正在使用MySQL)。
答案 0 :(得分:0)
最后,我决定选择“功能集”
解决方案
JSONObject featureCollection = new JSONObject();
featureCollection.put("type", "FeatureCollection");
JSONObject properties = new JSONObject();
properties.put("name", "ESPG:4326");
JSONObject crs = new JSONObject();
crs.put("type", "name");
crs.put("properties", properties);
featureCollection.put("crs", crs);
JSONArray features = new JSONArray();
// ciclo for
for (PaqueteJson obj : paqueteService.findJson()) {
JSONObject feature = new JSONObject();
feature.put("type", "Feature");
//JSONArray JSONArrayCoord = new JSONArray();
JSONObject geometry = new JSONObject();
JSONObject desc = new JSONObject();
JSONObject newFeature = new JSONObject();
//JSONArrayCoord.put(0, Double.parseDouble(obj.getLongitud()));
//JSONArrayCoord.put(1, Double.parseDouble(obj.getLatitud()));
JSONArray JSONArrayCoord = new JSONArray("[" + obj.getLongitud() + "," + obj.getLatitud() + "]");
geometry.put("type", "Point");
geometry.put("coordinates", JSONArrayCoord);
feature.put("geometry", geometry);
// proper.put("properties", desc);
feature.put("properties", desc);
desc.put("name", obj.getNombreEscaneo());
features.put(feature);
featureCollection.put("features", features);
// System.out.println(featureCollection.toString());
// }
}
System.out.println(featureCollection.toString());