Google places API
Recently Google released
The Google Places API
So I thought I should contribute to Spring Social google project at https://github.com/GabiAxel/spring-social-google
I forked this code which you can find it here https://github.com/gabhi/spring-social-google
Few observations:
- Google places Doesnt support REST yet.
So I started to write a consumer for this.
To consume Google places URL is of the format
https://maps.googleapis.com/maps/api/place/search/output?parameters
So I thought of providing some param and used following Java code to retrieve JSON output
try {
URL url = new URL("https://maps.googleapis.com/maps/api/place/search" +
"/json?location=-33.8670522,151.1957362&radius=500&types=food" +
"&name=&sensor=false&key=AIzaSyB0f-PalseZwVFQJnkZjvGMumaOOGE6LoI");
InputStream response = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(response));for (String line; (line = reader.readLine()) != null;) {ret.append(line);
System.out.println(line);}
PlaceSearchResults ob = new ObjectMapper().readValue(ret.toString(), PlaceSearchResults.class);
System.out.println(ob);
reader.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
Some of JSON objects are following
public class Geometry {
Location location;
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
}
Place.java
public class Location {
String lat;
String lng;
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLng() {
return lng;
}
public void setLng(String lng) {
this.lng = lng;
}
}
OpeningHours.java
public class OpeningHours {
String open_now;
public String getOpen_now() {
return open_now;
}
public void setOpen_now(String open_now) {
this.open_now = open_now;
}
}
Place.java
public class Place extends ApiEntity {
private String formatted_address;
private String icon;
private String name;
private String rating;
private String reference;
String[] types;
Geometry geometry;
String vicinity;
OpeningHours opening_hours ;
public OpeningHours getOpening_hours() {
return opening_hours;
}
public void setOpening_hours(OpeningHours opening_hours) {
this.opening_hours = opening_hours;
}
public String getVicinity() {
return vicinity;
}
public void setVicinity(String vicinity) {
this.vicinity = vicinity;
}
public String[] getTypes() {
return types;
}
public void setTypes(String[] types) {
this.types = types;
}
public Geometry getGeometry() {
return geometry;
}
public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}
// to add “types” : [ "restaurant", "food", "establishment" ]
//
// “geometry” : {
// “location” : {
// “lat” : -33.8750460,
// “lng” : 151.2052720
// }
// }
//
public String getFormatted_address() {
return formatted_address;
}
public void setFormatted_address(String formatted_address) {
this.formatted_address = formatted_address;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public Place(String formatted_address, String icon, String name, String rating, String reference) {
this.formatted_address = formatted_address;
this.icon = icon;
this.name = name;
this.rating = rating;
this.reference = reference;
}
public Place(String id) {
super(id);
}
public Place() {
}
}
PlaceSearchResults.java
public class PlaceSearchResults {
public String[] getHtml_attributions() {
return html_attributions;
}
private String next_page_token;
public String getNext_page_token() {
return next_page_token;
}
public void setNext_page_token(String next_page_token) {
this.next_page_token = next_page_token;
}
public void setHtml_attributions(String[] html_attributions) {
this.html_attributions = html_attributions;
}
public Place[] getResults() {
return results;
}
public void setResults(Place[] results) {
this.results = results;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
String[] html_attributions;
Place[] results;
String status;
}