I am trying to find distance between two locations in android using the LocationListener and related classes and methods.
- I was able to get the coordinates of the given address by using geo coding class. And also was able to get distance between two addresses using the distanceto method.
2.Now I want to give address and get coordinates and then get the distance between previous given address location and the current location (which changes) in real time. since there is no current location method in android i got to use Location Listener.
- the want the distance between the given address location coordinate and the real time changing location when moving using locationlistener and other location classes and methods. I wrote and deployed on a device with all permissions in manifest, but the gps gets disabled as soon as i start the app. Below is my code
//below code to get the address location coordinates which is succesful
int i=1;
Geocoder geo=new Geocoder(this);
List<Address> addressList = geo.getFromLocationName(adrs,i);
Address address = addressList.get(0);
if(address.hasLatitude() && address.hasLongitude()){
la开发者_C百科t = address.getLatitude();
lon = address.getLongitude();
String adlalo= "Latitude of above address: "+lat +" " +
" "+"Longitude of above address: "+lon;
t2.setText(adlalo);
}
// and below is the location listener class
public class LoctListner implements LocationListener
{
public void onLocationChanged(Location loc)
{
t3 = (TextView) findViewById(R.id.textView3);
l1.setLatitude(lat);
l1.setLongitude(lon);
float d=l1.distanceTo(loc);
String s="the distance between starting and ending point is "+d;
t3.setText(s);
}
}
help me where i made the mistake. Unable to see the log since i got to take the device out to test.since this app needs a moving location.Thanks in advance
Unable to see the log since i got to take the device out to test.since this app needs a moving location
Perhaps you forgot to call requestLocationUpdates() ?
Unable to see the log
You have not put any Logs . Use Logs.D("YOURTAG", "Logs statements");
Location l2 = new Location("");
l2.setLatitude(loc.getLatitude());
l2.setLongitude(loc.getLongitude());
^ This is not required
float door = l1.distanceTo(loc);
You have to wait for a few minutes till the GPS Icon stops animating. This means that a fix has been attained. So check if loc
is null
The Geocoder is useless.You can not get address by latlng with the Geocoder. You can use Gson to parse a json doc from Google maps Api.The json contains your address info.
精彩评论