Google GeoCoding

Geo-coding means we are now working with something location based. that's great. Google make it easy by providing the service.

we can found any location based information the URL given below:

1:  http://maps.googleapis.com/maps/api/geocode/json  

here the last portion for which format response you want??
json or xml?
just make your choice, if we want response in xml format then put 'xml' instead of  'json'.
we also include required parameter in the url.
we can search a location by name or longitude, latitude based and many more. we are going to show example of both name and longitude, latitude based.
Here i use jackson object mapper to mapped the response. you need to add the jar in your class path.
oh, i forgot to say add commons.io.2.4 jar or upper version (i don't know does the upper version already exits. :P ).
you can find jackson jar from here.
here function that give the longitude and latitude given below,

1:  public GoogleResponse convertToLatLong(String fullAddress, String locationType) throws IOException {  
2:      URL url = new URL(URL + "?address="  
3:          + URLEncoder.encode(fullAddress, "UTF-8") + "&sensor=false" + "&types="+locationType);  
4:      // Open the Connection  
5:      URLConnection conn = url.openConnection();  
6:      InputStream in = conn.getInputStream();  
7:      ObjectMapper mapper = new ObjectMapper();  
8:      GoogleResponse response = (GoogleResponse) mapper.readValue(in, GoogleResponse.class);  
9:      in.close();  
10:      return response;  
11:    }  

here we send sensor= false refers that this response is not for a device. set the URL value that i mentioned above.
you can get location from longitude and latitude by using the function that is given below,

1:  public GoogleResponse convertFromLatLong(String latlongString, String locationType) throws IOException {  
2:      URL url;  
3:        url = new URL(URL + "?latlng="  
4:          + URLEncoder.encode(latlongString, "UTF-8") + "&sensor=false" + "&types="+locationType);  
5:      // Open the Connection  
6:      URLConnection conn = url.openConnection();  
7:      InputStream in = conn.getInputStream();  
8:      ObjectMapper mapper = new ObjectMapper();  
9:      GoogleResponse response = (GoogleResponse) mapper.readValue(in, GoogleResponse.class);  
10:      in.close();  
11:      return response;  
12:    }  

Here remember the key point always encode the url in "UTF-8", may be google like that.
now where is the GoogleResponse Class??
Don't worry. :D, right here.

1:  /**  
2:   *  
3:   * @author Ataur Rahman  
4:   */  
5:  public class GoogleResponse {  
6:    private Result[] results ;  
7:    private String status ;  
8:    public Result[] getResults() {  
9:      return results;  
10:    }  
11:    public void setResults(Result[] results) {  
12:      this.results = results;  
13:    }  
14:    public String getStatus() {  
15:      return status;  
16:    }  
17:    public void setStatus(String status) {  
18:      this.status = status;  
19:    }  
20:  }  

shit, where is the Result Class?

1:  /**  
2:   *  
3:   * @author Ataur Rahman  
4:   */  
5:  public class Result {  
6:    private String formatted_address;  
7:    private boolean partial_match;  
8:    private Geometry geometry;  
9:    @JsonIgnore  
10:    private Object address_components;  
11:    @JsonIgnore  
12:    private Object types;  
13:    private String place_id;  
14:    public String getFormatted_address() {  
15:      return formatted_address;  
16:    }  
17:    public void setFormatted_address(String formatted_address) {  
18:      this.formatted_address = formatted_address;  
19:    }  
20:    public boolean isPartial_match() {  
21:      return partial_match;  
22:    }  
23:    public void setPartial_match(boolean partial_match) {  
24:      this.partial_match = partial_match;  
25:    }  
26:    public Geometry getGeometry() {  
27:      return geometry;  
28:    }  
29:    public void setGeometry(Geometry geometry) {  
30:      this.geometry = geometry;  
31:    }  
32:    public Object getAddress_components() {  
33:      return address_components;  
34:    }  
35:    public void setAddress_components(Object address_components) {  
36:      this.address_components = address_components;  
37:    }  
38:    public Object getTypes() {  
39:      return types;  
40:    }  
41:    public void setTypes(Object types) {  
42:      this.types = types;  
43:    }  
44:    public String getPlace_id() {  
45:      return place_id;  
46:    }  
47:    public void setPlace_id(String place_id) {  
48:      this.place_id = place_id;  
49:    }  
50:  }  

Oh no, where is the Geometry class??

1:  /**  
2:   *  
3:   * @author Ataur Rahman  
4:   */  
5:  public class Geometry {  
6:    private Location location ;  
7:    private String location_type;  
8:    @JsonIgnore  
9:    private Object bounds;  
10:    @JsonIgnore  
11:    private Object viewport;  
12:    public Location getLocation() {  
13:      return location;  
14:    }  
15:    public void setLocation(Location location) {  
16:      this.location = location;  
17:    }  
18:    public String getLocation_type() {  
19:      return location_type;  
20:    }  
21:    public void setLocation_type(String location_type) {  
22:      this.location_type = location_type;  
23:    }  
24:    public Object getBounds() {  
25:      return bounds;  
26:    }  
27:    public void setBounds(Object bounds) {  
28:      this.bounds = bounds;  
29:    }  
30:    public Object getViewport() {  
31:      return viewport;  
32:    }  
33:    public void setViewport(Object viewport) {  
34:      this.viewport = viewport;  
35:    }  
36:  }  

Are you kidding?? where is the Location class?

1:  /**  
2:   *  
3:   * @author Ataur Rahman  
4:   */  
5:  public class Location {  
6:    private String lat;  
7:    private String lng;  
8:    public String getLat() {  
9:      return lat;  
10:    }  
11:    public void setLat(String lat) {  
12:      this.lat = lat;  
13:    }  
14:    public String getLng() {  
15:      return lng;  
16:    }  
17:    public void setLng(String lng) {  
18:      this.lng = lng;  
19:    }  
20:  }  

wow, all class are now complete. To know about the response detail, please go here and read it.
noted that when we want location from lat and longd, print only first two level (depth) address.if you want full, then just remove the counter part given in below code.

And the last of all my complete code given below,

1:  import java.io.IOException;  
2:  import java.io.InputStream;  
3:  import java.net.URL;  
4:  import java.net.URLConnection;  
5:  import java.net.URLEncoder;  
6:  import org.codehaus.jackson.map.ObjectMapper;  
7:  /**  
8:   *  
9:   * @author Ataur Rahman  
10:   */  
11:  public class AddressConverter {  
12:    private static final String URL = "http://maps.googleapis.com/maps/api/geocode/json";  
13:    /*used component filtering   
14:     *   
15:     *   
16:     * http://maps.google.com/maps/api/geocode/json?components=country:AU|postal_code:2340&sensor=false  
17:     *   
18:     *   
19:     *   
20:     *   
21:     */  
22:    private static final String locationType = "restaurant";//// ex: restaurant, airport etc  
23:    private static final String address = "KFC, Banani,Dhaka, Bangladesh";  
24:    private static String longitude = "";  
25:    private static String latitude = "";  
26:    public GoogleResponse convertToLatLong(String fullAddress, String locationType) throws IOException {  
27:      URL url = new URL(URL + "?address="  
28:          + URLEncoder.encode(fullAddress, "UTF-8") + "&sensor=false" + "&types="+locationType);  
29:      // Open the Connection  
30:      URLConnection conn = url.openConnection();  
31:      InputStream in = conn.getInputStream();  
32:      ObjectMapper mapper = new ObjectMapper();  
33:      GoogleResponse response = (GoogleResponse) mapper.readValue(in, GoogleResponse.class);  
34:      in.close();  
35:      return response;  
36:    }  
37:    public GoogleResponse convertFromLatLong(String latlongString, String locationType) throws IOException {  
38:      URL url;  
39:        url = new URL(URL + "?latlng="  
40:          + URLEncoder.encode(latlongString, "UTF-8") + "&sensor=false" + "&types="+locationType);  
41:      // Open the Connection  
42:      URLConnection conn = url.openConnection();  
43:      InputStream in = conn.getInputStream();  
44:      ObjectMapper mapper = new ObjectMapper();  
45:      GoogleResponse response = (GoogleResponse) mapper.readValue(in, GoogleResponse.class);  
46:      in.close();  
47:      return response;  
48:    }  
49:    public static void showAreaDependingLongLat(String lat, String longd) throws IOException {  
50:      GoogleResponse response = new AddressConverter().convertFromLatLong(lat + "," + longd, locationType);  
51:      if (response.getStatus().equals("OK")) {  
52:        int count = 0;  
53:        for (Result result : response.getResults()) {  
54:          count++;  
55:          if (count > 2)  
56:            break;  
57:          else  
58:          System.out.println("address is :" + result.getFormatted_address());  
59:        }  
60:      } else {  
61:        System.out.println(response.getStatus());  
62:      }  
63:    }  
64:    public static void main(String[] args) throws IOException {  
65:      GoogleResponse res = new AddressConverter().convertToLatLong(address, locationType);  
66:      if (res.getStatus().equals("OK")) {  
67:        for (Result result : res.getResults()) {  
68:          latitude = result.getGeometry().getLocation().getLat();  
69:          System.out.println("Lattitude of address is :" + result.getGeometry().getLocation().getLat());  
70:          longitude = result.getGeometry().getLocation().getLng();  
71:          System.out.println("Longitude of address is :" + result.getGeometry().getLocation().getLng());  
72:          System.out.println("Location is " + result.getGeometry().getLocation_type());  
73:          showAreaDependingLongLat(latitude,longitude);  
74:        }  
75:      } else {  
76:        System.out.println(res.getStatus());  
77:      }  
78:    }  
79:  }  
we already done. just take a hot coffee now. :D
last of all , i don't know why the code is working without giving any API_KEY. if you want then you can include it in url as a parameter.


Comments

Popular posts from this blog

UUID to BigInteger conversion and BigInteger to UUID conversion

ActiveMQ message producer and consumer with durable subscriber example

Create Maven Local Repository, Upload your own artifact & download jar from local mirror with Artifactory and Nexus OSS