草庐IT

java - 在 Primefaces 中使用谷歌地图库自动完成地址

coder 2024-03-21 原文

我正在使用 Primefaces 3.4 、Apache Tomcat 7 和 Java EE。我读了GoogleMaps API但我无法使用此功能。

我有一个 PrimeFaces 输入框,当我在上面写一个地址时,我想建议 GoogleMaps 库提供的其他地址。

将 JavaScript 代码放入我的 XHTML 时没有任何反应。我也有 GoogleMaps 的图书馆。

这是我的 XHTML:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<script type="text/javascript"
    src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">
    function initia(domicilioDesde, domicilioHasta) {

        var directionsDisplay;
        var directionsService = new google.maps.DirectionsService();
        var map;
        var oldDirections = [];
        var currentDirections = null;
        var cordoba = " ,Cordoba, Argentina";
        var direccion = domicilioDesde; /* '#{busquedaplayaMB.direccionDesde}'; */
        var start = direccion.concat(cordoba);
        var fin = domicilioHasta; /*'#{busquedaplayaMB.playaselected.domicilio}';*/
        var end = fin.concat(cordoba);
        var request = {
            origin : start,
            destination : end,
            travelMode : google.maps.DirectionsTravelMode.DRIVING
        }
        var myOptions = {
            zoom : 13,
            center : new google.maps.LatLng(#{busquedaplayaMB.latitudCentro},#{busquedaplayaMB.longitudCentro}),
            mapTypeId : google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"),
                myOptions);

        directionsDisplay = new google.maps.DirectionsRenderer({
            'map' : map,
            'preserveViewport' : true,
            'draggable' : true
        });
        directionsDisplay.setPanel(document
                .getElementById("directions_panel"));

        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });

        dlg2.show();
    }

    function undo() {
        currentDirections = null;
        directionsDisplay.setDirections(oldDirections.pop());
        if (!oldDirections.length) {
            setUndoDisabled(true);
        }
    }

    function setUndoDisabled(value) {
        document.getElementById("undo").disabled = value;
    }
</script>

<h:form id="form">
    <p:growl id="messages" showSummary="false" autoUpdate="true"
        globalOnly="true" />

    <div align="left" style="margin: 10px 0 10px 0;">
        <p:panel>
            <h:panelGrid columns="1" width="100%" cellspacing="5"
                cellpadding="5">
                <h:panelGrid columns="2" width="80%">
                    <p:column style="vertical-align:text-center;">
                        <h:panelGrid columns="2">
                            <p:column>
                                <p:watermark for="direccionBusqueda"
                                    value="Dirección de búsqueda" />
                                <p:inputText id="direccionBusqueda"
                                    value="#{busquedaplayaMB.direccionBusqueda}" required="true"
                                    requiredMessage="Campo dirección de búsqueda obligatorio"
                                    style="width:250px;">
                                    <p:ajax event="blur" update="direccionBusquedaMsg" />
                                </p:inputText>
                            </p:column>
                            <p:column>
                                <p:message id="direccionBusquedaMsg" display="icon"
                                    for="direccionBusqueda" />
                            </p:column>
                        </h:panelGrid>
                    </p:column>
                    <p:column>
                        <div align="center">
                            <h:outputLabel value="N° de cuadras: " />
                            <h:outputLabel id="nroCuadras"
                                value="#{busquedaplayaMB.distancia}" />
                            <h:inputHidden id="distancia"
                                value="#{busquedaplayaMB.distancia}" />
                            <p:slider for="distancia" minValue="1" maxValue="50"
                                style="width:100px;" update="nroCuadras" display="nroCuadras" />
                        </div>
                    </p:column>
                </h:panelGrid>

                <h:panelGrid columns="5" cellspacing="5" cellpadding="5"
                    width="100%">
                    <p:column style="vertical-align:text-center;">
                        <p:selectOneMenu value="#{busquedaplayaMB.categoriaParameter}"
                            effect="fade" style="width:160px;height:25px;line-height:17px;">
                            <f:selectItem itemLabel="Todas las categorías"
                                itemValue="#{null}" />
                            <f:selectItems
                                value="#{categoriaVehiculoMB.categoriaVehiculoList}"
                                var="categoria" itemValue="#{categoria}"
                                itemLabel="#{categoria.nombre}" />
                            <f:converter converterId="categoriaVehiculoConverter" />
                        </p:selectOneMenu>
                    </p:column>

                    <p:column style="vertical-align:text-center;">
                        <p:selectOneMenu value="#{busquedaplayaMB.tipoEstadiaParameter}"
                            effect="fade" style="width:160px;height:25px;line-height:17px;">
                            <f:selectItem itemLabel="Todas los tipos estadías"
                                itemValue="#{null}" />
                            <f:selectItems value="#{tipoEstadiaMB.tipoEstadiaList}"
                                var="tipoEstadia" itemValue="#{tipoEstadia}"
                                itemLabel="#{tipoEstadia.nombre}" />
                            <f:converter converterId="tipoEstadiaConverter" />
                        </p:selectOneMenu>
                    </p:column>

                    <p:column style="vertical-align:text-center;">
                        <div align="right">
                            <h:panelGrid columns="2">
                                <p:selectBooleanCheckbox id="check"
                                    value="#{busquedaplayaMB.checkPromociones}" />
                                <p:outputLabel for="check" value="Sólo con promociones" />
                            </h:panelGrid>
                        </div>
                    </p:column>
                    <p:column>
                        <div align="right">
                            <p:commandButton id="btnBuscarAvanzado" update="playas,mapa"
                                value="Buscar" action="#{busquedaplayaMB.busquedaAvanzada}"
                                ajax="false" icon="ui-icon-search" style="width:85px;" />
                        </div>
                    </p:column>
                </h:panelGrid>
            </h:panelGrid>
        </p:panel>
    </div>



    <div>
        <p:panel>
            <f:view contentType="text/html">
                <!--                <h1>Playas encontradas:</h1> -->
                <script src="http://maps.google.com/maps/api/js?sensor=false"
                    type="text/javascript"></script>

                <p:gmap center="#{busquedaplayaMB.coordenadas}" zoom="14"
                    type="ROADMAP" model="#{busquedaplayaMB.advancedModel}"
                    style="width:99,5%; height:350px;">

                    <p:ajax event="overlaySelect"
                        listener="#{busquedaplayaMB.onMarkerSelect}" />

                    <p:gmapInfoWindow>

                        <ui:fragment
                            rendered="#{busquedaplayaMB.marker.data.playa == null}">
                            <div align="center">
                                <ui:fragment rendered="#{busquedaplayaMB.usuario == null}">
                                    <h:outputText style="font-weight:bold;" value="¡Usted está aquí!" />
                                </ui:fragment>

                                <ui:fragment rendered="#{busquedaplayaMB.usuario != null}">
                                    <h:panelGrid column="1" style="text-align:center;"
                                        cellspacing="0px" cellpadding="0px">

                                        <p:column>
                                            <h:graphicImage library="fotos_perfil_usuarios"
                                                name="sinfoto.jpg"
                                                styleClass="bordes-foto-perfil-comentario"
                                                rendered="#{busquedaplayaMB.usuario.fotoUsuario == null}" />
                                            <h:graphicImage library="fotos_perfil_usuarios"
                                                name="#{busquedaplayaMB.usuario.nombreUser}.jpg"
                                                styleClass="bordes-foto-perfil-comentario"
                                                rendered="#{busquedaplayaMB.usuario.fotoUsuario != null}" />
                                        </p:column>
                                        <p:column>
                                            <h:outputText style="font-weight:bold;"
                                                value="#{busquedaplayaMB.usuario.nombre} #{busquedaplayaMB.usuario.apellido}" />
                                        </p:column>
                                        <p:column>
                                            <h:link id="linkUsuario" tittle="Ir a mi perfil"
                                                value="Ir a mi perfil" outcome="/cliente/perfilcliente" />
                                        </p:column>

                                    </h:panelGrid>
                                </ui:fragment>

                            </div>
                        </ui:fragment>

                        <ui:fragment
                            rendered="#{busquedaplayaMB.marker.data.playa != null}">
                            <div align="center">
                                <h:panelGrid column="1" style="text-align:center;"
                                    cellspacing="0px" cellpadding="0px">
                                    <p:column>
                                        <h:graphicImage library="fotos_perfil_playas"
                                            name="#{busquedaplayaMB.marker.data.id}_#{busquedaplayaMB.marker.data.nombreFoto}"
                                            styleClass="bordes-foto-perfil-comentario"
                                            rendered="#{busquedaplayaMB.marker.data.nombreFoto != null}" />
                                        <h:graphicImage library="fotos_perfil_playas"
                                            name="sinfoto.jpg"
                                            styleClass="bordes-foto-perfil-comentario"
                                            rendered="#{busquedaplayaMB.marker.data.nombreFoto == null}" />
                                    </p:column>
                                    <h:outputText style="font-weight:bold;"
                                        value="#{busquedaplayaMB.marker.data.playa.nombreComercial}" />

                                    <h:outputText
                                        value="#{busquedaplayaMB.marker.data.playa.domicilio} - #{busquedaplayaMB.marker.data.playa.barrio.nombre}" />

                                    <h:link id="link" tittle="Ir a playa" value="Ver información"
                                        outcome="/viewperfilplaya.html?id=#{busquedaplayaMB.marker.data.playa.id}" />

                                </h:panelGrid>
                            </div>
                        </ui:fragment>
                    </p:gmapInfoWindow>
                </p:gmap>
            </f:view>
        </p:panel>
    </div>

    <div style="margin: 5px;">
        <p:dataTable id="playas" var="playa" paginator="true"
            paginatorPosition="bottom" rows="5"
            emptyMessage="¡No existen playas!"
            value="#{busquedaplayaMB.playaResultadoBusqueda}">

            <p:column headerText="Nombre Comercial" styleClass="column-font">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{playa.nombreComercial}" />
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{playa.nombreComercial}"
                            styleClass="input-font" />
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="Barrio" styleClass="column-font">
                <h:outputText value="#{playa.barrio.nombre}" />
            </p:column>

            <p:column headerText="Domicilio" styleClass="column-font">
                <h:outputText value="#{playa.domicilio}" />
            </p:column>

            <p:column headerText="Perfil" style="text-align:center; width:50px;">
                <h:link id="verPerfil" title="Ver perfil"
                    outcome="/viewperfilplaya.html?id=#{playa.id}">
                    <h:graphicImage library="images/icons" name="go.png" />
                </h:link>
            </p:column>
            <p:column headerText="Ruta" style="text-align:center; width:50px;">
                <p:commandLink id="view2" oncomplete="dlgOrigen.show();"
                    title="¿Cómo llegar a esta playa?" update=":dlgO" process="@this">
                    <f:setPropertyActionListener value="#{playa}"
                        target="#{busquedaplayaMB.playaselected}" />
                    <h:graphicImage library="images/icons" name="search-map.png"
                        style="height:40px; width:40px" />
                </p:commandLink>
            </p:column>
        </p:dataTable>
    </div>
    <!--    </h:form> -->

    <p:dialog widgetVar="dlg2" width="800" height="400" modal="true"
        id="dialog" draggable="false" closable="true">

        <!--        <h:form id="frmComoLlegar"> -->
        <f:facet name="header">
            <h:outputText value="¿Cómo llegar a la playa?" />
        </f:facet>
        <div id="map_canvas" style="float: left; width: 65%; height: 100%"></div>
        <div style="float: right; width: 35%; height: 100%; overflow: auto">
            <div id="directions_panel" style="width: 100%"></div>
        </div>
        <f:facet name="footer" style="text-align=right;">
            <p:commandButton id="undo" value="Volver"
                style="float:right; width:274px; height: 42px" onclick="undo();"></p:commandButton>
        </f:facet>
    </p:dialog>
</h:form>

<p:dialog header="Dirección de origen" id="dlgO" widgetVar="dlgOrigen"
    resizable="false" closable="true">
    <h:form id="frmComoLlegar">

        <h:panelGrid columns="2" cellspacing="10" cellpadding="10">
            <h:outputLabel for="dirOrigen" value="Dirección de origen: " />
            <p:inputText id="dirOrigen" required="true"
                value="#{busquedaplayaMB.direccionDesde}"
                onkeyup="if (event.keyCode == 13) { document.getElementById(':frmComoLlegar:siguiente').click(); return false; }" />
            <p:column />
            <p:column>
                <div align="right">
                    <p:commandButton id="siguiente" value="Siguiente" ajax="true"
                        onclick="initia(jQuery('#frmComoLlegar\\:dirOrigen').val(), '#{busquedaplayaMB.playaselected}');"
                        update=":dialog" process="@this"
                        action="#{busquedaplayaMB.tomarDomicilioDesde}" />
                </div>
            </p:column>
        </h:panelGrid>
    </h:form>
</p:dialog>

最佳答案

我已经使用 GMap3 实现了这个功能jQuery 插件(也与 JSF + Primefaces 结合使用)。看这个例子:https://github.com/jbdemonte/gmap3/blob/master/examples/autocomplete/autocomplete.html

关于java - 在 Primefaces 中使用谷歌地图库自动完成地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17645616/

有关java - 在 Primefaces 中使用谷歌地图库自动完成地址的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  5. ruby - 在 Ruby 中使用匿名模块 - 2

    假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于

  6. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  7. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  8. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  9. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  10. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

随机推荐