捕捉到道路的Andr​​oid捉到、道路、oid、Andr

2023-09-04 10:06:54 作者:繁花落尽余音未散

我正在写一个Android应用程序,我需要一个纬度/经度值,找到最近的路给它的纬度/经度值的能力。我在 http://econym.org.uk/gmap/snap.htm ,并试图实现这一点,但我不得不使用谷歌地图Web服务,而不是JavaScript的(因为它是一个Android应用程序)。当我做出这样

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

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

它不返回我最近的路了!看来,上述方法不能与web服务工作。有没有人有关于如何解决这个问题的任何其他的想法?

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?

推荐答案

您的网址,似乎很好地工作。

Your URL seems to work perfectly.

下面是我用来测试它的AsyncTask的。

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

在logcat的输出,如果你往下看几行,你会看到:

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.