Draw path problem

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Draw path problem

Postby subbarao » Wed May 05, 2010 2:03 pm

hi all, plz see the below code related to show the draw path from our present location to specified location in Sanfransisco. But I dont get path it shows SAXEXCEPTION plz solve it.


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.drivingdirections);
from = (TextView)findViewById(R.id.from);
to = (TextView) findViewById(R.id.to);
distance =(TextView)findViewById(R.id.distance);
Button back = (Button)findViewById(R.id.btnback);

back.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
finish();

}
});


String name = getIntent().getExtras().getString("GymName");
String address = getIntent().getExtras().getString("Address");
//destination geopoint

int x=Integer.parseInt(getIntent().getExtras().getString("GymLatitude"));
int y=Integer.parseInt(getIntent().getExtras().getString("GymLongitude").substring(1));

mapView = (MapView) findViewById(R.id.map_view);

//to find current location geopoint

locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
strLocationProvider = getLocationProvider(locationManager);

locationManager.requestLocationUpdates(strLocationProvider, 0, 0, locationListener);
Location myloc= locationManager.getLastKnownLocation(strLocationProvider);
gp=getGeoByLocation(myloc);



GeoPoint destGeoPoint = new GeoPoint((int)(x), (int) (-y));


//to draw a path between source (gp) and destination(destGeoPoint) points

DrawPath(gp, destGeoPoint, Color.GREEN, mapView);
mapView.getController().setZoom(10);
mapView.getController().animateTo(gp);
}

private String getLocationProvider(LocationManager location) {
String provider="";
try
{
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);
}
catch(Exception e)
{
e.printStackTrace();
}
return provider;



}

public final LocationListener locationListener = new LocationListener()
{
//call when location changed
@Override
public void onLocationChanged(Location location)
{
//to get current Geopoint
mapView.getController().animateTo(getGeoByLocation(location));
}
@Override
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
}
};



@Override
protected boolean isRouteDisplayed()
{
return false;
}

protected GeoPoint getGeoByLocation(Location location) {
//to get current Geopoint
gp = null;
try
{
if (location != null)
{
geoLatitude = location.getLatitude()*1E6;
geoLongitude = location.getLongitude()*1E6;
gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return gp;


}

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(src.getLatitudeE6() / 1.0E6));
urlString.append(",");
urlString.append(Double.toString(src.getLongitudeE6() / 1.0E6));
urlString.append("&daddr=");// to
urlString.append(Double.toString(dest.getLatitudeE6() / 1.0E6));
urlString.append(",");
urlString.append(Double.toString(dest.getLongitudeE6() / 1.0E6));
urlString.append("&ie=UTF8&0&om=0&output=kml");
Log.d("xxx", "URL=" + urlString.toString());
// get the kml (XML) doc. And parse it to get the coordinates(direction
// route).
Document doc = null;
HttpURLConnection urlConnection = null;
URL url = null;
try {
url = new URL(urlString.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(urlConnection.getInputStream()); //while debugging here shows SAXException

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

// src
GeoPoint startGP = new GeoPoint((int) (Double
.parseDouble(lngLat[1]) * 1E6), (int) (Double
.parseDouble(lngLat[0]) * 1E6));
mMapView01.getOverlays()
.add(new MyOverLay(startGP, startGP, 1));
GeoPoint gp1;
GeoPoint gp2 = startGP;
for (int i = 1; i < pairs.length; i++) // the last one would be

{
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));
mMapView01.getOverlays().add(new MyOverLay(gp1, gp2, 2, color));
Log.d("xxx", "pair:" + pairs[i]);
}
mMapView01.getOverlays().add(new MyOverLay(dest, dest, 3)); // use
// the
// default
// color
}
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (ParserConfigurationException e)
{
e.printStackTrace();
} catch (SAXException e)
{
e.printStackTrace();
}
}

}
subbarao
Freshman
Freshman
 
Posts: 6
Joined: Wed Mar 03, 2010 6:09 am
Location: Hyderabad

Top

Postby kekomal » Wed May 05, 2010 4:57 pm

Can you post the output of e.printStackTrace()?
kekomal
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Mar 17, 2010 10:44 am
Location: Bilbao, Spain

Postby edinky » Tue May 11, 2010 9:59 am

whether your network behide the proxy /?
edinky
Once Poster
Once Poster
 
Posts: 1
Joined: Tue May 11, 2010 9:31 am
Location: china

Re: Draw path problem

Postby Kirisawa » Fri Aug 20, 2010 3:48 pm

It could other problems specific to XML. I had some problems in the past concerning JAVA and XML. For example you might be dealing with BOM. This also gave me a SAXException.

The Byte-Order-Mark (or BOM), is a special marker added at the very beginning of an Unicode file encoded in UTF-8, UTF-16 or UTF-32. It is used to indicate whether the file uses the big-endian or little-endian byte order. The BOM is mandatory for UTF-16 and UTF-32, but it is optional for UTF-8.
Kirisawa
Freshman
Freshman
 
Posts: 5
Joined: Fri Aug 20, 2010 3:06 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: Exabot [Bot] and 3 guests