James Mureithi
James Mureithi

Reputation: 27

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.

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.

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.

Upvotes: 0

Views: 633

Answers (1)

James Mureithi
James Mureithi

Reputation: 27

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.

Upvotes: 1

Related Questions