gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:53:38 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com