2

I am trying to find distance between two locations in android using the LocationListener and related classes and methods.

  1. 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.

  1. 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()){
          lat = 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

visista
  • 246
  • 4
  • 15
  • the lat,lon are doubles and l1,l2 are Location objects declared outside oncreate of the activity. – visista Jun 30 '11 at 05:29

2 Answers2

0

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

Reno
  • 33,594
  • 11
  • 89
  • 102
  • locman.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, minDistance, loclis); actually did call it before those above code blocks... And yeah it shows a null pointer exception if i dont move... understandable but even on move it doesn;t get updated. thanks for your answer – visista Jun 30 '11 at 12:41
  • and yeah i can straight away use loc instead of creating a new location l2. thank you. – visista Jun 30 '11 at 13:14
  • How long have you waited for a location, are you inside a building? – Reno Jun 30 '11 at 13:34
  • I am testing it outside while driving... it takes 30secs at least to fix the location. at max 1 min...... I think i got whats going wrong here my address location needs to be given by user... meanwhile since location is changing the locationlistener is getting called and since the address location is null by that time it is producing a App not resp error... I think i need to run the locationlistener logic in background using asynctask. – visista Jun 30 '11 at 13:59
  • now the getfromlocation method is giving probs... Unable to parse response from server .. and it keeps on coming everytime. what shud be the best way – visista Jun 30 '11 at 15:49
  • Try using [this](http://stackoverflow.com/questions/5222164/geocoder-getfromlocation-throws-exception/5222243#5222243) – Reno Jul 01 '11 at 02:10
  • i found the reason ...the wifi strength was very weak to get response from the backend service for geocoding. so disabled it and it fetched the data with 3g very fast. thanks – visista Jul 01 '11 at 13:05
0

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.

lanyimo
  • 367
  • 1
  • 3
  • 13