1

I am using this method to draw route between two points on map in my application

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


      }

this method is working fine(from last three months) to draw path but form about 15 days it gives problem and not showing/drawing path.

my logcat output is

 08-11 10:15:47.363: W/System.err(401): org.xml.sax.SAXParseException: unterminated entity ref (position:ENTITY_REF &@1:808 in java.io.InputStreamReader@44f9cde8) 
 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)

I could not understand what is the problem? any help?

Thanks in Advance.

Deepak Swami
  • 3,838
  • 1
  • 31
  • 46

3 Answers3

6

see this link definitely help you. because the Google Directions from Google by parsing the KML file is no longer available since 27 July 2012 (because Google has changed the structure of retrieving Google Directions, now you can only get it by JSON or XML), it is time to migrate your code to JSON instead of KML.

Community
  • 1
  • 1
4

If your purpose is only to draw the path between two geo-points then you can use android internal map activity.It easy and no overhead of having error.

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);
nKandel
  • 2,543
  • 1
  • 29
  • 47
  • my application is currently on market and it is not good to change flow of application i have to show path in my current activity only – Deepak Swami Aug 11 '12 at 05:20
2

Well, according to the Exception there is an error (unterminated entity ref) in some XML you are reading. I guess the points you want to draw are read from that XML and obviously there's an error in that XML (since those about 15 days probably). Open the XML in an editor if that's possible and look for a <ref> with no matching </ref>.

Then you have two options:

  1. Fix the problem in program generating the XML
  2. Try to catch the SAXParserException in your App and skip the errornous point.
Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • how to Fix the problem in program generating the XML.i am using document builder for it. any alternative for this – Deepak Swami Aug 13 '12 at 06:35
  • No, google maps is generating the XML, you're just reading and parsing it. As you cannot fix google maps, can you try to just write what you get from the inputstream of the urlConnection to stdout, so you can see the XML you get from google maps? Look here (http://stackoverflow.com/questions/2345861/java-how-do-i-pipe-inputstream-to-standard-out) on how to do that. If the XML is very long you can also write it to some file for easier editing/searching. – Ridcully Aug 13 '12 at 06:50