如何在不使用谷歌地图的情况下获取特定城市或街道的纬度和经度?

6

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

4个回答

10

这被称为地理编码,需要将地名匹配到一个纬度和经度。

你可以在应用中编写一个已知的地名/纬度-经度列表,或者在运行时读取它们,并在需要时进行搜索。另外,你也可以使用在线地理编码服务,比如Yahoo提供的服务,或者Google提供的服务。


2
Google提供反向地理编码 - 请通过this链接进行操作。
此外,例如下面的Web服务可以为您提供所提供参数的纬度和经度的地址,但响应是以JSON格式返回的,请参见this链接。
如果您想获得XML格式的响应,请查看here

2

1

我曾经遇到过同样的问题。最终,我通过从服务器端获取经纬度来解决了这个问题。这个链接提供了从Java中获取经纬度的示例代码。希望能对某些人有所帮助。

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;


    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接