【问题标题】:How to get Latitude and Longitude for particular city or street without using google map?如何在不使用谷歌地图的情况下获取特定城市或街道的纬度和经度?
【发布时间】:2013-01-02 19:30:57
【问题描述】:

如何在不使用谷歌地图的情况下获取特定城市或街道或位置的纬度和经度? 例如,用户输入的城市名称是“Chennai”,我只需要显示该城市的纬度和经度。如何做到这一点?

【问题讨论】:

    标签: java java-me latitude-longitude jsr179


    【解决方案1】:

    这称为地理编码,需要将地名与纬度和经度匹配。

    您可以将已知的地名/纬度列表编码到您的应用程序中,在运行时读取它们并在需要时搜索它们,或者您可以使用在线地理编码服务,例如 this one from Yahoo 或 @987654322 @。

    【讨论】:

      【解决方案2】:

      在维基百科页面查看Obtaining geographic coordinates

      【讨论】:

        【解决方案3】:

        Google 提供反向地理编码 - 请通过this 链接

        例如下面的 web 服务会为您提供参数中提供的 lat long 的地址,但 hte 响应是在 json 中,请参阅this 链接

        如果您想在 xml 中获得响应,请检查 here

        【讨论】:

          【解决方案4】:

          我遇到了同样的问题。最后我通过从服务器端获取 lat long 来修复它。 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;
          
          
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-06-12
            • 2013-10-10
            • 1970-01-01
            • 2012-06-24
            • 1970-01-01
            • 2012-04-30
            • 2014-09-04
            • 1970-01-01
            相关资源
            最近更新 更多