2017-08-25 147 views
0

我遵循Primefaces展示地理編碼功能與p:gmap,如果我把一個真實的地址,沒關係,但如果我把一個無效的地址沒有任何反應,我只收到消息Chrome控制檯「找不到地理編碼」,但我必須趕上事件,以便我可以向用戶顯示自定義消息。Gmap Primefaces Geocode自定義錯誤消息

看得更深我在Primefaces庫裏找到了gmap.js,並且啓動了一個Primefaces.error(「找不到Geocode」),但是我仍然有這個問題,我怎麼能夠捕捉到這個問題?

進出口使用Primefaces 5.2

<p:gmap 
    widgetVar="w_gmap_edit" 
    id="gmapId_edit" 
    center="#{tiendasController.centerGeoMap}" 
    zoom="15" 
    type="ROADMAP" 
    style="width:400px;height:450px" 
    model="#{tiendasController.mapModel}" 
    onPointClick="handlePointClick_edit(event)" 
> 
    <p:ajax 
    event="geocode" 
    listener="#{tiendasController.onGeoCodeAction}" 
    onstart="onCompleteGeoCodeEdit(xhr, status, args)" 
    update="@this center-form:tiendas-editar-latitud-value center-form:tiendas-editar-longitud-value" 
    /> 
</p:gmap> 

以及JavaScript函數:

function geocode_edit() { 
    PF('w_gmap_edit').geocode(document.getElementById('center-form:tiendas-editar-direccion-value').value); 
} 

回答

0

你試試這個:

視圖元素

<a:row> <a:label value="" span="2"/> <p:commandButton value="#{bundleCommun.btn_recherche}" icon="ui-icon-search" onclick="geocode();" type="button" /> </a:row> <p:gmap id="geoGmap" widgetVar="geoMap" center="#{addHubController.centerGeoMap}" zoom="12" type="ROADMAP" model="#{addHubController.geoModel}" onPointClick="handlePointClick(event);" style="width:100%;height:400px"> <p:ajax event="geocode" listener="#{addHubController.onGeocode}" update="@this" /> </p:gmap>

腳本

<script> 
    function geocode() { 

     var map=PF('geoMap').getMap();//PERFECT 

     var address=document.getElementById('form:address').value;//PERFECT 

     var geocoder = new google.maps.Geocoder();//PERFECT 

     geocoder.geocode({'address': address}, function(results, status) { 

      if (status === 'OK') { 
       map.setCenter(results[0].geometry.location); 
       var marker = new google.maps.Marker({ 
        map: map, 
        position: results[0].geometry.location 
       }); 
      } else { 
       alert('CUSTOMIZED ERROR MESSAGE'); 
      } 
     }); 
</script> 

PRIMEFACES GMAP/GOOGLE MAP API, GEOCODE TUTORIAL

這是爲我工作非常精細。祝你好運

+0

感謝Mehdi的迴應,我嘗試了它,但總是執行else塊,無論地址是否正常,就好像函數總是返回false。 –

+0

@JorgeDominguez我找到了解決方案,我編輯了我的答案,並用上面的代碼捕獲了錯誤。祝你好運再次 –

+0

嗨@MehdiBouzidi,非常感謝,它的工作原理,唯一的事情就是p:ajax中的goecode事件不會執行,所以我必須在javascript中做一些調整,但現在我可以顯示一條消息,再次感謝。 –