地理编码的以下结果让我很困惑
前两个是正确的。最后一个显然是错误的。在这些计算之间,代码中没有任何更改。关于如何调试这个有什么想法吗?
Geocoder::Calculations.distance_between("allentown,pa","scranton,pa")
#=> 56.604020682719295
Geocoder::Calculations.distance_between("allentown,pa","harrisburg,pa")
#=> 77.94099956445362
Geocoder::Calculations.distance_between("allentown,pa","bethlehem,pa")
#=> 3365.993496166768
对于"伯利恒;
results = Geocoder.search("bethlehem,pa")
results.size # => 8
并不是所有这些结果都在美国中
results.map { |r| r.country_code }
=> ["br", "us", "us", "us", "us", "us", "us", "us"]
巴西的打击首当其冲。如果结果令人震惊,那么就采用这个(请参阅下面的代码(。我认为巴西伯利恒排在第一位,因为该州被称为"伯利恒";pará;其更接近(使用任何度量的字符串距离("0";pa";比";宾夕法尼亚州";
distance_between
https://github.com/alexreisner/geocoder/blob/f7a83fac8cf8564b79d017091004cbb9d406e4ae/lib/geocoder/calculations.rb#L84
将最终调用coordinates
https://github.com/alexreisner/geocoder/blob/f7a83fac8cf8564b79d017091004cbb9d406e4ae/lib/geocoder.rb#L28
它看起来像这样:
def self.coordinates(address, options = {})
if (results = search(address, options)).size > 0
results.first.coordinates
end
end
因此,按国家增加你的位置字符串,你会得到不同的结果:
Geocoder::Calculations.distance_between("allentown,pa, USA","bethlehem,pa, USA")
# => 4.97795894130203
它可以归结为作为字符串的位置描述,因为字符串通常不是唯一的。有多个巴黎,伯尔尼,当然还有伯利恒:-(
因此,也有多个allentown也就不足为奇了:
require "geocoder"
allentowns = Geocoder.search("allentown,pa")
bethlehems = Geocoder.search("bethlehem,pa")
allentowns.each do |allentown|
bethlehems.each do |bethlehem|
distance = Geocoder::Calculations.distance_between([allentown.latitude, allentown.longitude], [bethlehem.latitude, bethlehem.longitude])
puts "#{allentown.address} -> #{bethlehem.address}: #{distance}"
end
end
显示有Allentown,Lehigh County,Pennsylvania,United States和Allentown,Pittsburgh,Allegheny County,宾夕法尼亚州