Já faz algum tempo que perguntaram, mas vou deixar como eu fiz aqui. Dá pra fazer com o primefaces direto, eu só não consegui resolver um problema com curvas. Mas pra quem interessar, é até simples:
Usei um método de exemplo do próprio Google, vou colocar ela aqui pq não estou achando o link dela:
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.primefaces.model.map.LatLng;
import org.xml.sax.SAXException;
public class GeocodingSample {
private static final String GEOCODER_REQUEST_PREFIX_FOR_XML = "http://maps.google.com/maps/api/geocode/xml";
private static final String DESTINATION_REQUEST_PREFIX_FOR_XML = "http://maps.googleapis.com/maps/api/directions/xml?origin=";
public LatLng latitudeLongitude(String address) throws UnsupportedEncodingException, MalformedURLException, IOException, ParserConfigurationException, SAXException, XPathExpressionException{
// prepare a URL to the geocoder
URL url = new URL(GEOCODER_REQUEST_PREFIX_FOR_XML + "?address=" + URLEncoder.encode(address, "UTF-8") + "&sensor=false");
// prepare an HTTP connection to the geocoder
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Document geocoderResultDocument = null;
try {
// open the connection and get results as InputSource.
conn.connect();
InputSource geocoderResultInputSource = new InputSource(conn.getInputStream());
// read result and parse into XML Document
geocoderResultDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(geocoderResultInputSource);
} finally {
conn.disconnect();
}
// prepare XPath
XPath xpath = XPathFactory.newInstance().newXPath();
// extract the result
NodeList resultNodeList = null;
//
// // a) obtain the formatted_address field for every result
// resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result/formatted_address", geocoderResultDocument, XPathConstants.NODESET);
// for(int i=0; i<resultNodeList.getLength(); ++i) {
// System.out.println(resultNodeList.item(i).getTextContent());
// }
//
// // b) extract the locality for the first result
// resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/address_component[type/text()='locality']/long_name", geocoderResultDocument, XPathConstants.NODESET);
// for(int i=0; i<resultNodeList.getLength(); ++i) {
// System.out.println(resultNodeList.item(i).getTextContent());
// }
// c) extract the coordinates of the first result
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/geometry/location/*", geocoderResultDocument, XPathConstants.NODESET);
float lat = Float.NaN;
float lng = Float.NaN;
for(int i=0; i<resultNodeList.getLength(); ++i) {
Node node = resultNodeList.item(i);
if("lat".equals(node.getNodeName())) lat = Float.parseFloat(node.getTextContent());
if("lng".equals(node.getNodeName())) lng = Float.parseFloat(node.getTextContent());
}
return new LatLng(lat, lng);
// System.out.println("lat/lng=" + lat + "," + lng);
// c) extract the coordinates of the first result
// resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/address_component[type/text() = 'administrative_area_level_1']/country[short_name/text() = 'US']/*", geocoderResultDocument, XPathConstants.NODESET);
// lat = Float.NaN;
// lng = Float.NaN;
// for(int i=0; i<resultNodeList.getLength(); ++i) {
// Node node = resultNodeList.item(i);
// if("lat".equals(node.getNodeName())) lat = Float.parseFloat(node.getTextContent());
// if("lng".equals(node.getNodeName())) lng = Float.parseFloat(node.getTextContent());
// }
// System.out.println("lat/lng=" + lat + "," + lng);
}
public ArrayList<LatLng> geraCoordenadasRota(LatLng latFunc, LatLng latVend) throws MalformedURLException, IOException, ParserConfigurationException, SAXException{
ArrayList<LatLng> lista = new ArrayList<LatLng>();
ArrayList<String> listaLat = new ArrayList<String>();
ArrayList<String> listaLng = new ArrayList<String>();
String coordFunc = String.valueOf(latFunc.getLat())+","+String.valueOf(latFunc.getLng());
String coordVend = String.valueOf(latVend.getLat())+","+String.valueOf(latVend.getLng());
URL url = new URL(DESTINATION_REQUEST_PREFIX_FOR_XML+coordFunc+"&destination="+coordVend+"&sensor=false");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Document geocoderResultDocument = null;
try {
conn.connect();
InputSource geocoderResultInputSource = new InputSource(conn.getInputStream());
geocoderResultDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(geocoderResultInputSource);
} finally {
conn.disconnect();
}
NodeList nodes = geocoderResultDocument.getElementsByTagName("lat");
for (int i=0; i<nodes.getLength()-4; i++){
Node node = nodes.item(i);
listaLat.add(node.getTextContent());
}
nodes = geocoderResultDocument.getElementsByTagName("lng");
for (int i=0; i<nodes.getLength()-4; i++){
Node node = nodes.item(i);
listaLng.add(node.getTextContent());
}
for (int i=0; i<listaLat.size(); i++){
lista.add(new LatLng(Double.parseDouble(listaLat.get(i)), Double.parseDouble(listaLng.get(i))));
}
return lista;
}
}
O primeiro método é o de exemplo do Google, que retorna a latitude e longitude de um endereço(as partes comentadas do código servem pra mostrar todas as outras informações do endereço fornecido. O segundo método eu montei, ele só lê o xml de retorno do Google, encontra as coordenadas e retorna elas num ArrayList.
Depois, no seu bean, é só colocar um MapModel, adicionar o Polylines contendo esse ArrayList como parâmetro e no xhtml, setar o modelo do gmap como sendo o MapModel do seu bean.
Fiz o método hoje, então não testei ele muito, então se alguém tiver sugestão pra melhorar o método, ou mesmo pra deixar o código mais apresentável, eu agradeço também.
Abraços.