CoordinatesUtil.java 文件源码

java
阅读 24 收藏 0 点赞 0 评论 0

项目:C4SG-Obsolete 作者:
/**@param{Object} Address- The physical address of a location
  * This method returns Geocode coordinates of the Address object.Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway,
  * Mountain View, CA") into geographic coordinates (like latitude 37.423021 and longitude -122.083739).Please give all the physical Address values 
  * for a precise coordinate. setting none of the values will give a null value for the Latitude and Longitude. 
  */ 
public static GeoCode getCoordinates(Address address) throws Exception
 {
  GeoCode geo= new GeoCode();
  String physicalAddress = (address.getAddress1() == null ? "" : address.getAddress1() + ",")
          + (address.getAddress2() == null ? "" : address.getAddress2() + ",")
          + (address.getCityName() == null ? "" : address.getCityName() + ",")
          + (address.getState()    == null ? "" : address.getState() + "-")
          + (address.getZip()      == null ? "" : address.getZip() + ",")
          + (address.getCountry()  == null ? "" :  address.getCountry());
  String api = GMAPADDRESS + URLEncoder.encode(physicalAddress, "UTF-8") + SENSOR;
  URL url = new URL(api);
  HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
  httpConnection.connect();
  int responseCode = httpConnection.getResponseCode();
  if(responseCode == 200)
   {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.parse(httpConnection.getInputStream());
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = xpath.compile(STATUS);
    String status = (String)expr.evaluate(document, XPathConstants.STRING);
    if(status.equals("OK"))
     {
       expr = xpath.compile(LATITUDE);
       String latitude = (String)expr.evaluate(document, XPathConstants.STRING);
          expr = xpath.compile(LONGITUDE);
       String longitude = (String)expr.evaluate(document, XPathConstants.STRING);
       geo.setLatitude(latitude);
       geo.setLongitude(longitude);
     }
    }
    else
    {
        throw new Exception("Fail to Convert to Geocode");
    }
    return geo;
  }
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号