Google Geocoding API with Java using p:gmap

Posted: January 10, 2013 in web

xthml

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

Backing Bean

    ...
	
    @Inject
    public void init() {
        try {
            initMap();
        } catch (JsonSyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    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")),
                    GoogleGeoCodeResponse.class);
            double lat = Double.parseDouble(result.results[0].geometry.location.lat);
            double lng = Double.parseDouble(result.results[0].geometry.location.lng);
            LatLng coord = new LatLng(lat, lng);
            Circle circle = new Circle(coord, 100300);
            circle.setStrokeColor("#00ff00");
            circle.setFillColor("#00ff00");
            circle.setStrokeOpacity(0.7);
            circle.setFillOpacity(0.7);
            circleModel.addOverlay(circle);
        }
    }

    private String jsonCoord(String address) throws IOException {
        URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?address=" + 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;
        }
        in.close();
        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;
    }

}

pom.xml

		<dependency>
		    <groupId>com.google.code.gson</groupId>
		    <artifactId>gson</artifactId>
		    <version>2.2.2</version>
		</dependency>
Advertisements
Comments
  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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s