2010-07-17 78 views
4

我正在編寫一個Android應用程序,我需要能夠獲取緯度/經度值並找到距離它最近的道路的緯度/經度值。我已閱讀http://econym.org.uk/gmap/snap.htm的文章,並試圖實現這一點,但我不得不使用Google Maps Web服務而不是JavaScript(因爲它是一個Android應用程序)。當我做出這樣對齊道路Android

maps.google.com/maps/api/directions/xml?origin=52.0,0 &目的地的請求= 52.0,0 &傳感器=真

它不返回我的最近的路!似乎上述方法不適用於web服務。有沒有人有任何其他想法如何解決這個問題?

回答

4

你的網址似乎很完美。

這是我用來測試它的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> 

他們是你正在尋找的座標。 我希望這可以幫助。