Tengo mi controlador y ahí mismo hago mi objeto Json con formato GeoJson para poder visualizarlo en Google Maps (Google API), trato de enviarlo como Model hacia la vista, y de asignarlo como variable para poder incrustarlo en la lectura de Google Maps, pero simplemente no pasa nada, el mapa carga pero sin ningun tipo de valores del Json, no marca ningún error simplemente no sucede nada.
Ya está verificado el GeoJson en geojson.io, por lo que es correcto los datos.
Este es mi controlador:
@GetMapping("/test")
public String Json( Model model) throws JSONException {
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();
.
.
.
.
// System.out.println(featureCollection.toString());
// }
}
//System.out.println(featureCollection.toString());
model.addAttribute("geojson",featureCollection);
return "test";
}
Lo envio con nombre"geojson" hacia la vista.
Mi HTML:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<title>Data Layer: Simple</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body onload="initMap()">
<div id="map"></div>
<script th:inline="javascript">
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom : 4,
center : {
lat : -7,
lng : 137
}
});
var json = [[${geojson}]];
//var geojson = /*[[${geojson}]]*/'default';
map.data.addGeoJson(json);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=MYKEYHERE&callback=initMap">
</script>
</body>
</html>
Lo recibo con dicho nombre "geojson" y lo asigno a una variable para cargarlo con .addGeoJson
Realicé muchas busquedas y la mayoría lo hace como Json externo (pasando la URL) pero yo lo realizo enviandolo desde el Controlador pero sin éxito.