捕捉到道路 Android

发布于 2024-09-10 11:09:45 字数 429 浏览 9 评论 0原文

我正在编写一个 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

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

I'm writing an Android app and I need the ability to take a lat/long value and find the lat/long value of the nearest road to it. I've read the article at http://econym.org.uk/gmap/snap.htm, and tried to implement this, but I've had to use the Google Maps Webservices rather than javascript (since it's an android app). When I make a request like

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

it doesn't return me the closest road at all! Seems that the above method doesn't work with the webservices. Has anyone got any other ideas about how to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2024-09-17 11:09:46

您的网址似乎工作正常。

这是我用来测试它的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>

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

Your URL seems to work perfectly.

Here is the AsyncTask I used to test it.

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;
}

Within the logcat output if you look down a few lines you should see:

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>

They are the coordinates you are looking for.
I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文