I'm trying to give a direction to the user using google map from one location to another. I'm using the code bellow but I have no idea why it's not working. I can't figure out the problem everything seems right.
final double latitude = 37.894404;
final double longitude = -122.0660386;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
Location lastKnownLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true));
if(lastKnownLocation != null){
double lat = lastKnownLocation.getLatitude();
double longi = lastKnownLocation.getLongitude();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr="+lat+","+longi+"&daddr="+latitude+","+longitude));
startActivity(intent);
}else{
Toast.makeText(contactus.this,"Coudn't get provider", Toast.LENGTH_SHORT).show();
}
}
I actually got it working and here is the code I used,
final double latitude = 45.894404;
final double longitude = -112.0660386;
LocationManager lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
String towers = lManager.getBestProvider(crit, false);
Location userCurrentLocation = lManager.getLastKnownLocation(towers);
if(userCurrentLocation != null){
double lat = userCurrentLocation.getLatitude();
double longi = userCurrentLocation.getLongitude();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr="+lat+","+longi+"&daddr="+latitude+","+longitude));
startActivity(intent);
}else{
Toast.makeText(contactus.this, "Couldn't locate a provider to find your location", Toast.LENGTH_LONG).show();
}
Don't forget to add the premission to find the user location to you manifest, include it above ,
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>