package org.oneliveweb.geocode; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringWriter; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.openedit.ModuleManager; import org.openedit.OpenEditException; import org.openedit.util.FileUtils; import org.openedit.util.OutputFiller; public class PositionSearch { private static final Log log = LogFactory.getLog(PositionSearch.class); protected String fieldGoogleKey = "AIzaSyAgR338n3OFlAwyG4ZE0ZdaMMllx7i-1Fw"; protected String fieldCatalogId; protected ModuleManager fieldModuleManager; // http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&output=xml&key=abcdefg //int delay = 0; //Hits for this search public ModuleManager getModuleManager() { return fieldModuleManager; } public void setModuleManager(ModuleManager inModuleManager) { fieldModuleManager = inModuleManager; } public String getCatalogId() { return fieldCatalogId; } public void setCatalogId(String inCatalogId) { fieldCatalogId = inCatalogId; } public String getGoogleKey() { return fieldGoogleKey; } public void setGoogleKey(String inGoogleKey) { fieldGoogleKey = inGoogleKey; } public JSONObject getPositions(String latlong, String lookupString, String range, String pagetoken) throws Exception { ArrayList l = new ArrayList(); String responseString = null; if (lookupString == null) { return null; } lookupString = URLEncoder.encode(lookupString, "UTF-8"); // try // { // Thread.sleep(delay); // } // catch (InterruptedException e) // { // // TODO Auto-generated catch block // //e.printStackTrace(); // } // https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=1500&type=restaurant&keyword=cruise&key=YOUR_API_KEY ///String url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=" + lookupString + "&inputtype=textquery&fields=photos,formatted_address,name,opening_hours,rating&locationbias=circle:" + range +"@" + latlong; String url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" + lookupString + "&radius=" + range +"&location=" + latlong; if(pagetoken != null) { url = url + "&pagetoken=" + pagetoken; } //AIzaSyAgR338n3OFlAwyG4ZE0ZdaMMllx7i-1Fw if( getGoogleKey() != null) { url = url + "&key=" + getGoogleKey(); } responseString = downloadToString(url); JSONParser parser = new JSONParser(); JSONObject results = (JSONObject) parser.parse(responseString); return results; //List positions = parseGoogleResponse(responseString); } public JSONObject getContactInfo(String inPosition) throws Exception { ArrayList l = new ArrayList(); String responseString = null; String url = "https://maps.googleapis.com/maps/api/place/details/json?place_id=" + inPosition + "&fields=website,formatted_phone_number,opening_hours"; if( getGoogleKey() != null) { url = url + "&key=" + getGoogleKey(); } responseString = downloadToString(url); JSONParser parser = new JSONParser(); JSONObject results = (JSONObject) parser.parse(responseString); return results; //List positions = parseGoogleResponse(responseString); } public String downloadToString(String inUrl) { StringWriter out = null; InputStream in = null; try { URL url = new URL(inUrl); URLConnection con = url.openConnection(); con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"); con.setUseCaches(false); con.connect(); //*** create new output file //*** make a growable storage area to read into out = new StringWriter(); //*** read in url connection stream into input stream in = con.getInputStream(); //*** fill output stream new OutputFiller().fill(new InputStreamReader(in),out); return out.toString(); } catch ( Exception ex) { throw new OpenEditException(ex); } finally { //*** close output stream FileUtils.safeClose(out); //*** close input stream FileUtils.safeClose(in); } } // // // 1600 amphitheatre mountain view ca // // 200 // geocode // // //
// 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA //
// // // US // // CA // // Santa Clara // // Mountain View // // 1600 Amphitheatre Pkwy // // // 94043 // // // // // // // // -122.083739,37.423021,0 // //
//
//
}