2011-03-11 55 views

回答

10

這就是所謂的地理編碼,需要追加對地名的經度和緯度。

您可以將已知的地名/緯度列表編碼到您的應用中,在運行時讀取它們並在需要時搜索它們,也可以使用在線地理編碼服務,例如this one from Yahoothis one from Google

2

谷歌提供反向地理編碼 - 請去通過this鏈接

還例如下面的Web服務讓您在參數長提供的經緯度的地址,但興田響應是JSON,看到this鏈接

如果你想得到的答覆在XML然後檢查here

1

我有同樣的問題..最後,我通過從服務器端獲得拉長來修復它。 This鏈接有示例代碼用於從java獲取lat和long。希望它有助於某個人。

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; 


    } 
相關問題