Geocoding support is provided with geocode and reverseGeocode ajax behavior events.
<script src="https://maps.google.com/maps/api/js?key=AIzaSyCvCDkYieuUBmMWon_mfLAfjuaeuosuqow&sensor=false"></script>
<script>
function geocode() {
PF('geoMap').geocode(document.getElementById('address').value);
}
function reverseGeocode() {
var lat = document.getElementById('lat').value,
lng = document.getElementById('lng').value;
PF('revGeoMap').reverseGeocode(lat, lng);
}
</script>
<h:form prependId="false">
<div class="card">
<h5 style="margin-top:0">Geocode</h5>
<h:panelGrid columns="3" style="margin-bottom:10px" cellpadding="5">
<p:outputLabel for="address" value="Address:"/>
<p:inputText id="address"/>
<p:commandButton value="Geocode" icon="pi pi-search" onclick="geocode()" type="button"/>
</h:panelGrid>
<p:gmap id="geoGmap" widgetVar="geoMap" center="#{geocodeView.centerGeoMap}" zoom="2" type="ROADMAP"
model="#{geocodeView.geoModel}" style="width:100%;height:400px">
<p:ajax event="geocode" listener="#{geocodeView.onGeocode}" update="@this"/>
</p:gmap>
</div>
<div class="card">
<h5>Reverse Geocode</h5>
<h:panelGrid columns="5" style="margin-bottom:10px" cellpadding="5">
<p:outputLabel for="lat" value="Lat:"/>
<p:inputText id="lat"/>
<p:outputLabel for="lng" value="Lng:"/>
<p:inputText id="lng"/>
<p:commandButton value="Reverse Geocode" icon="pi pi-search" onclick="reverseGeocode()"
type="button"/>
</h:panelGrid>
<p:gmap id="revGeoGmap" widgetVar="revGeoMap" center="#{geocodeView.centerRevGeoMap}" zoom="2"
type="ROADMAP" model="#{geocodeView.revGeoModel}" style="width:100%;height:400px">
<p:ajax event="reverseGeocode" listener="#{geocodeView.onReverseGeocode}" update="@this"/>
</p:gmap>
</div>
</h:form>
package org.primefaces.showcase.view.data.gmap;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Named;
import java.util.List;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.primefaces.event.map.GeocodeEvent;
import org.primefaces.event.map.ReverseGeocodeEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.GeocodeResult;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;
@Named
@RequestScoped
@RegisterForReflection(serialization = true)
public class GeocodeView {
private MapModel geoModel;
private MapModel revGeoModel;
private String centerGeoMap = "41.850033, -87.6500523";
private String centerRevGeoMap = "41.850033, -87.6500523";
@PostConstruct
public void init() {
geoModel = new DefaultMapModel();
revGeoModel = new DefaultMapModel();
}
public void onGeocode(GeocodeEvent event) {
List<GeocodeResult> results = event.getResults();
if (results != null && !results.isEmpty()) {
LatLng center = results.get(0).getLatLng();
centerGeoMap = center.getLat() + "," + center.getLng();
for (int i = 0; i < results.size(); i++) {
GeocodeResult result = results.get(i);
geoModel.addOverlay(new Marker(result.getLatLng(), result.getAddress()));
}
}
}
public void onReverseGeocode(ReverseGeocodeEvent event) {
List<String> addresses = event.getAddresses();
LatLng coord = event.getLatlng();
if (addresses != null && !addresses.isEmpty()) {
centerRevGeoMap = coord.getLat() + "," + coord.getLng();
revGeoModel.addOverlay(new Marker(coord, addresses.get(0)));
}
}
public MapModel getGeoModel() {
return geoModel;
}
public MapModel getRevGeoModel() {
return revGeoModel;
}
public String getCenterGeoMap() {
return centerGeoMap;
}
public String getCenterRevGeoMap() {
return centerRevGeoMap;
}
}