2012-08-11 95 views
1

我使用這種方法來繪製在我的應用在地圖上的兩個點之間的路線在Android MapView上繪製兩個GeoPoint之間的路線時出錯?

private void DrawPath(GeoPoint src, GeoPoint dest, int color, 
      MapView mMapView01) { 


     // connect to map web service 
     StringBuilder urlString = new StringBuilder(); 
     urlString.append("http://maps.google.com/maps?f=d&hl=en"); 
     urlString.append("&saddr=");//from 
     urlString.append(Double.toString((double)src.getLatitudeE6()/1.0E6)); 
     urlString.append(","); 
     urlString.append(Double.toString((double)src.getLongitudeE6()/1.0E6)); 
     urlString.append("&daddr=");//to 
     urlString.append(Double.toString((double)dest.getLatitudeE6()/1.0E6)); 
     urlString.append(","); 
     urlString.append(Double.toString((double)dest.getLongitudeE6()/1.0E6)); 
     urlString.append("&ie=UTF8&0&om=0&output=kml"); 
     Log.d("xxx","URL="+urlString.toString()); 


     try 
     { 
      URL url = new URL(urlString.toString()); 
      urlString = null; 
      HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setDoOutput(true); 
      urlConnection.setDoInput(true); 
      urlConnection.connect(); 

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      Document doc = db.parse(urlConnection.getInputStream()); 

      if(doc.getElementsByTagName("GeometryCollection").getLength()>0) 
      { 

      String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ; 
      Log.d("xxx","path="+ path); 
      String [] pairs = path.split(" "); 
      String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height 
      // src 
      GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6)); 

      GeoPoint gp1; 
      GeoPoint gp2 = startGP; 



      for(int i=1;i<pairs.length;i++) // the last one would be crash 
      { 
       lngLat = pairs[i].split(","); 
       gp1 = gp2; 
       // watch out! For GeoPoint, first:latitude, second:longitude 
       gp2 = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6)); 



       mapRouteOverlay = new MapRouteOverlay(gp1,gp2,2,color); 

       mMapView01.getOverlays().add(mapRouteOverlay); 
       Log.d("xxx","pair:" + pairs[i]); 
       Log.e("Check ","Draw " + mapRouteOverlay); 
      } 

      } 
     } 
     catch (MalformedURLException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (ParserConfigurationException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (SAXException e) 
     { 
      e.printStackTrace(); 
     } 


     } 

這種方法是工作的罰款(從最近三個月)繪製路徑,但形成大約15天它給人的問題,尚未出現/繪製路徑。

我logcat的輸出

08-11 10:15:47.363: W/System.err(401): org.xml.sax.SAXParseException: unterminated entity ref (position:ENTITY_REF &@1:808 in [email protected]) 
08-11 10:15:47.373: W/System.err(401):  at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:153) 
08-11 10:15:47.373: W/System.err(401):  at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:110) 
08-11 10:15:47.373: W/System.err(401):  at com.map.MapMainActivity.DrawPath(MapMainActivity.java:621) 
08-11 10:15:47.373: W/System.err(401):  at com.map.MapMainActivity.getDirection(MapMainActivity.java:582) 
08-11 10:15:47.373: W/System.err(401):  at com.map.MapMainActivity$DownloadWebPageTask.onPostExecute(MapMainActivity.java:1318) 
08-11 10:15:47.373: W/System.err(401):  at com.map.MapMainActivity$DownloadWebPageTask.onPostExecute(MapMainActivity.java:1) 
08-11 10:15:47.373: W/System.err(401):  at android.os.AsyncTask.finish(AsyncTask.java:417) 
08-11 10:15:47.373: W/System.err(401):  at android.os.AsyncTask.access$300(AsyncTask.java:127) 
08-11 10:15:47.373: W/System.err(401):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 
08-11 10:15:47.373: W/System.err(401):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-11 10:15:47.373: W/System.err(401):  at android.os.Looper.loop(Looper.java:123) 
08-11 10:15:47.383: W/System.err(401):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
08-11 10:15:47.383: W/System.err(401):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-11 10:15:47.383: W/System.err(401):  at java.lang.reflect.Method.invoke(Method.java:521) 
08-11 10:15:47.383: W/System.err(401):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
08-11 10:15:47.383: W/System.err(401):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
08-11 10:15:47.383: W/System.err(401):  at dalvik.system.NativeStart.main(Native Method) 

我不明白是什麼問題? 有幫助嗎?

在此先感謝。

回答

5

看到這個link絕對幫助你。 ,因爲從2012年7月27日起Google分析KML文件的指示將不再可用(因爲Google已更改檢索Google Directions的結構,現在只能通過JSON或XML獲取),現在可以遷移您的代碼轉換爲JSON而不是KML。

3

如果你的目的只是繪製兩個地理點之間的路徑,那麼你可以使用android內部地圖activity.It容易,沒有錯誤的開銷。

String uri = "http://maps.google.com/maps?saddr=" + currentLatitude+","+currentLongitude+"&daddr="+fixedLatitude+","+fixedLongitude; 
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)); 
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); 
startActivity(intent); 
+0

我的應用程序是目前在市場上,這是不好改變應用程序的流程,我必須表現出路徑我目前的活動只有 – 2012-08-11 05:20:28

+1

+1謝謝你的回答 – 2012-08-14 04:50:37

1

那麼,根據異常,在您正在閱讀的某些XML中出現錯誤(unterminated entity ref)。我想你想繪製的點是從該XML中讀取的,顯然這個XML中有一個錯誤(因爲大概15天左右)。 如果可能,在編輯器中打開XML並查找<ref>,但沒有匹配的</ref>

這時你有兩種選擇:

  1. 修復程序問題產生XML
  2. 試圖抓住在App的SAXParserException並跳過errornous點。
+0

如何解決程序生成XML中的問題,我正在使用文檔生成器。此 – 2012-08-13 06:35:09

+0

的任何替代方法否,Google地圖正在生成XML,您只需閱讀並解析它。由於您無法修復Google地圖,您是否可以嘗試將從urlConnection的輸入流中獲得的內容寫入stdout,以便您可以看到從Google地圖獲取的XML?看看這裏(http://stackoverflow.com/questions/2345861/java-how-do-i-pipe-in​​putstream-to-standard-out)如何做到這一點。如果XML非常長,您還可以將它寫入某個文件以便於編輯/搜索。 – Ridcully 2012-08-13 06:50:33

+0

+1謝謝你的回答 – 2012-08-14 04:51:15

相關問題