我有一个对象数组,其中包含一些地址数据,包括纬度和经度。
我有一个计算两组纬度/经度坐标之间距离的函数。
我需要将我的数组从一组特定的经/纬度中从最接近到最远排序。
我正在尝试使用arr.sort(comparefunction)
,但到目前为止还没有运气。
可以在我的比较函数中使用外部函数吗?
这是我尝试的代码。
function getDistance (lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1/180
var radlat2 = Math.PI * lat2/180
var theta = lon1-lon2
var radtheta = Math.PI * theta/180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
if (dist > 1) {
dist = 1;
}
dist = Math.acos(dist)
dist = dist * 180/Math.PI
dist = dist * 60 * 1.1515
if (unit=="K") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
return dist
}
let distributors = [
{
city: "A",
lat: 44.3384925,
long: -59.6993207,
name: "Name 1",
},
{
city: "B",
lat: 44.3384925,
long: -78.6993207,
name: "Name 2",
},
{
city: "C",
lat: 64.3384925,
long: -39.6993207,
name: "Name 3",
}
];
let sortedDist = distributors.sort(function(a, b){
if(getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K") < getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K")){
return -1;
}
if(getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K") > getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K")){
return 1;
}
return 0;
})
因此,在这种情况下,数组中的第二个元素距离44.3617171302915, -79.65860486970848
最近,但是我console.log(sortedDist)
并没有任何改变。
答案 0 :(得分:3)
问题是您正在将from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
from intents import fallback_intent, getLocation
import random
app = Flask(__name__)
location_fallback = ['What kind of restaurant are you seeking?', 'What kind? Nearby, Cheap or The best?']
welcome = ['hello', 'what\'s up', 'hey','hi', 'what\'s happening?']
near = ['near', 'nearby']
cheap = ['cheap', 'good for my pockets']
good = ['good', 'top rated']
intro_resp = ['''Hey! Welcome to Crave! This interactive platform connects you to the top foodies in the world! We provide you with the best food places where ever you are. The instructions are simple:
1. Save our number in your Phone as Crave.
2. Text us and tell us what type of food you are craving!
This is from python''', '''
Welcome to Crave! Are you ready to get some food for today?
1. Save our number in your Phone as Crave.
2. Text us and tell us what type of food you are craving!
''']
@app.route('/sms', methods=['GET','POST'])
def sms():
num = request.form['From']
msg = request.form['Body'].lower()
resp = MessagingResponse()
#welcome intent
if any(word in msg for word in welcome):
if any(near_word in msg for near_word in near):
resp.message('These are the location of places near you!')
print(str(msg.split()))
return str(resp)
elif any(cheap_word in msg for cheap_word in cheap):
resp.message('These are the location of places that are low cost to you!')
return str(resp)
elif any(good_word in msg for good_word in good):
resp.message('These are the best places in town!')
return str(resp)
else:
location_fallback[random.randint(0,1)]
resp.message(intro_resp[random.randint(0, 1)])
print(str(msg.split()))
return str(resp)
else:
resp.message(fallback_intent())
print(str(msg))
return str(resp)
if __name__ == '__main__':
app.run(debug=True)
与a
进行比较,请尝试:
a
或语法较短的相同:
let sortedDist = distributors.sort(function(a, b){
if(getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K") < getDistance(44.3617171302915, -79.65860486970848, b.lat, b.long, "K")){
return -1;
}
if(getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K") > getDistance(44.3617171302915, -79.65860486970848, b.lat, b.long, "K")){
return 1;
}
return 0;
})