Package com.googlecode.mashups.services.generic.api

Examples of com.googlecode.mashups.services.generic.api.Location


   
    public void testGetLocationFromAddress() throws Exception {
        LocationService locationService = GenericServicesFactory.getLocationService();
       
        try {
            Location location = locationService.getLocationFromAddress("Cairo, Egypt");
           
            System.out.println("LatLng for (\"Cairo, Egypt\") is: " + location.getLatitude() +", " + location.getLongitude());
        } catch (Exception e) {
            e.printStackTrace();
            fail("Unable to get the location from the address ...");
        }       
    }
View Full Code Here


    public static LocationService getInstance() {
        return locationService;
    }
   
  public Location getLocationFromAddress(String address) throws Exception {
    Location   location       = new Location();
    String     encodedAddress = URLEncoder.encode(address, "UTF-8");    
    URL        url            = new URL("http://maps.googleapis.com/maps/api/geocode/json?address=" + encodedAddress + "&sensor=false");
    String     addresses      = readURL(url);   
    JSONObject json           = new JSONObject(addresses);
   
    String status = (String) json.get(Mashups4JSFConstants.STATUS_LABEL);
   
    if (Mashups4JSFConstants.OK_MESSAGE.equalsIgnoreCase(status)) {
        log.debug("OK Status is: " + status + " for: " + address);               
        JSONArray results = json.getJSONArray(Mashups4JSFConstants.RESULTS_LABEL);
       
        if (results.length() <= 0) {
            return null;
        }
       
        JSONObject result       = results.getJSONObject(0);
        JSONObject jsonLocation = result.getJSONObject(Mashups4JSFConstants.GEOMETRY_LABEL).getJSONObject(Mashups4JSFConstants.LOCATION_LABEL);
       
        Double latitude  = (Double) jsonLocation.get(Mashups4JSFConstants.LATITUDE_LABEL);
        Double longitude = (Double) jsonLocation.get(Mashups4JSFConstants.LONGITUDE_LABEL);               
       
        location.setLatitude(latitude);
        location.setLongitude(longitude);
       
    } else {
        log.debug("[From Google Service] Status is: " + status);
        return null;
    }
View Full Code Here

TOP

Related Classes of com.googlecode.mashups.services.generic.api.Location

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.