How to get distance between a given location and current location real time in android

大兔子大兔子 提交于 2019-12-13 12:38:44

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/6530271/how-to-get-distance-between-a-given-location-and-current-location-real-time-in-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!