如何获得在android系统给定的位置和当前位置的实时之间的距离 [英] How to get distance between a given location and current location real time in android

查看:86
本文介绍了如何获得在android系统给定的位置和当前位置的实时之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到用LocationListener的和相关的类和方法在Android的两个地点之间的距离。


  1. 我能够通过使用地理编码类来获取给定地址的坐标。并且还能够使用distanceto方法两个地址之间获得距离。

2.Now我想给地址,并获得坐标,然后得到previous给定的地址位置,并在实时的当前位置(其改变)之间的距离。由于采用的是Android没有当前位置的方法,我得到使用位置监听器。


  1. 的要协调的给定的地址位置之间的距离,并使用LocationListener的和其他的位置的类和方法移动时实时位置变化。我写了和部署清单中的所有权限的设备上,但GPS得到,只要我启动应用程序禁用。下面是我的code

低于code //获取地址位置坐标是成功的。

  INT I = 1;
    地理codeR地理=新的地缘codeR(本);
    清单<地址> AddressList中= geo.getFromLocationName(美国存托凭证,I);
    地址地址= addressList.get(0);
    如果(address.hasLatitude()及&放大器; address.hasLongitude()){
          纬度= address.getLatitude();
          经度= address.getLongitude();
        字符串adlalo =上述地址的纬度:+纬度++
        +上述地址的经度:+ LON;
        t2.setText(adlalo);
    }

//以下是位置监听器类

 公共类LoctListner实现LocationListener的
{
       公共无效onLocationChanged(位置LOC)
    {
      T3 =(的TextView)findViewById(R.id.textView3);
      l1.setLatitude(LAT);
  l1.setLongitude(LON);
        浮D = l1.distanceTo(LOC);
        字符串s =的开始和结束点之间的距离是+ D;
        t3.setText(多个); }}

帮我在哪里,我所犯的错误。无法查看日志,因为我得把设备出test.since这个应用程序的需求,提前移动location.Thanks


解决方案

  

无法查看日志,因为我得把设备出test.since这个程序需要移动位置


也许你忘了打电话requestLocationUpdates()?


  

无法查看日志


您还没有把任何日志。使用Logs.D(YOURTAG,日志报表);

 位置L2 =新的位置();
  l2.setLatitude(loc.getLatitude());
  l2.setLongitude(loc.getLongitude());

^这不是必需的

 浮动门= l1.distanceTo(LOC);

您需要等待几分钟,直到GPS图标停止动画。这意味着,一个修复已经实现。所以检查位置

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

解决方案

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

这篇关于如何获得在android系统给定的位置和当前位置的实时之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆