首页 \ 问答 \ 使用android.location.Location.distanceTo(startLocation)获取有时增加的距离,有时会减少?(use android.location.Location.distanceTo(startLocation) to get the distance that sometimes increase and sometimes reduce?)

使用android.location.Location.distanceTo(startLocation)获取有时增加的距离,有时会减少?(use android.location.Location.distanceTo(startLocation) to get the distance that sometimes increase and sometimes reduce?)

在我的Android运行应用程序中,我使用android.location.Location.distanceTo(startLocation)来获取我开始运行的startLocation和我停止运行的endLocation之间的距离。 但是距离有时会增加,有时它会在我跑步时减少。 有谁帮我解决了这个问题?

我的代码是这样的:

LocationManager locationManager = (LocationManager) getSystemService(
                    Context.LOCATION_SERVICE);
String provider = LocationManager.GPS_PROVIDER;
Location startLocation = locationManager
                        .getLastKnownLocation(provider);
LocationListener locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        double distance = location.distanceTo(startLocation)/1000;
    } 

    @Override
    public void onStatusChanged(String provider, int status,
            Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
};
locationManager.requestLocationUpdates(provider, 2000, 3,
                        locationListener);

In my android Running App, I use android.location.Location.distanceTo(startLocation) to get the distance between startLocation where i start running and endLocation where i stop running. But the distance sometimes increase, sometimes it reduce when i run. anyone help me to solve the question?

my code like this :

LocationManager locationManager = (LocationManager) getSystemService(
                    Context.LOCATION_SERVICE);
String provider = LocationManager.GPS_PROVIDER;
Location startLocation = locationManager
                        .getLastKnownLocation(provider);
LocationListener locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        double distance = location.distanceTo(startLocation)/1000;
    } 

    @Override
    public void onStatusChanged(String provider, int status,
            Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
};
locationManager.requestLocationUpdates(provider, 2000, 3,
                        locationListener);

原文:https://stackoverflow.com/questions/37644466
更新时间:2022-04-03 13:04

最满意答案

您不需要具有smallest=array[i] ,只需使用INTEGER.MAX_VALUEarray[0]初始化变量,并迭代数组,将值与此变量进行比较。

这是在O(n)时间O(1)空间中实现的 ,这是你能得到的最好的! :)

一种更简单的方法

 int[] array ={-1, 2, 1};
 boolean max_val_present = false;  

 int min = Integer.MAX_VALUE;

 for(int i=0;i<array.length;i++) // Loop to find the smallest number in array[]
 {
      if(min > array[i] && array[i] > 0)
         min=array[i];
      //Edge Case, if all numbers are negative and a MAX value is present
      if(array[i] == Integer.MAX_VALUE)
        max_val_present = true;
 }

 if(min == Integer.MAX_VALUE && !max_val_present) 
 //no positive value found and Integer.MAX_VALUE 
 //is also not present, return -1 as indicator
    return -1; 

 return min; //return min positive if -1 is not returned

You do not need to have smallest=array[i], just initialize a variable with INTEGER.MAX_VALUE or array[0] and iterate over the array comparing the value with this variable.

This is achieved in O(n) time and O(1) space and thats the best you can get! :)

a simpler way would be

 int[] array ={-1, 2, 1};
 boolean max_val_present = false;  

 int min = Integer.MAX_VALUE;

 for(int i=0;i<array.length;i++) // Loop to find the smallest number in array[]
 {
      if(min > array[i] && array[i] > 0)
         min=array[i];
      //Edge Case, if all numbers are negative and a MAX value is present
      if(array[i] == Integer.MAX_VALUE)
        max_val_present = true;
 }

 if(min == Integer.MAX_VALUE && !max_val_present) 
 //no positive value found and Integer.MAX_VALUE 
 //is also not present, return -1 as indicator
    return -1; 

 return min; //return min positive if -1 is not returned

相关问答

更多
  • 最简单的方法是对数组进行排序,然后返回前两个元素和最后两个元素。 使用slice()可以防止数组本身被排序: var numbers = [2, 4, 9, 2, 0, 16, 24]; var sorted = numbers.slice().sort(function(a, b) { return a - b; }); var smallest = sorted[0], secondSmallest = sorted[1], ...
  • 你可以使用列表推导: >>> some_list = [-5, -1, -13, -11, 4, 8, 16, 32] >>> max([n for n in some_list if n<0]) -1 >>> min([n for n in some_list if n>0]) 4 You can just use list comprehensions: >>> some_list = [-5, -1, -13, -11, 4, 8, 16, 32] >>> max([n for n in some ...
  • 在一般情况下,您的问题没有很好的解决方案,因为这些值以具有有限精度的浮点格式存储,并且只能精确存储分母2的分数。 例如0.1 * 10可能不是您平台上的整数值。 如果用积分量计算数组中的值,则应将它们表示为具有足够大小的整数对的归一化分数,并计算其分母的最小公倍数。 如果您想要近似解决问题,您可以指定epsilon的值,并手动设计解决方案。 我可以不考虑库函数来满足这个需求,但是一个暴力解决方案很容易编写: unsigned long long ullgcd(unsigned long long a, un ...
  • 在这些方面: int t[][] = new int[2][3]; int smallest = t[0][0]; 你从 smallest 开始就是0.所以除非你输入一个负数,否则它永远不会改变。 你可以使用: int t[][] = new int[2][3]; int smallest = Integer.MAX_VALUE; 确保输入的第一个号码被使用。 另外,你的缩进风格有些不同寻常。 或 for (...) { // Code } 要么 for (...) { // Code ...
  • 您不需要具有smallest=array[i] ,只需使用INTEGER.MAX_VALUE或array[0]初始化变量,并迭代数组,将值与此变量进行比较。 这是在O(n)时间和O(1)空间中实现的 ,这是你能得到的最好的! :) 一种更简单的方法 int[] array ={-1, 2, 1}; boolean max_val_present = false; int min = Integer.MAX_VALUE; for(int i=0;i
  • 如果迭代数组并构建一个图形,你在A或B中遇到的每个唯一整数变成一个顶点,并且每对整数A [i]和B [i]在两个顶点之间创建一个边,那么这个图就会最多有2×N个顶点,因此该图占用的空间与N呈线性关系。 然后,对于图中的每个边,我们将决定它的方向,即它连接的两个整数中的哪一个将在数组C中使用。最后,我们将再次迭代数组,对于每对整数,我们看一下图中相应的边缘知道要使用哪个整数。 我认为确定图中方向所需的操作都可以在N的线性时间内完成(如果我错了就纠正我),因此整个算法应该具有O(N)时间和空间复杂度。 决定图中 ...
  • 你可以尝试,最大限度的否定: list(Arr).index(max(Arr[Arr<0])) 在上面, Arr[Arr<0]将得到所有数字小于0或负数,并且将max应用于列表将给出负数的最大值。 然后,它可以与index一起使用以获取列表中的号码索引。 对于积极的分钟: list(Arr).index(min(Arr[Arr>0])) You can try, for max of negative: list(Arr).index(max(Arr[Arr<0])) In above, Arr[Ar ...
  • 重复直到循环以i = 1开始并且为每个循环添加2将允许您定位奇数索引。 minIx变量控制是否找到正的最小值以及它具有哪个索引。 i := 1; minIx := 0; minVal := 0; repeat if (arr[i] > 0) then begin // Positive value if (minIx = 0) or (arr[i] < minVal) then begin minVal := arr[i]; minIx := i; end; ...
  • 处理真正的小数字或真正大数字时使用科学记数法: double reallyTiny = 1.0e-16; // .0000000000000001 但从最小的数字开始的最好方法是使用: double theTiniestPositive = DBL_MIN; // 2.2250738585072014e-308 Use scientific notation when dealing with really small or really large numbers: double re ...
  • 该算法似乎很适合查找数组的最小值。 如果对arraym中包含的可能元素没有任何了解,则需要检查每个元素,因此您的性能是线性的(与数组中元素的数量成正比)。 如果元素是完全有序的,则可以查找第一个(或最后一个,取决于或排序)元素(恒定时间性能) 如果元素是半有序的(例如堆结构),那么可以通过类似于二进制搜索的技术(log(n)性能) 如果元素无序,但您知道它们不能小于某个值,则可以在找到尽可能最小的值时停止搜索。 所以回到超时的问题:如果元素是无序的,并且没有其他限制,那么你的算法是好的,所以超时必须来自程序 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)