【问题标题】:Sorting lat long coordinates from current location从当前位置对经纬度坐标进行排序
【发布时间】:2018-10-02 23:01:58
【问题描述】:

我正在尝试根据与用户当前位置的距离对经纬度坐标列表进行排序。我试过使用比较器,但似乎无法让它工作。我尝试了许多不同的方法来实现比较器,包括使用数组列表,并尝试了许多不同的网络教程。我正在考虑使用一个集合然后对其进行排序,但不知道如何。我在正确的轨道上吗?我的代码如下。如果有人能指出我正确的方向,那就太好了。非常感谢!

package com.example.qdogg.mealmapnb;

 //@Author Quinn Clark 2018
//


/**
 *Sorting based on proximity soon
 */

 import android.content.Intent;
 import android.location.Location;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ListView;

 import com.google.android.gms.maps.model.LatLng;
 import com.google.android.gms.maps.model.Marker;

 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;

 public abstract class SleepMap extends MainActivity implements 
OnItemClickListener, LocationProviderSleepMap.LocationProviderListener {
ListView listView;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sleep_map);

    listView = (ListView) findViewById(R.id.sleeplist);
    listView.setOnItemClickListener(this);
}

@Override
public void gotCurrentLocation(Location location) {
    final Location CLSM = new Location("");
    CLSM.setLatitude(location.getLatitude());
    CLSM.setLongitude(location.getLongitude());

    Location IFCSM = new Location("");
    IFCSM.setLatitude(51.042580);
    IFCSM.setLongitude(-114.062782);

    Location BGCSM = new Location("");
    BGCSM.setLatitude(51.039370);
    BGCSM.setLongitude(-114.084020);

    float CLSMtoIFCSM = CLSM.distanceTo(IFCSM);
    float CLSMtoBGC = CLSM.distanceTo(BGCSM);

}

@Override
public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
    switch (position) {
        case 0:
            Intent innfromthecold = new Intent(this, ifcactivity.class);
            startActivity(innfromthecold);
            break;
        case 1:
            Intent boysandgirlsclub = new Intent(this, bgcactivity.class);
            startActivity(boysandgirlsclub);
            break;
        default:
            //Nothing

    }
}
}

【问题讨论】:

  • gotCurrentLocation() 对一些 局部变量 进行一些计算,因此当方法返回时,这些计算值会丢失。您使用ArrayListCompartor 进行描述,但问题中没有包含任何代码。请更新尝试失败的详细信息。

标签: java android


【解决方案1】:

在伪代码中应该是这样的

public class DistanceComparator implements Comparator<Location> {
    private Location current;

    public DistanceComparator(Location loc) {
        current = loc;
    }

    @Override
    public int compare(Location l1, Location l2) {
        int distance1 = calculateDistance(current, l1); // your code to calculate distance
        int distance2 = calculateDistance(current, l2);

        if (distance1 < distance2) return -1;
        if (distance1 == distance2) return 0;
        return 1;
    }
}

【讨论】:

  • 您能解释一下您是如何计算距离的吗?我对此很陌生,感谢您的帮助。
  • @QuinnClark 的确切代码取决于您的 Location 类代码。
  • 我怎样才能适应多个条目?我看到你有 if (distance1 &lt; distance2) return -1; if (distance == distance2) return 0, ; 。这可以在多个位置工作吗?
  • @QuinnClark 你说的多个位置是什么意思?您将您的位置添加到集合(列表、数组)中,然后使用给定的比较器对该集合进行排序。该比较器将根据与给定位置的距离对集合中的元素进行排序
  • 对不起,我应该澄清一下。当我阅读您的示例时,在我看来,只有当我想将一个距离与另一个距离进行比较时,该代码才有效。不过,我还会有更多。我认为如果我只是重复它,然后创建另一个比较器以找到其他比较器中最接近的点,它可能会起作用,但这似乎非常复杂,我相信有更好的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 2013-07-06
  • 2020-12-11
  • 2014-09-05
相关资源
最近更新 更多