{"id":1220,"date":"2022-05-08T16:14:45","date_gmt":"2022-05-08T14:14:45","guid":{"rendered":"http:\/\/www.joanillo.org\/?p=1220"},"modified":"2022-05-12T12:29:40","modified_gmt":"2022-05-12T10:29:40","slug":"openlayers-point-linestring-multilinestring-icon-ejemplo-canonico","status":"publish","type":"post","link":"https:\/\/www.joanillo.org\/?p=1220&lang=es","title":{"rendered":"Openlayers: Point, LineString, MultiLineString, Icon. Ejemplo can\u00f3nico"},"content":{"rendered":"<p><a href=\"http:\/\/www.joanillo.org\/wp-content\/uploads\/2022\/05\/exemple_punts_linies_icones.png\"><img src=\"http:\/\/www.joanillo.org\/wp-content\/uploads\/2022\/05\/exemple_punts_linies_icones.png\" alt=\"Ejemplo can\u00f3nico Openlayers\" title=\"Ejemplo can\u00f3nico Openlayers\" width=\"200\" class=\"alignright size-medium wp-image-2338\"><\/a>Vamos a mostrar un ejemplo m\u00ednimo de <b>Openlayers<\/b> que aclare los conceptos de dibujar un punto, una l\u00ednea, una multil\u00ednea y un icono. Y c\u00f3mo aplicar estilos a estos elementos.<\/p>\n<p>Definimos primero estos elementos (puntos, l\u00edneas, multil\u00edneas, iconos) a partir de los datos de ejemplo (coordenadas). Entonces definimos <em>features<\/em> a partir de estos elementos, que aa\u00f1adimos a un array de <em>features<\/em>. Cada <em>feature<\/em> puede tener su estilo.<\/p>\n<p>Definimos despu\u00e9s el <em>vectorSource<\/em> a partir del array de <em>features<\/em>. Despu\u00e9s definimos el <em>VectorLayer<\/em> a partir del <em>vectorSource<\/em>. Y finalmente a\u00f1adimos al mapa los <em>layers<\/em> que queremos pintar.<\/p>\n<p>Este es el codi inicial y previo para un peque\u00f1o proyecto que quiero hacer, que se trata de una aplicaci\u00f3n m\u00f2vil para enviar las coordenadas de posici\u00f3n a una RestAPI donde hay una base de datos (MongoDB), y con una aplicaci\u00f3n web (front-end, que es la part que aqu\u00ed se empieza a explicar) poder visualizar el recorrido del ciclista (la idea del proyecto es poder hacer el tracking de un ciclista en una primera etapa; y en una etapa posterior poder hacer el tracking de todos los ciclistas que participan en una carrera o salida).<\/p>\n<p>El c\u00f3digo final:<br \/>\n<code><br \/>\nimport 'ol\/ol.css';<br \/>\nimport Feature from 'ol\/Feature';<br \/>\nimport Map from 'ol\/Map';<br \/>\nimport VectorSource from 'ol\/source\/Vector';<br \/>\nimport View from 'ol\/View';<br \/>\nimport {Icon, Circle as CircleStyle, Fill, Stroke, Style} from 'ol\/style';<br \/>\nimport {LineString, MultiLineString, Point} from 'ol\/geom';<br \/>\nimport {getVectorContext} from 'ol\/render';<br \/>\nimport {fromLonLat} from 'ol\/proj';<br \/>\nimport {Tile as TileLayer, Vector as VectorLayer} from 'ol\/layer';<br \/>\nimport OSM from 'ol\/source\/OSM';<\/code><\/p>\n<p><code>\/\/dades<br \/>\nvar json_points = [<br \/>\n{'lon':1.4234,'lat':41.2344,'name':'punt 0'},<br \/>\n{'lon':1.4235,'lat':41.2345,'name':'punt 1'},<br \/>\n{'lon':1.4235,'lat':41.2346,'name':'punt 2'},<br \/>\n{'lon':1.4236,'lat':41.2346,'name':'punt 3'},<br \/>\n{'lon':1.4236,'lat':41.2347,'name':'punt 4'},<br \/>\n{'lon':1.4238,'lat':41.2349,'name':'punt 4'},<br \/>\n{'lon':1.4239,'lat':41.2350,'name':'punt 4'}<br \/>\n];<\/code><\/p>\n<p><code>\/\/ estils<br \/>\nvar styles = {<br \/>\n'Point': new Style({<br \/>\nimage: new CircleStyle({<br \/>\nfill: new Fill({<br \/>\ncolor: 'rgba(100,100,100,.8)',<br \/>\n}),<br \/>\nradius: 3,<br \/>\nstroke: new Stroke({<br \/>\ncolor: '#000000',<br \/>\nwidth: 2,<br \/>\n}),<br \/>\n}),<br \/>\n}),<br \/>\n'LineString': new Style({<br \/>\nstroke: new Stroke({<br \/>\ncolor: '#f00',<br \/>\nwidth: 3,<br \/>\n}),<br \/>\n}),<br \/>\n'MultiLineString': new Style({<br \/>\nstroke: new Stroke({<br \/>\ncolor: '#000000',<br \/>\nwidth: .8,<br \/>\n}),<br \/>\n}),<br \/>\n'icona': new Style({<br \/>\nimage: new Icon({<br \/>\nanchor: [0.5,20],<br \/>\nanchorXUnits: 'fraction',<br \/>\nanchorYUnits: 'pixels',<br \/>\nsrc: 'img\/circle_red_8.png'<br \/>\n})<br \/>\n})<br \/>\n};<\/code><\/p>\n<p><code>\/\/ points<br \/>\nvar features_points = new Array();<\/code><\/p>\n<p><code>var feature_point0 = new Feature({<br \/>\n'geometry': new Point(fromLonLat([json_points[0].lon,json_points[0].lat])),<br \/>\n'name': json_points[0].name,<br \/>\n});<\/code><\/p>\n<p><code>var feature_point1 = new Feature({<br \/>\n'geometry': new Point(fromLonLat([json_points[1].lon,json_points[1].lat])),<br \/>\n'name': json_points[0].name,<br \/>\n});<\/code><\/p>\n<p><code>var feature_point2 = new Feature({<br \/>\n'geometry': new Point(fromLonLat([json_points[2].lon,json_points[2].lat])),<br \/>\n'name': json_points[0].name,<br \/>\n});<\/code><\/p>\n<p><code>var feature_point3 = new Feature({<br \/>\n'geometry': new Point(fromLonLat([json_points[3].lon,json_points[3].lat])),<br \/>\n'name': json_points[0].name,<br \/>\n});<\/code><\/p>\n<p><code>var feature_point4 = new Feature({<br \/>\n'geometry': new Point(fromLonLat([json_points[4].lon,json_points[4].lat])),<br \/>\n'name': json_points[0].name,<br \/>\n});<\/code><\/p>\n<p><code>features_points.push(feature_point0)<br \/>\nfeatures_points.push(feature_point1)<br \/>\nfeatures_points.push(feature_point2)<br \/>\nfeatures_points.push(feature_point3)<br \/>\nfeatures_points.push(feature_point4)<\/p>\n<p>\/\/ l\u00ednies<br \/>\n\/\/Create a feature and add geometry as a thing<br \/>\nvar track1 = new MultiLineString([<br \/>\n[[json_points[0].lon,json_points[0].lat],[json_points[1].lon,json_points[1].lat]],<br \/>\n[[json_points[1].lon,json_points[1].lat],[json_points[2].lon,json_points[2].lat]],<br \/>\n[[json_points[2].lon,json_points[2].lat],[json_points[3].lon,json_points[3].lat]],<br \/>\n[[json_points[3].lon,json_points[3].lat],[json_points[4].lon,json_points[4].lat]]<br \/>\n]).transform('EPSG:4326','EPSG:3857');<\/p>\n<p>var feature_multilinestring = new Feature({<br \/>\ngeometry: track1,<br \/>\nname: 'nom track 1'<br \/>\n});<\/p>\n<p>var line1 = new LineString([<br \/>\n[json_points[5].lon,json_points[5].lat],[json_points[6].lon,json_points[6].lat]<br \/>\n]).transform('EPSG:4326','EPSG:3857');<\/p>\n<p>var feature_linestring = new Feature({<br \/>\ngeometry: line1,<br \/>\nname: 'nom l\u00ednia 1'<br \/>\n});<\/p>\n<p>var features_strings = new Array();<\/p>\n<p>features_strings.push(feature_multilinestring)<br \/>\nfeatures_strings.push(feature_linestring)<\/p>\n<p>\/\/icones<br \/>\nvar features_icones = new Array();<\/p>\n<p>let feature_icona1 = new Feature({<br \/>\ngeometry: new Point(fromLonLat([json_points[5].lon,json_points[5].lat])),<br \/>\nname: 'Inici',<br \/>\n});<\/p>\n<p>let feature_icona2 = new Feature({<br \/>\ngeometry: new Point(fromLonLat([json_points[6].lon,json_points[6].lat])),<br \/>\nname: 'Final',<br \/>\n});<\/p>\n<p>feature_icona1.setStyle(styles.icona); \/\/no cal si defineixo el estil en el layer.<br \/>\nfeature_icona2.setStyle(styles.icona); \/\/per\u00f2 definir\u00e9 l'estil en la icona si cada icona ha de tenir un estil diferent<\/p>\n<p>features_icones.push(feature_icona1);<br \/>\nfeatures_icones.push(feature_icona2);<\/p>\n<p>\/\/ vectorSources<br \/>\nvar vectorSourcePoints = new VectorSource({<br \/>\nfeatures: features_points,<br \/>\nwrapX: false,<br \/>\n});<\/p>\n<p>var vectorSourceStrings = new VectorSource({<br \/>\nfeatures: features_strings,<br \/>\nwrapX: false,<br \/>\n});<\/p>\n<p>var vectorSourceIcones = new VectorSource({<br \/>\nfeatures: features_icones,<br \/>\nwrapX: false,<br \/>\n});<\/p>\n<p>\/\/ layers<br \/>\nvar layerPoints = new VectorLayer({<br \/>\nsource: vectorSourcePoints,<br \/>\nstyle: styles.Point,<br \/>\n});<\/p>\n<p>var layerStrings = new VectorLayer({<br \/>\nsource: vectorSourceStrings,<br \/>\nstyle: styles.MultiLineString,<br \/>\n});<\/p>\n<p>var layerIcones = new VectorLayer({<br \/>\nsource: vectorSourceIcones,<br \/>\nstyle: styles.icona,<br \/>\n});<\/p>\n<p><\/code><code><\/code><code><\/code><code><\/code><code><\/code><code><\/code><code><\/code><code><\/code><code><\/code><code>var map = new Map({<br \/>\nlayers: [<br \/>\nnew TileLayer({<br \/>\nsource: new OSM({<br \/>\nlayer: 'OSM'<br \/>\n})<br \/>\n}),<br \/>\nlayerPoints,<br \/>\nlayerStrings,<br \/>\nlayerIcones<br \/>\n],<br \/>\ntarget: 'map',<br \/>\nview: new View({<br \/>\ncenter: fromLonLat([1.4234, 41.2344]),<br \/>\nzoom: 18<br \/>\n})<br \/>\n});<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vamos a mostrar un ejemplo m\u00ednimo de Openlayers que aclare los conceptos de dibujar un punto, una l\u00ednea, una multil\u00ednea y un icono. Y c\u00f3mo aplicar estilos a estos elementos. Definimos primero estos elementos (puntos, l\u00edneas, multil\u00edneas, iconos) a partir de los datos de ejemplo (coordenadas). Entonces definimos features a partir de estos elementos, que [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[324,320,340,192,539],"tags":[],"_links":{"self":[{"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/posts\/1220"}],"collection":[{"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1220"}],"version-history":[{"count":2,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/posts\/1220\/revisions"}],"predecessor-version":[{"id":1238,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/posts\/1220\/revisions\/1238"}],"wp:attachment":[{"href":"https:\/\/www.joanillo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}