ReqGeoLoc.java :  » UnTagged » ot-sims » fr » insa » lyon » ot » sims » semantique » Android Open Source

Android Open Source » UnTagged » ot sims 
ot sims » fr » insa » lyon » ot » sims » semantique » ReqGeoLoc.java
package fr.insa.lyon.ot.sims.semantique;

import java.net.URL;
import java.net.URLEncoder;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import fr.insa.lyon.ot.sims.semantique.Request.Position;

public class ReqGeoLoc {
  
  public Position geoloc(String street, String zip, String city) {
    String data = "";
    try {
      if ((street != null) && (street != "")) {
        data += "&street="+URLEncoder.encode(street, "UTF-8");
      }
      if ((zip != null) && (zip != "")) {
        data += "&zip="+URLEncoder.encode(zip, "UTF-8");
      }
      if ((city != null) && (city != "")) {
        data += "&city="+URLEncoder.encode(city, "UTF-8");
      }
    } catch (Exception e) {
      System.out.println(e);
    }
    if (data == "") {
      return null;
    }
    return geolocReq(data);
  }

  public Position geoloc(String location) {
    String data = "";
    try {
      if ((location != null) && (location != "")) {
        data += "&location="+URLEncoder.encode(location, "UTF-8");
      }
    } catch (Exception e) {
      System.out.println(e);
    }
    if (data == "") {
      return null;
    }
    return geolocReq(data);
  }

  private Position geolocReq (String data) {
    String urlReq = "http://local.yahooapis.com/MapsService/V1/geocode?appid=brJNbOjV34EricvoH5YnjrtOk3EUayx9bvKHJ33BqCB91ThOdGPc8hUv7gPEjPY-&query="+data;
    try {
      URL url = new URL(urlReq);
          Document respDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream());
          
          XPathFactory factory = XPathFactory.newInstance();
          XPath xPath=factory.newXPath();
          
          //Get the results
          NodeList nodes = (NodeList)xPath.evaluate("/ResultSet/Result", respDoc, XPathConstants.NODESET);
            // int nodeCount = nodes.getLength();
          
        //iterate over search Result nodes
            // for (int i = 0; i < 1; i++)
            int i = 0;
            {
              //Get each xpath expression as a string
            double latitude = (Double)xPath.evaluate("Latitude", nodes.item(i), XPathConstants.NUMBER);
                double longitude = (Double) xPath.evaluate("Longitude", nodes
                        .item(i), XPathConstants.NUMBER);
              String address = (String)xPath.evaluate("Address", nodes.item(i), XPathConstants.STRING);
              String city = (String)xPath.evaluate("City", nodes.item(i), XPathConstants.STRING);
              String zip = (String)xPath.evaluate("Zip", nodes.item(i), XPathConstants.STRING);
              String country = (String)xPath.evaluate("Country", nodes.item(i), XPathConstants.STRING);

              Position pos = Request.createPosition();
              pos.latitude = latitude;
              pos.longitude = longitude;
              pos.address = address;
              pos.city = city+", "+country;
                pos.zipCode = zip;
              
              
              /*
              System.out.println("Latitude: " + latitude);
              System.out.println("Longitude: " + longitude);
              System.out.println("Address: " + address);
              System.out.println("City: " + city);
              System.out.println("Zip: " + zip);
              System.out.println("Country: " + country);
              System.out.println("--");
              */
              return pos;
          }
    } catch (Exception e) {
      System.out.println(e);
    }
    return null;
  }
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.