2011-03-11 21 views

Respuesta

10

Esto se conoce como codificación geográfica, y requiere un nombre de lugar a juego hasta una latitud y longitud.

Puede codificar una lista conocida de nombres de lugares/latones en su aplicación, leerlos en tiempo de ejecución y buscarlos cuando sea necesario, o puede utilizar un servicio de geocodificación en línea como this one from Yahoo o this one from Google.

2

Google ofrece geocodificación inversa - por favor vaya a través this enlace

también por ejemplo por debajo servicio web le da la dirección de Lat Long proporcionada en el parámetro pero HTE respuesta está en JSON, ver this enlace

si desea obtener una respuesta en xml y luego verificar here

1

Tuve el mismo problema ... Finalmente lo arreglé obteniendo el latitud larga del lado del servidor. El enlace This tiene el código de ejemplo para obtener lat y long desde java. Espero que ayude a alguien.

private static final String GEOCODE_REQUEST_URL = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&"; 
    private static HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); 
    String strLatitude; 
    String strLongtitude; 
     public void getLongitudeLatitude(String address) { 
      try { 
       StringBuilder urlBuilder = new StringBuilder(GEOCODE_REQUEST_URL); 
       if (StringUtils.isNotBlank(address)) { 
        urlBuilder.append("&address=").append(URLEncoder.encode(address, "UTF-8")); 
       } 

       final GetMethod getMethod = new GetMethod(urlBuilder.toString()); 
       try { 
        httpClient.executeMethod(getMethod); 
        Reader reader = new InputStreamReader(getMethod.getResponseBodyAsStream(), getMethod.getResponseCharSet()); 

        int data = reader.read(); 
        char[] buffer = new char[1024]; 
        Writer writer = new StringWriter(); 
        while ((data = reader.read(buffer)) != -1) { 
          writer.write(buffer, 0, data); 
        } 

        String result = writer.toString(); 
        System.out.println(result.toString()); 

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
        DocumentBuilder db = dbf.newDocumentBuilder(); 
        InputSource is = new InputSource(); 
        is.setCharacterStream(new StringReader("<"+writer.toString().trim())); 
        Document doc = db.parse(is); 

        strLatitude = getXpathValue(doc, "//GeocodeResponse/result/geometry/location/lat/text()"); 
        System.out.println("Latitude:" + strLatitude); 

        strLongtitude = getXpathValue(doc,"//GeocodeResponse/result/geometry/location/lng/text()"); 
        System.out.println("Longitude:" + strLongtitude); 


       } finally { 
        getMethod.releaseConnection(); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

     private String getXpathValue(Document doc, String strXpath) throws XPathExpressionException { 
      XPath xPath = XPathFactory.newInstance().newXPath(); 
      XPathExpression expr = xPath.compile(strXpath); 
      String resultData = null; 
      Object result4 = expr.evaluate(doc, XPathConstants.NODESET); 
      NodeList nodes = (NodeList) result4; 
      for (int i = 0; i < nodes.getLength(); i++) { 
       resultData = nodes.item(i).getNodeValue(); 
      } 
      return resultData; 


    } 
Cuestiones relacionadas