/* Title: Geolocalizacao Abre a ferramenta que captura a posicao do usuario e mostra no mapa Veja: Arquivo: i3geo/ferramentas/geolocal/index.js.php Licenca: GPL2 i3Geo Interface Integrada de Ferramentas de Geoprocessamento para Internet Direitos Autorais Reservados (c) 2006 Ministério do Meio Ambiente Brasil Desenvolvedor: Edmar Moretti edmar.moretti@gmail.com Este programa é software livre; você pode redistribuí-lo e/ou modificá-lo sob os termos da Licença Pública Geral GNU conforme publicada pela Free Software Foundation; Este programa é distribuído na expectativa de que seja útil, porém, SEM NENHUMA GARANTIA; nem mesmo a garantia implícita de COMERCIABILIDADE OU ADEQUAÇÃO A UMA FINALIDADE ESPECÍFICA. Consulte a Licença Pública Geral do GNU para mais detalhes. Você deve ter recebido uma cópia da Licença Pública Geral do GNU junto com este programa; se não, escreva para a Free Software Foundation, Inc., no endereço 59 Temple Street, Suite 330, Boston, MA 02111-1307 USA. */ if (typeof (i3GEOF) === 'undefined') { var i3GEOF = {}; } /* * Classe: i3GEOF.geolocal */ i3GEOF.geolocal = { /* * Variavel: posicoes * * Objetos capturados */ posicoes : [], contador : 0, tempo : null, /* * Variavel: aguarde * * Estilo do objeto DOM com a imagem de aguarde existente no cabeçalho da janela. */ aguarde : "", /** * Template no formato mustache. E preenchido na carga do javascript com o programa dependencias.php */ MUSTACHE : "", MUSTACHELISTA : "", /** * Susbtitutos para o template */ mustacheHash : function() { var dicionario = i3GEO.idioma.objetoIdioma(i3GEOF.geolocal.dicionario); dicionario["locaplic"] = i3GEO.configura.locaplic; return dicionario; }, /* * Function: inicia * * Inicia a ferramenta. É chamado por criaJanelaFlutuante * * Parametro: * * iddiv {String} - id do div que receberá o conteudo HTML da ferramenta */ inicia : function(iddiv) { if(i3GEOF.geolocal.MUSTACHE == ""){ var t1 = i3GEO.configura.locaplic + "/ferramentas/geolocal/template_mst.html", t2 = i3GEO.configura.locaplic + "/ferramentas/geolocal/template_lista_mst.html"; $.when( $.get(t1),$.get(t2) ).done(function(r1,r2) { i3GEOF.geolocal.MUSTACHE = r1[0]; i3GEOF.geolocal.MUSTACHELISTA = r2[0]; i3GEOF.geolocal.inicia(iddiv); }).fail(function() { i3GEO.janela.closeMsg($trad("erroTpl")); return; }); return; } // se nao permitir a localizacao, retorna uma mensagem if (navigator.geolocation) { $i(iddiv).innerHTML = i3GEOF.geolocal.html(); $i("i3GEOFgeolocalFormTempo").onsubmit = i3GEOF.geolocal.capturaTempo; i3GEOF.geolocal.capturaCoordenada(); } else { $i(iddiv).innerHTML = $trad('msgNavegador', i3GEOF.geolocal.dicionario); } }, /* * Function: html * * Gera o código html para apresentação das opções da ferramenta * * Retorno: * * String com o código html */ html : function() { var ins = Mustache.render(i3GEOF.geolocal.MUSTACHE, i3GEOF.geolocal.mustacheHash()); return ins; }, /* * Function: iniciaJanelaFlutuante * * Cria a janela flutuante para controle da ferramenta. */ iniciaJanelaFlutuante : function() { var janela, divid, temp, titulo, cabecalho, minimiza; if ($i("i3GEOF.geolocal")) { return; } cabecalho = function() { }; minimiza = function() { i3GEO.janela.minimiza("i3GEOF.geolocal",200); }; // cria a janela flutuante titulo = "" + $trad('localizaUsuario',i3GEOF.geolocal.dicionario) + ""; janela = i3GEO.janela.cria( "410", "230", "", "", "", titulo, "i3GEOF.geolocal", false, "hd", cabecalho, minimiza, "", true, "", "", "", "", "118" ); divid = janela[2].id; $i("i3GEOF.geolocal_corpo").style.backgroundColor = "white"; $i("i3GEOF.geolocal_corpo").style.textAlign = "left"; i3GEOF.geolocal.aguarde = $i("i3GEOF.geolocal_imagemCabecalho").style; i3GEOF.geolocal.inicia(divid); temp = function() { var api; if (i3GEO.Interface["ATUAL"] === "openlayers") { if (typeof OpenLayers.Control == "undefined") { api = "ol3"; } else { api = "openlayers"; } } else { api = i3GEO.Interface["ATUAL"]; } i3GEOF.geolocal[api].removeLayer(); i3GEOF.geolocal.paraTempo(); }; YAHOO.util.Event.addListener(janela[0].close, "click", temp); }, capturaCoordenada : function() { if (i3GEOF.geolocal.aguarde.visibility == "visible") { return; } i3GEOF.geolocal.aguarde.visibility = "visible"; var retorno = function(position) { position.id = i3GEOF.geolocal.contador; i3GEOF.geolocal.posicoes[i3GEOF.geolocal.contador] = position; i3GEOF.geolocal.contador = i3GEOF.geolocal.contador + 1; var n = parseInt($i("i3GEOFgeolocalMaximo").value, 10); if (n > 0 && i3GEOF.geolocal.posicoes.length > n) { i3GEOF.geolocal.posicoes.splice(0, (i3GEOF.geolocal.posicoes.length - n)); } i3GEOF.geolocal.listaCoord(position); }; navigator.geolocation.getCurrentPosition(retorno, i3GEOF.geolocal.erro); }, capturaTempo : function() { var tempo = parseInt($i("i3GEOFgeolocalTempo").value, 10); if (tempo) { if (tempo > 0) { i3GEOF.geolocal.tempo = setInterval("i3GEOF.geolocal.capturaCoordenada()", tempo * 1000); } else { clearInterval(i3GEOF.geolocal.tempo); } } return false; }, paraTempo : function() { if ($i("i3GEOFgeolocalTempo")) { $i("i3GEOFgeolocalTempo").value = 0; } clearInterval(i3GEOF.geolocal.tempo); }, erro : function(error) { i3GEOF.geolocal.paraTempo(); var erro = ""; switch (error.code) { case error.PERMISSION_DENIED: erro = "User denied the request for Geolocation."; break; case error.POSITION_UNAVAILABLE: erro = "Location information is unavailable."; break; case error.TIMEOUT: erro = "The request to get user location timed out."; break; case error.UNKNOWN_ERROR: erro = "An unknown error occurred."; break; } i3GEO.janela.tempoMsg(erro); i3GEOF.geolocal.aguarde.visibility = "hidden"; }, listaCoord : function(position) { var ps = i3GEOF.geolocal.posicoes; var dados = { "limpa": $trad('limpa', i3GEOF.geolocal.dicionario), "locaplic": i3GEO.configura.locaplic, "longitude": position.coords.longitude, "latitude": position.coords.latitude, "id": position.id }; var ntr = document.createElement("tr"); ntr.id = "linhaGeolocal"+position.id; var temp = Mustache.render( "{{#data}}" + i3GEOF.geolocal.MUSTACHELISTA + "{{/data}}", {"data":dados} ); $(ntr).html(temp); var tabela = $i("i3GEOFgeolocalListaDePontos"); tabela.appendChild(ntr); $i("i3GEOFgeolocalNcoord").innerHTML = ps.length; if (i3GEO.Interface["ATUAL"] === "openlayers") { if (typeof OpenLayers.Control == "undefined") { api = "ol3"; } else { api = "openlayers"; } } else { api = i3GEO.Interface["ATUAL"]; } i3GEOF.geolocal[api].desenha(); i3GEOF.geolocal.aguarde.visibility = "hidden"; }, limpa : function() { i3GEOF.geolocal.posicoes = []; if (i3GEO.Interface["ATUAL"] === "openlayers") { if (typeof OpenLayers.Control == "undefined") { api = "ol3"; } else { api = "openlayers"; } } else { api = i3GEO.Interface["ATUAL"]; } i3GEOF.geolocal[api].removeLayer(); var tabela = $i("i3GEOFgeolocalListaDePontos"); var cabecalho = tabela.getElementsByTagName("tr"); tabela.innerHtml = ""; tabela.appendChild(cabecalho); }, excluiLinha : function(i) { i3GEOF.geolocal.posicoes[i] = ""; var linha = $i("linhaGeolocal"+i); linha.parentNode.removeChild(linha); }, panLinha : function(i) { var posicao = i3GEOF.geolocal.posicoes[i]; // @FIXME o pan nao funciona no OSM if (posicao != undefined && i3GEO.Interface.openlayers.googleLike != true) { i3GEO.navega.pan2ponto((posicao.coords.longitude), (posicao.coords.latitude)); } }, info : function(i) { i3GEO.mapa.dialogo.cliqueIdentificaDefault( i3GEOF.geolocal.posicoes[i].coords.longitude, i3GEOF.geolocal.posicoes[i].coords.latitude); }, mostraxy : function(i) { if (i3GEO.Interface.ATUAL === "googleearth") { return; } var posicao = i3GEOF.geolocal.posicoes[i]; i3GEO.desenho.addPin( posicao.coords.longitude, posicao.coords.latitude, "", "", i3GEO.configura.locaplic + '/imagens/google/confluence.png', "pingeolocal"); }, escondexy : function() { i3GEO.desenho.removePins("pingeolocal"); }, posicoes2pontos : function() { var ps = i3GEOF.geolocal.posicoes, n = ps.length, i, pontos = []; for (i = 0; i < n; i++) { pontos.push((ps[i].coords.longitude) + " " + ps[i].coords.latitude); } return pontos; }, criaShp : function() { function ativanovotema(retorno) { var pontos = i3GEOF.geolocal.posicoes2pontos(), temaNovo = retorno.data, converteParaLinha = function() { var cp = new cpaint(); cp.set_response_type("JSON"); var p = i3GEO.configura.locaplic + "/classesphp/mapa_controle.php?g_sid=" + i3GEO.configura.sid + "&funcao=sphPT2shp¶=linha&tema=" + temaNovo; cp.call(p, "sphPT2shp", i3GEO.atualiza); }; var p = window.parent.i3GEO.configura.locaplic + "/ferramentas/inserexy2/exec.php?g_sid=" + i3GEO.configura.sid + "&funcao=insereSHP&tema=" + retorno.data; var cp = new cpaint(); cp.set_response_type("JSON"); cp.set_transfer_mode('POST'); cp.call(p, "insereSHP", converteParaLinha, "&xy=" + pontos.join(" ")); } var cp = new cpaint(); cp.set_response_type("JSON"); cp.set_transfer_mode("POST"); var p = window.parent.i3GEO.configura.locaplic + "/classesphp/mapa_controle.php?g_sid=" + i3GEO.configura.sid; cp.call(p, "criaSHPvazio", ativanovotema, "&funcao=criashpvazio"); }, ol3 : { desenha : function() { if (!i3GEO.desenho.layergrafico) { i3GEO.desenho.openlayers.criaLayerGrafico(); } i3GEOF.geolocal.ol3.removeFiguras(); var ps = i3GEOF.geolocal.posicoes, n = ps.length, i, feature; for (i = 0; i < n; i++) { feature = new ol.Feature({ geometry : new ol.geom.Point([ ps[i].coords.longitude, ps[i].coords.latitude ]) }); feature.setProperties({ origem : "i3GEOFgeolocal" }); feature.setId(i3GEO.util.uid()); feature.setStyle( new ol.style.Style({ image: new ol.style.Circle({ radius: 6, fill: new ol.style.Fill({ color: 'red' }), stroke: new ol.style.Stroke({ color: 'white', width: 2 }) }) }) ); i3GEO.desenho.layergrafico.getSource().addFeature(feature); // linha if (ps[i + 1]) { feature = new ol.Feature({ geometry : new ol.geom.LineString([ [ ps[i].coords.longitude, ps[i].coords.latitude ], [ ps[i + 1].coords.longitude, ps[i + 1].coords.latitude ] ]) }); feature.setProperties({ origem : "i3GEOFgeolocal" }); feature.setStyle( new ol.style.Style({ stroke: new ol.style.Stroke({ color: 'white)', width: 2 }), fill: new ol.style.Fill({ color: 'red' }) }) ); feature.setId(i3GEO.util.uid()); i3GEO.desenho.layergrafico.getSource().addFeature(feature); } } }, removeFiguras : function() { var s, features, n, f, i, remover = []; s = i3GEO.desenho.layergrafico.getSource(); features = s.getFeatures(); n = features.length; for(i = 0; i < n; i++){ f = features[i]; if(f.getProperties().origem === "i3GEOFgeolocal"){ remover.push(f.getId()); } } n = remover.length; if(n > 0){ for(i = 0; i < n; i++){ f = s.getFeatureById(remover[i]); i3GEO.desenho.layergrafico.getSource().removeFeature(f); } } }, removeLayer : function() { var temp = window.confirm($trad("x94")); if (temp) { i3GEOF.geolocal.ol3.removeFiguras(); } } }, openlayers : { desenha : function() { // allow testing of specific renderers via "?renderer=Canvas", etc var renderer = OpenLayers.Util.getParameters(window.location.href).renderer, layer_style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']), style_blue = OpenLayers.Util.extend({}, layer_style), vectorLayer, ps = i3GEOF.geolocal.posicoes, n = ps.length, i, point, pointFeature = [], pointList = []; renderer = (renderer) ? [ renderer ] : OpenLayers.Layer.Vector.prototype.renderers; style_blue.strokeColor = "blue"; style_blue.fillColor = "yellow"; style_blue.graphicName = "square"; style_blue.pointRadius = 3; style_blue.strokeWidth = 2; style_blue.rotation = 0; style_blue.strokeLinecap = "butt"; if (i3geoOL.getLayersByName("Coordenadas").length === 0) { vectorLayer = new OpenLayers.Layer.Vector("Coordenadas", { style : layer_style, renderers : renderer }); } else { vectorLayer = i3geoOL.getLayersByName("Coordenadas")[0]; vectorLayer.removeFeatures(vectorLayer.features); } for (i = 0; i < n; i++) { point = new OpenLayers.Geometry.Point((ps[i].coords.longitude), (ps[i].coords.latitude)); i3GEOF.geolocal.wgs2google(point); pointList.push(point); pointFeature.push(new OpenLayers.Feature.Vector(point, null, style_blue)); } var lineFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(pointList), null, style_blue); i3geoOL.addLayer(vectorLayer); vectorLayer.addFeatures([ lineFeature ]); vectorLayer.addFeatures(pointFeature); i3GEOF.geolocal.panLinha(n - 1); }, removeLayer : function() { i3geoOL.removeLayer(i3geoOL.getLayersByName("Coordenadas")[0], false); } }, googlemaps : { linhas : null, marca : null, desenha : function() { var ps = i3GEOF.geolocal.posicoes, n = ps.length, i, l, pointFeature = []; for (i = 0; i < n; i++) { l = new google.maps.LatLng(ps[i].coords.latitude, ps[i].coords.longitude); pointFeature.push(l); } i3GEOF.geolocal.googlemaps.removeLayer(); i3GEOF.geolocal.googlemaps.marca = new google.maps.Marker({ position : new google.maps.LatLng(ps[0].coords.latitude, ps[0].coords.longitude), map : i3GeoMap }); i3GEOF.geolocal.googlemaps.linhas = new google.maps.Polyline({ path : pointFeature, geodesic : true, strokeColor : 'blue', strokeOpacity : 1.0, strokeWeight : 2, name : "Coordenadas" }); i3GEOF.geolocal.googlemaps.linhas.setMap(i3GeoMap); i3GEOF.geolocal.panLinha(n - 1); }, removeLayer : function() { if (i3GEOF.geolocal.googlemaps.linhas) { i3GEOF.geolocal.googlemaps.linhas.setMap(null); } if (i3GEOF.geolocal.googlemaps.marca) { i3GEOF.geolocal.googlemaps.marca.setMap(null); } } }, // @TODO implementar desenho dos pontos e linhas googleearth : { desenha : function() { }, removeLayer : function() { } }, wgs2google : function(obj) { if (i3GEO.Interface.openlayers.googleLike === true || i3GEO.Interface.ATUAL === "googlemaps") { var projWGS84 = new OpenLayers.Projection("EPSG:4326"), proj900913 = new OpenLayers.Projection("EPSG:900913"); return obj.transform(projWGS84, proj900913); } else { return obj; } } };