java - 从当前位置排序经纬度坐标

标签 java android

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

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

    }
}
}

最佳答案

在伪代码中它应该看起来像这样

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;
    }
}

关于java - 从当前位置排序经纬度坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52610529/

相关文章:

java - Java 中的 "Math.random()"问题

java - Log4j 和 syslogappender

java - 创建和使用自定义 jar 文件时出现问题

java - 不能从静态上下文中引用非静态变量名

java - 将文本中心与给定位置对齐

java - 在 Java Swing 应用程序中创建在线帮助 - 使用 pdf 用户文档

android - Elevation API 给我不合逻辑的结果

android - Android 上的 Facebook 登录按钮导致 ExceptionInInitializerError

android - thread/runnable, handler, runonuithread, asynctask 之间的区别

java - 通过 int 值设置枚举