使用 android.location.Location 靜態方法:
float[] results = new float[1];
Location.distanceBetween(lat1, lng1, lat2, lng2, results);
float distanceInMeters = results[0];
底層採用 Haversine 公式,精度滿足大多數應用。
2dsphere 索引,geoNear 命令毫秒級返回最近 N 個點。若 N < 10000 且頻率低,可在內存中遍歷計算距離,配合經度緯度邊界框預過濾(Math.abs(lat - targetLat) < delta)減少計算次數。
將坐標編碼為 Geohash 字符串,按前綴分桶存儲。查詢時只需檢查目標 Geohash 及其相鄰桶,大幅縮小掃描範圍。Redis、Elasticsearch 均支持 Geohash 索引。
經緯度距離計算公式詳解:movable-type.co.uk/scripts/latlong.html