Java + GMaps JSF + PostGis + JasonSanford / geojson-google-maps

88 views
Skip to first unread message

Cassia Freitas

unread,
Feb 26, 2014, 7:45:08 AM2/26/14
to gmaps4...@googlegroups.com
Guys want to test how to plot lines and points in google maps using Java + JSF + PostGIS + GMaps JasonSanford / GeoJSON-google-maps. First have a spaceport in postgis with a table of rows and other points, after searching I thought it best to bring these geometries into a GeoJSON bank using ST_AsGeoJSON postgis function. The GeoJSON-google-maps do the job of converting the GeoJSON objects to google maps, specification: https://github.com/JasonSanford/geojson-google-maps But in practice I use this utility in java? Look GeoJSON.js put the script in the root of my project and in my head where I only index show any two points. 

How do I pass to my postgis GeoJSON returned by the script and return objects from google maps to my index?

>     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html
>       xmlns:h="http://java.sun.com/jsf/html"
>       xmlns:f="http://java.sun.com/jsf/core"
>       xmlns:ui="http://java.sun.com/jsf/facelets"
>     <h:head>  
>         <title> Facelet Title </title>  
>         <script type="text/javascript"  
>                 src="https://maps.google.com/maps/api/js?sensor=true">  
>         </script>         
>         <!--         Script GeoJson  salvo na raíz do projeto-->
>         <script type="text/javascript" src="GeoJSON.js"> </script>
>     </h:head>  
>     <h:body>  
>         <h1 align="center"> GMaps4JSF</h1>  
>     <!--O mapa tem que estar dentro de um form -->
>         <h:form id="form">  
>             <h:panelGroup id="map">  
>      <!--Desenhando um mapa com pontos e balão de informação -->
>                 <m:map width="1000px" height="700px" latitude="30.01" longitude="51.00" enableScrollWheelZoom="true">  
>                     <m:marker latitude= "-30.038337"    longitude=    "-51.229502"/>  
>                     <m:htmlInformationWindow latitude= "-30.038337"    longitude=    "-51.229502" htmlText="Point 1"/>
>                     <m:marker latitude=    "-30.038246" longitude=    "-51.229631"/>  
>                     <m:htmlInformationWindow latitude="-30.038246"    longitude="-51.229631" htmlText="Point 2"/>
>                 </m:map> 
>             </h:panelGroup>


This script Geojson.js


>     var GeoJSON = function( geojson, options ){
>     var _geometryToGoogleMaps = function( geojsonGeometry, options, geojsonProperties ){
>         var googleObj, opts = _copy(options);
>         switch ( geojsonGeometry.type ){
>             case "Point":
>                 opts.position = new google.maps.LatLng(geojsonGeometry.coordinates[1],
> geojsonGeometry.coordinates[0]);
>                 googleObj = new google.maps.Marker(opts);
>                 if (geojsonProperties) {
>                     googleObj.set("geojsonProperties", geojsonProperties);
>                 }
>                 break;
>             case "MultiPoint":
>                 googleObj = [];
>                 for (var i = 0; i < geojsonGeometry.coordinates.length; i++){
>                     opts.position = new google.maps.LatLng(geojsonGeometry.coordinates[i][1],
> geojsonGeometry.coordinates[i][0]);
>                     googleObj.push(new google.maps.Marker(opts));
>                 }
>                 if (geojsonProperties) {
>                     for (var k = 0; k < googleObj.length; k++){
>                         googleObj[k].set("geojsonProperties", geojsonProperties);
>                     }
>                 }
>                 break;
>             case "LineString":
>                 var path = [];
>                 for (var i = 0; i < geojsonGeometry.coordinates.length; i++){
>                     var coord = geojsonGeometry.coordinates[i];
>                     var ll = new google.maps.LatLng(coord[1], coord[0]);
>                     path.push(ll);
>                 }
>                 opts.path = path;
>                 googleObj = new google.maps.Polyline(opts);
>                 if (geojsonProperties) {
>                     googleObj.set("geojsonProperties", geojsonProperties);
>                 }
>                 break;
>             case "MultiLineString":
>                 googleObj = [];
>                 for (var i = 0; i < geojsonGeometry.coordinates.length; i++){
>                     var path = [];
>                     for (var j = 0; j < geojsonGeometry.coordinates[i].length; j++){
>                         var coord = geojsonGeometry.coordinates[i][j];
>                         var ll = new google.maps.LatLng(coord[1], coord[0]);
>                         path.push(ll);
>                     }
>                     opts.path = path;
>                     googleObj.push(new google.maps.Polyline(opts));
>                 }
>                 if (geojsonProperties) {
>                     for (var k = 0; k < googleObj.length; k++){
>                         googleObj[k].set("geojsonProperties", geojsonProperties);
>                     }
>                 }
>                 break;
>             case "Polygon":
>                 var paths = [];
>                 var exteriorDirection;
>                 var interiorDirection;
>                 for (var i = 0; i < geojsonGeometry.coordinates.length; i++){
>                     var path = [];
>                     for (var j = 0; j < geojsonGeometry.coordinates[i].length; j++){
>                         var ll = new google.maps.LatLng(geojsonGeometry.coordinates[i][j][1],
> geojsonGeometry.coordinates[i][j][0]);
>                         path.push(ll);
>                     }
>                     if(!i){
>                         exteriorDirection = _ccw(path);
>                         paths.push(path);
>                     }else if(i == 1){
>                         interiorDirection = _ccw(path);
>                         if(exteriorDirection == interiorDirection){
>                             paths.push(path.reverse());
>                         }else{
>                             paths.push(path);
>                         }
>                     }else{
>                         if(exteriorDirection == interiorDirection){
>                             paths.push(path.reverse());
>                         }else{
>                             paths.push(path);
>                         }
>                     }
>                 }
>                 opts.paths = paths;
>                 googleObj = new google.maps.Polygon(opts);
>                 if (geojsonProperties) {
>                     googleObj.set("geojsonProperties", geojsonProperties);
>                 }
>                 break;
>             case "MultiPolygon":
>                 googleObj = [];
>                 for (var i = 0; i < geojsonGeometry.coordinates.length; i++){
>                     var paths = [];
>                     var exteriorDirection;
>                     var interiorDirection;
>                     for (var j = 0; j < geojsonGeometry.coordinates[i].length; j++){
>                         var path = [];
>                         for (var k = 0; k < geojsonGeometry.coordinates[i][j].length; k++){
>                             var ll = new google.maps.LatLng(geojsonGeometry.coordinates[i][j][k][1],
> geojsonGeometry.coordinates[i][j][k][0]);
>                             path.push(ll);
>                         }
>                         if(!j){
>                             exteriorDirection = _ccw(path);
>                             paths.push(path);
>                         }else if(j == 1){
>                             interiorDirection = _ccw(path);
>                             if(exteriorDirection == interiorDirection){
>                                 paths.push(path.reverse());
>                             }else{
>                                 paths.push(path);
>                             }
>                         }else{
>                             if(exteriorDirection == interiorDirection){
>                                 paths.push(path.reverse());
>                             }else{
>                                 paths.push(path);
>                             }
>                         }
>                     }
>                     opts.paths = paths;
>                     googleObj.push(new google.maps.Polygon(opts));
>                 }
>                 if (geojsonProperties) {
>                     for (var k = 0; k < googleObj.length; k++){
>                         googleObj[k].set("geojsonProperties", geojsonProperties);
>                     }
>                 }
>                 break;
>             case "GeometryCollection":
>                 googleObj = [];
>                 if (!geojsonGeometry.geometries){
>                     googleObj = _error("Invalid GeoJSON object: GeometryCollection object missing \"geometries\" member.");
>                 }else{
>                     for (var i = 0; i < geojsonGeometry.geometries.length; i++){
>                         googleObj.push(_geometryToGoogleMaps(geojsonGeometry.geometries[i],
> opts, geojsonProperties || null));
>                     }
>                 }
>                 break;
>             default:
>                 googleObj = _error("Invalid GeoJSON object: Geometry object must be one of \"Point\", \"LineString\", \"Polygon\" or
> \"MultiPolygon\".");
>         }
>         return googleObj;
>     };
>     var _error = function( message ){
>         return {
>             type: "Error",
>             message: message
>         };
>     };
>     var _ccw = function( path ){
>         var isCCW;
>         var a = 0;
>         for (var i = 0; i < path.length-2; i++){
>             a += ((path[i+1].lat() - path[i].lat()) * (path[i+2].lng() - path[i].lng()) - (path[i+2].lat() - path[i].lat()) * (path[i+1].lng() - path[i].lng()));
>         }
>         if(a > 0){
>             isCCW = true;
>         }
>         else{
>             isCCW = false;
>         }
>         return isCCW;
>     };
>   var _copy = function(obj){
>     var newObj = {};
>     for(var i in obj){
>       if(obj.hasOwnProperty(i)){
>         newObj[i] = obj[i];
>       }
>     }
>     return newObj;   };
>     var obj;
>     var opts = options || {};
>     switch ( geojson.type ){
>         case "FeatureCollection":
>             if (!geojson.features){
>                 obj = _error("Invalid GeoJSON object: FeatureCollection object missing \"features\" member.");
>             }else{
>                 obj = [];
>                 for (var i = 0; i < geojson.features.length; i++){
>                     obj.push(_geometryToGoogleMaps(geojson.features[i].geometry, opts,
> geojson.features[i].properties));
>                 }
>             }
>             break;
>         case "GeometryCollection":
>             if (!geojson.geometries){
>                 obj = _error("Invalid GeoJSON object: GeometryCollection object missing \"geometries\" member.");
>             }else{
>                 obj = [];
>                 for (var i = 0; i < geojson.geometries.length; i++){
>                     obj.push(_geometryToGoogleMaps(geojson.geometries[i], opts));
>                 }
>             }
>             break;
>         case "Feature":
>             if (!( geojson.properties && geojson.geometry )){
>                 obj = _error("Invalid GeoJSON object: Feature object missing \"properties\" or \"geometry\" member.");
>             }else{
>                 obj = _geometryToGoogleMaps(geojson.geometry, opts, geojson.properties);
>             }
>             break;
>         case "Point": case "MultiPoint": case "LineString": case "MultiLineString": case "Polygon": case "MultiPolygon":
>             obj = geojson.coordinates
>                 ? obj = _geometryToGoogleMaps(geojson, opts)
>                 : _error("Invalid GeoJSON object: Geometry object missing \"coordinates\" member.");
>             break;
>         default:
>             obj = _error("Invalid GeoJSON object: GeoJSON object must be one of \"Point\", \"LineString\", \"Polygon\", \"MultiPolygon\",
> \"Feature\", \"FeatureCollection\" or \"GeometryCollection\".");
>     }
>     return obj;
> };
Reply all
Reply to author
Forward
0 new messages