假设我在一些文件位置可以使用“json”对象,读取文件,评估距离并邀请朋友是任务。
require 'json'
class GeoSearch
attr_accessor :originalLatitude, :originalLongitude, :invitationMap
def initialize
@originalLatitude = 28.521134
@originalLongitude = 77.206567
@invitationMap = Hash.new
end
def degreesToRadians(degrees)
return degrees * Math::PI / 180
end
def findDistance(lat1, long1, lat2, long2)
earthRadiusKm = 6371
deltaLat = degreesToRadians(lat2 - lat1)
deltaLon = degreesToRadians(long2 - long1)
lat1 = degreesToRadians(lat1)
lat2 = degreesToRadians(lat2)
arial = Math.sin(deltaLat/2) * Math.sin(deltaLat/2) + Math.sin(deltaLon/2) * Math.sin(deltaLon/2) * Math.cos(lat1) * Math.cos(lat2)
distance = earthRadiusKm * (2 * Math.atan2(Math.sqrt(arial), Math.sqrt(1-arial)))
distance
end
def readJsonFile(fileName)
file = File.read(fileName)
jsonData = JSON.parse(file)
jsonArray = jsonData["friends"]
iteratorOnJson(jsonArray)
end
def iteratorOnJson(jsonArray)
visitingFriendsIds = []
jsonArray.each do |element|
if(findDistance(@originalLatitude, @originalLongitude, element['latitude'], element['longitude']) <= 100)
visitingFriendsIds << element['id']
@invitationMap[element['id']] = element['name']
end
end
visitingFriendsIds = sortElements(visitingFriendsIds, 0 ,visitingFriendsIds.length-1)
sendInvitationtoFriends(visitingFriendsIds, jsonArray)
end
def sendInvitationtoFriends(visitingFriendsIds, jsonArray)
visitingFriendsIds.each do |friendId|
puts "id: " + friendId.to_s + " name: " + @invitationMap[friendId]
end
end
def sortElements(inputArray, low, high)
if(low < high)
partition = quickSort(inputArray, low, high)
sortElements(inputArray, low, partition-1)
sortElements(inputArray, partition+1, high)
end
inputArray
end
def quickSort(inputArray, low, high)
pivotalPoint = inputArray[high]
index = low
for iterator in low..high do
if(inputArray[iterator] <= pivotalPoint)
swapElements(inputArray, iterator, index)
index = index + 1
end
end
index -1
end
def swapElements(inputArray, xIndex, yIndex)
temp = inputArray[xIndex]
inputArray[xIndex] = inputArray[yIndex]
inputArray[yIndex] = temp
end
end
GeoSearch.new.readJsonFile("/home/neha/Desktop/friends.json")#Add your fileName
如果Json对象看起来像:
# {
# "friends": [
# {
# "id": "5",
# "name": "Agueda",
# "latitude": 28.521134,
# "longitude": 78.206567
# },
# {
# "id": "2",
# "name": "Hisako",
# "latitude": 28.521134,
# "longitude": 74.206567
# },
# {
# "id": "1",
# "name": "Addie",
# "latitude": 28.521134,
# "longitude": 76.206567
# },
# {
# "id": "4",
# "name": "Laure",
# "latitude": 37.5758033375583,
# "longitude": -122.012044535507
# },
# {
# "id": "3",
# "name": "Maye",
# "latitude": 41.77117,
# "longitude": -87.888795
# }
# ]
# }
输出匹配好友(100公里以内)的姓名和用户ID,按用户ID(升序)排序。
有没有办法降低所写方法的复杂性。
答案 0 :(得分:1)
您可以使用Haversine Formula计算距离,然后将该距离添加到用户的哈希值。对其进行排序并选择给定距离内的那些:
require "json"
friends=JSON.parse(DATA.read)['friends']
olat = 28.521134
olon = 77.206567
friends
.map {|h| h['dist']=findDistance olat, olon, h["latitude"], h["longitude"]; h}
.select {|h| h['dist']<100}
.sort_by {|h| h['id']}
.each {|h| puts "%s is %.2f kilometers away" % [h['name'], h['dist']]}
__END__
{
"friends": [
{
"id": "5",
"name": "Agueda",
"latitude": 28.521134,
"longitude": 78.206567
},
{
"id": "2",
"name": "Hisako",
"latitude": 28.521134,
"longitude": 74.206567
},
{
"id": "1",
"name": "Addie",
"latitude": 28.521134,
"longitude": 76.206567
},
{
"id": "4",
"name": "Laure",
"latitude": 37.5758033375583,
"longitude": -122.012044535507
},
{
"id": "3",
"name": "Maye",
"latitude": 41.77117,
"longitude": -87.888795
}
]
}
打印:
Addie is 97.70 kilometers away
Agueda is 97.70 kilometers away