God Jul
Jag håller på och försöker skapa en gpsfunktion till min app men den gör så att mobilen kraschar när den ska hitta min position, rita ut startpoint och endpoint samt en linje mellan de. Får meddelandet att aktiviteten inte svarar. Tyvärr kommer inget upp på DDMS/Logcat. Finns det något annat sätt att lösa det här på?
Kod:
public class MapsActivity extends MapActivity {
private MapView mapView;
private LocationManager locationManager;
private String strLocationProvider;
private String TAG;
private GeoPoint endPoint;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapView = (MapView) findViewById(R.id.myMapView1);
mapView.setBuiltInZoomControls(true);
mapView.displayZoomControls(true);
SharedPreferences preferences = getSharedPreferences("distrubutionCompany", 0);
String adress = preferences.getString("adress", "");
System.out.println("VAD ÄR DET FÖR ADRESS"+ adress) ;
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
strLocationProvider = getLocationProvider(locationManager);
locationManager.requestLocationUpdates(strLocationProvider, 0, 0, locationListener);
endPoint = getGeoPoint(adress);
mapView.getController().setZoom(20);
}
private GeoPoint getGeoPoint (String adress) {
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
GeoPoint p = null;
try {
List<Address> addresses = geoCoder.getFromLocationName(
adress, 5);
if (addresses.size() > 0) {
p= new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
}
} catch (IOException e) {
e.printStackTrace();
}
return p;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public String getLocationProvider(LocationManager locationManager)
{
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)
{
Log.d(TAG, e.toString());
e.printStackTrace();
}
return provider;
}
public final LocationListener locationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
System.out.println("testar om jag kommer hit1");
try
{
if (location != null) {
GeoPoint startPoint = getGeoByLocation(location);
clearOverlays(); // rensa alla lager
System.out.println("testar om jag kommer hit4");
DrawPath(startPoint, endPoint, Color.RED, mapView);
System.out.println("testar om jag kommer hit2");
System.out.println("Vad är startpoint och endpoint?" + startPoint + endPoint);
mapView.getController().animateTo(getGeoByLocation(location));
mapView.getController().setZoom(20);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
@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
}
};
private GeoPoint getGeoByLocation(Location location)
{
GeoPoint gp = null;
try
{
if (location != null)
{
double geoLatitude = location.getLatitude()*1E6;
double 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((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());
// 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());
// draw many lines for each layer
if(doc.getElementsByTagName("GeometryCollection").getLength()>0)
{
//String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
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));
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 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));
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();
}
}
public void clearOverlays() {
mapView.getOverlays().clear();
}
}