java - 捕捉到道路安卓

标签 java android google-maps web-services map-directions

我正在编写一个 Android 应用程序,我需要能够获取纬度/经度值并找到离它最近的道路的纬度/经度值。我已经在 http://econym.org.uk/gmap/snap.htm 阅读了这篇文章,并尝试实现这一点,但我不得不使用 Google Maps Webservices 而不是 javascript(因为它是一个 android 应用程序)。当我提出类似

的请求时

maps.google.com/maps/api/directions/xml?origin=52.0,0&destination=52.0,0&sensor=true

它根本没有返回最近的道路!似乎上述方法不适用于网络服务。有没有人对如何解决这个问题有任何其他想法?

最佳答案

您的 URL 似乎工作正常。

这是我用来测试它的 AsyncTask。

public class SnapToRoad extends AsyncTask<Void, Void, Void> {

private static final String TAG = SnapToRoad.class.getSimpleName();

@Override
protected Void doInBackground(Void... params) {
    Reader rd = null;
    try {
        URL url = new URL("http://maps.google.com/maps/api/directions/xml?origin=52.0,0&destination=52.0,0&sensor=true");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setReadTimeout(10000 /* milliseconds */);
        con.setConnectTimeout(15000 /* milliseconds */);
        con.connect();
        if (con.getResponseCode() == 200) {

            rd = new InputStreamReader(con.getInputStream());
            StringBuffer sb = new StringBuffer();
            final char[] buf = new char[1024];
            int read;
            while ((read = rd.read(buf)) > 0) {
                sb.append(buf, 0, read);
            }
            Log.v(TAG, sb.toString());
        } 
        con.disconnect();
    } catch (Exception e) {
        Log.e("foo", "bar", e);
    } finally {
        if (rd != null) {
            try {
                rd.close();
            } catch (IOException e) {
                Log.e(TAG, "", e);
            }
        }
    }
    return null;
}

在 logcat 输出中,如果您向下看几行,您应该会看到:

11-07 16:20:42.880: V/SnapToRoad(13920):     <start_location>
11-07 16:20:42.880: V/SnapToRoad(13920):      <lat>51.9999900</lat>
11-07 16:20:42.880: V/SnapToRoad(13920):      <lng>0.0064800</lng>
11-07 16:20:42.880: V/SnapToRoad(13920):     </start_location>

它们是您要查找的坐标。 我希望这会有所帮助。

关于java - 捕捉到道路安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3272427/

相关文章:

javascript - 谷歌地图引用标记监听器

java - 在 start 方法中与 FXML 元素交互时如何修复 NullPointerException?

java - 如何在 postgres - Hibernate 中设置锁定超时

android - requestCellInfoUpdate() 的正确用法是什么?

java - 我如何要求用户在第一次启动时选择一个设置?

javascript - 仅当谷歌地图空闲秒数时触发事件

javascript - 如何在 map 上绕特定城市转一圈?

java - 将登录页面添加到现有的 angular2 应用程序

java - 如何显示部署在 Tomcat 中的应用程序的维护页面?

android - 如何在Android中调试两个进程?