下载地址: https://www.pan38.com/share.php?code=pvvmX 提取码:8888
声明:仅供学习参考用途
import java.util.*;
public class NearbyPeopleFinder {
private static final double EARTH_RADIUS = 6371; // 地球半径(km)
public static List<User> findNearbyUsers(User currentUser, List<User> allUsers, double radiusKm) {
List<User> result = new ArrayList<>();
for (User user : allUsers) {
if (user.equals(currentUser)) continue;
double distance = calculateDistance(
currentUser.getLatitude(), currentUser.getLongitude(),
user.getLatitude(), user.getLongitude());
if (distance <= radiusKm) {
result.add(user);
}
}
return result;
}
private static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return EARTH_RADIUS * c;
}
}
class User {
private String userId;
private double latitude;
private double longitude;
// 构造函数、getter和setter方法
public User(String userId, double latitude, double longitude) {
this.userId = userId;
this.latitude = latitude;
this.longitude = longitude;
}
public double getLatitude() { return latitude; }
public double getLongitude() { return longitude; }
public String getUserId() { return userId; }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="@+id/avatar"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/default_avatar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="16dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"/>
<TextView
android:id="@+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/gray"/>
</LinearLayout>
</LinearLayout>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。