i want to geocode my location and display with google-maps-for-rails gem
view:
= gmaps({ "map_options" => { "auto_adjust" => true } })
= form_tag geocode_geo_locations_path, :method => :get, :remote => true do
= text_field_tag 'address'
= submit_tag 'search'
in my controller i have:
def geocode
#location = Gmaps4rails.geocode(params[:address])
if #location
#location_hash = #location.map { |loc| {:longitude => loc[:lng], :latitude => loc[:lat], :address => loc[:matched_address]} }.first
end
end
and in the view geocode.js.erb
Gmaps4Rails.replace_markers(<%=raw #location_hash.to_json %>);
it work so far, that i get a marker on the map, however the map doesnt get auto_adjusted .
how can i accomplish that??
Ok... Indeed you just noticed a bug :)
Corrected now in 0.8.2.
Thanks!
Related
I am simply trying to plot a map for the distance_matrix Google API that shows the current location of one object and the distance between the other. (Simple GPS) using lon and lat -- Currently, both objects are configured correctly to support the Distance Matrix Map, but I cannot get the map to show in the view. I don't know if I am coding the parameters correctly. How can I setup my Distance Matrix helper method to show the map in the view with the current_location and time that it takes to reach the destination?
trucks_helper.rb
distance_matrix_map [Doesn't work]
def distance_matrix_map_for(location, options={})
params = {
:origins => [location.lat, location.lon].join(", "),
:zoom => 15,
:size => "500x500",
:destinations => [location.delivery.delivery_order.order.lat, location.delivery.delivery_order.order.lon].join(","),
:sensor => true
}.merge(options)
query_string = params.map{|k, v| "#{k}=#{v}"}.join("&")
image_tag "http://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&#{query_string}", :alt => location.current_location_title, class: 'img-fluid img-rounded'
end
static_map_for builder [Works]
def static_map_for(location, options = {})
params = {
:center => [location.lat, location.lon].join(","),
:zoom => 15,
:size => "300x300",
:markers => [location.lat, location.lon].join(","),
:sensor => true
}.merge(options)
query_string = params.map {|k, v| "#{k}=#{v}"}.join("&")
image_tag "http://maps.googleapis.com/maps/api/staticmap?#{query_string}", :alt => location.current_location_title, class: 'img-fluid img-rounded'
end
Here is the model
class Admin::Filter < ActiveRecord::Base
validates_uniqueness_of :name
has_many :filter_values,class_name: "Admin::FilterValue",foreign_key: "admin_filter_id"
enum field_type: [:select_tag,:select_tag_without_search,:check_box_tag]
def underscore_filter_name
if self.name.split.size > 1
self.name.replace(self.name.scan(/[A-Z][a-z]*/).join("_"))
else
"#{self.name.downcase}_filter"
end
end
end
The function I am talking about is underscore_filter_name. Now I am calling this inside the rails console like this: Admin::Filter.first.underscore_filter_name which returns a value but when I try similarly inside the view it throws an error. Here is the view:
-#filters.each do |filter|
%legend
=filter.name
-case filter.field_type
-when "select_tag"
= simple_form_for :"#{filter.underscore_field_name(filter)}",:url=> admin_requests_path,:method => "get",html: {:"data-filter"=>"#{filter.underscore_field_name}"} do |f|
= f.select("#{filter.name}", filter.filter_values.all.collect {|p| [ p.name, p.id ] }, {:include_blank => "Please select a #{filter.name}"},{:multiple => true,class: "form-control chosen-select select_tag_filter"})
-when "select_tag_without_search"
= select_tag "#{filter.name}", options_for_select(filter.filter_values.all.collect{ |u| [u.name, u.id]}), { :multiple => true,class: "search-free-chosen-select"}
-when "check_box_tag"
= simple_form_for :priority,:url=> admin_requests_path,:method => "get",html: {id: "priority_filter"} do |f|
= f.collection_check_boxes "#{filter.name}", filter.filter_values,:id,:name, :item_wrapper_class => 'inline'
The below line is the error I am getting:
undefined method `underscore_field_name' for #<Admin::Filter:0x007f07a322f068>
Why is this? I am using Rails 4.1
You have defined underscore_filter_name as the method name and you have typed in the view as underscore_field_name. So is the error.
Change it to
= simple_form_for :"#{filter.underscore_filter_name(filter)}",:url=> admin_requests_path,:method => "get",html: {:"data-filter"=>"#{filter.underscore_field_name}"} do |f|
This should resolve the error.
You've got in the view...
underscore_field_name
but the name of the method is
underscore_filter_name
I am working on a project that is a backend for a mobile app.
Una of the API calls returns a large amount of data as a json and it takes a lot to genrate the json.
Right now, I am pre processing the data to generate a hash with all the information and the
Places.each do |item|
place = item.place
place.discounts.each do |discount|
response_item = {
:id => place.id,
:latitude => item.latitude,
:longitude => item.longitude,
:name => place.name,
:url_image => place.img,
:stars => 0,
:is_habitue => false,#is_habitue,
:discount => {
:id => discount.id,
:title => discount.title,
:description => discount.description,
:raw_title => discount.raw_title,
:expiration => discount.expiration
}
}
categories = []
place.categories.each do |category|
categories.append ({
:name => category.name,
:label => category.label
})
end
response_item[:categories] = categories
benefits = []
discount.benefits.each do |benefit|
benefits.append ({
:benefit_type => benefit.benefit_type,
:label => benefit.label
})
end
response_item[:benefits] = benefits
processed_places.append response_item
end
end
render :json => {:places=>processed_places}, :status=>200
I takes about 1.4 seconds to process 2700 results but more than 6 seconds to generate the json.
thanks
Maybe you could try RABL. It's got great features :)
I have an application where I'm showing a business listing and then displaying various jobs related to that business. The job locations are working great but I'd like to add a marker for the business which should be in the center which I can then customise.
Here is my controller/business_controller
def show
#business = Business.find(params[:id])
#distance = #business.service_area
#jobs = Job.near(#business.location, #distance, {:order => :distance, :units => :km})
#json = #jobs.to_gmaps4rails
#mapoptions = {
"map_options" => {"center_latitude" => #business.latitude,
"center_longitude" => #business.longitude,
"auto_zoom" => true}
}
respond_to do |format|
format.html
end
end
In my view views/businesses/show
<p><%= gmaps(:map_options => #mapoptions,"markers" => { "data" => #json }) %></p>
How can I create a marker for the business location?
I'd say:
markers_array = JSON.parse #jobs.to_gmaps4rails
markers_array << {lat: #business.latitude, lng: #business.longitude}
#json = markers_array.to_json
I have a view which contain multiple links:
<% a.each do |q| %>
<%= link_to "stock it",
{ :action => "stock",
:qid => q.question_id,
:qur => q.question_answers_url,
:qti => q.title } ,
:remote => true %>
<div id="<%= "stock" + q.question_id.to_s %>"></div>
<% end %>
Each link generate AJAX-request. Here is a controller:
def stock
if(!Later.where(:question_id => params[:qid]).exists?)
later = Later.new(:question_id => params[:qid], :name => params[:qti], :url => params[:qur])
later.save
end
respond_to do |format|
format.js { render :layout=>false }
end
end
Now return to the view. Each link has a 'div' with unique id='stock'. When user press the link I need to add text to specific div with corresponding id.
I have a stock.js.erb file:
$("#stock<number>").html("some text");
How can I pass div-id to stock.js.erb and how can I use it ?
Common use is to add object.id to your DOM id. That what you exactly did:
<div id="<%= "stock_#{q.question_id}" %>"></div>
Then in your controller you shoud define your question_id or your exact question:
def stock
if(!Later.where(:question_id => params[:qid]).exists?)
later = Later.new(:question_id => params[:qid], :name => params[:qti], :url => params[:qur])
later.save
end
#question_id = params[:qid]
end
Now it will be shared with your stock.js.erb file:
$("#stock_<%= #question_id %>").html("some text");