Google Geocoding API with Java using p:gmap

Posted: January 10, 2013 in web


<p:gmap center="36.350527,-90.113159" zoom="5"  type="TERRAIN" style="width:1000px;height:600px"
                        model="#{backingbean.circleModel}" />

Backing Bean

    public void init() {
        try {
        } catch (JsonSyntaxException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block

    private void initMap() throws JsonSyntaxException, IOException {
        circleModel = new DefaultMapModel();
        List<String> tmp = new ArrayList<String>();

        tmp.add("500 Eubank Boulevard Southeast Albuquerque, NM 87123");
        tmp.add("6350 Peachtree Dunwoody Road Northeast Atlanta, GA 30328");
        tmp.add("1701 Dallas Parkway Plano, TX 75093");

        Gson gson = new Gson();
        for (String address : tmp) {
            GoogleGeoCodeResponse result = gson.fromJson(jsonCoord(URLEncoder.encode(address, "UTF-8")),
            double lat = Double.parseDouble(result.results[0];
            double lng = Double.parseDouble(result.results[0].geometry.location.lng);
            LatLng coord = new LatLng(lat, lng);
            Circle circle = new Circle(coord, 100300);

    private String jsonCoord(String address) throws IOException {
        URL url = new URL("" + address + "&sensor=false");
        URLConnection connection = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        String jsonResult = "";
        while ((inputLine = in.readLine()) != null) {
            jsonResult += inputLine;
        return jsonResult;


GoogleGeoCodeResponse class

public class GoogleGeoCodeResponse {
    public String status;
    public results[] results;

    public GoogleGeoCodeResponse() {

    public class results {
        public String formatted_address;
        public geometry geometry;
        public String[] types;
        public address_component[] address_components;

    public class geometry {
        public bounds bounds;
        public String location_type;
        public location location;
        public bounds viewport;

    public class bounds {

        public location northeast;
        public location southwest;

    public class location {
        public String lat;
        public String lng;

    public class address_component {
        public String long_name;
        public String short_name;
        public String[] types;



  1. Mauro Pepe says:

    Great post, works really fine!

  2. Luciano Pontes says:

    This code needs some kind of proxy configuration? I’m running behind a proxy to make any request http and https?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s