一直在尝试使用 geocoder gem 来使用 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
结尾
这就是我要运行的代码,即使存在指定参数内的附近位置,它也会返回一个空数组。当我尝试使用真实点直接在rails控制台中运行代码时。例如。
data = Datum.near([36.7689503,-1.381776],10,:units => :km)
我没有生成任何错误,但我得到的输出不可读或难以理解。输出是:
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 []>
任何帮助将不胜感激。