[英]Geocoder,s gem near method not working on rails 5
Been trying to use the geocoder gem to find locations using the geocoders method near for finding certain locations near to certain points. 试图使用geocoder宝石通过附近的geocoders方法来查找位置,以找到靠近某些点的特定位置。
def index
@data = Datum.all
@emergency = Emergency.last
@data_near = Datum.near([@emergency.longitude,@emergency.latitude],100,:units => :km)
@geojson = Array.new
@data_near.each do |mapdata|
@geojson << {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [mapdata.longitude, mapdata.latitude]
},
properties: {
name: mapdata.description,
address: mapdata.address,
BusType: 'hospital',
popupContent: "#{mapdata.address} is a #{mapdata.description}",
:'marker-color' => '#00607d',
:'marker-symbol' => 'circle',
:'marker-size' => 'medium'
}
}
end
respond_to do |format|
format.html
format.json{ render json: @geojson }
end
end 结束
That's the code i am trying to run,it returns an empty array even when the locations near within the specified parameters are present. 那就是我要运行的代码,即使存在指定参数内附近的位置,它也会返回一个空数组。 When i try to run the code directly in the rails console with real points.eg.
当我尝试直接在带有真实点的rails控制台中运行代码时。
data = Datum.near([36.7689503,-1.381776],10,:units => :km)
I do not get any error generated but the output i get is less than readable or comprehensible.The output is: 我没有产生任何错误,但是我得到的输出少于可读性或可理解性。输出为:
Datum Load (0.3ms) SELECT data.*, (111.19492664455873 * ABS(data.latitude - 36.7689503) * 0.7071067811865475) + (96.29763124613503 * ABS(data.longitude - -1.381776) * 0.7071067811865475) AS distance, CASE WHEN (data.latitude >= 36.7689503 AND data.longitude >= -1.381776) THEN 45.0 WHEN (data.latitude < 36.7689503 AND data.longitude >= -1.381776) THEN 135.0 WHEN (data.latitude < 36.7689503 AND data.longitude < -1.381776) THEN 225.0 WHEN (data.latitude >= 36.7689503 AND data.longitude < -1.381776) THEN 315.0 END AS bearing FROM "data" WHERE (data.latitude BETWEEN 36.67901813940813 AND 36.85888246059187 AND data.longitude BETWEEN -1.4940430256783075 AND -1.2695089743216923) ORDER BY distance ASC LIMIT ? [["LIMIT", 11]]
=> #<ActiveRecord::Relation []>
Any help will be highly appreciated. 任何帮助将不胜感激。
t.string "longitude"
t.string "latitude"
Had saved up my coordinates in the database with the string data-type which is wrong ,the coordinates should be saved with the float data attribute.This solved my problem for anyone that maybe experiencing the same problem.Its a good place to start checking. 如果我用错误的字符串数据类型将我的坐标保存在数据库中,则坐标应该使用float data属性保存。这对于可能遇到相同问题的任何人都解决了我的问题。这是开始检查的好地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.