How can I enable the drawing tools on my map using gmaps4rails? - ruby-on-rails

I've tried: <%= gmaps({"map_options" => #map_options,"markers" => { "data" => #markers}, "libraries" => "drawing", "circles" => { "data" => '[
{"lng": -87.6355, "lat": 41.8886, "radius": 600, "strokeColor": "#FF0000"}
]'
}}) %>
#map_options is just an instance variable where I pass a lat/lng from user's location. everything else works fine but can't load the drawing tools.

Two issues here:
libraries should be in the maps_options hash
it's value should be an Array
Source code.

Related

Rails: gmaps4rails - Start the map location atlocation within the database

I dont think there is need for me to post specific code from my application.
In simple terms I have a db table with name,address,longitude,latitude,gmaps.
Each record stores a location. Okay. When I load the page, the google maps just starts off in the middle of the atlantic ocean, I was wondering is there a way for it to start off on one of my locations that I already have in my table?
Hey when you pass a record to the view you can use it to center the map.
<%= gmaps({
"map_options" => { "auto_adjust" => false, "center_longitude" => #record.long, "center_latitude" => #record.lat, "auto_zoom" => false, "zoom" => 5 },
"markers" => {"data" => #json }
})
%>
When you try this, make sure you have set auto_adjust to false.

Customize mapTypeControlOptions with gmaps4rails gem

Is it possible to customize how the map options are displayed on the google map?
It seems this is an option in the API using mapTypeControlOptions wiht an array of mapTypeIDs.
Thanks to apneadiving for developing this awesome gem!!!
You can add any option google map enables. From the doc here:
If you lack some options for maps and markers, you can simply pass
what you need in :raw:
<%= gmaps(:markers => {:data => #json, :options => { :raw => '{ animation: google.maps.Animation.BOUNCE }' } },
:map_options => { :raw => '{ disableDefaultUI: true, scrollwheel: false }' }) %>

How do I convert an array of strings into a comma-separated string?

I have an array:
array = ["10", "20", "50", "99"]
And I want to convert it into a simple comma-separated string list like this:
"10", "20", "50", "99"
array.join(',') will almost do what you want; it will not retain the quotes around the values nor the spaces after.
For retaining quotes and spaces: array.map{|item| %Q{"#{item}"}}.join(', ')
This will print "\"10\", \"20\", \"50\", \"99\"". The escaped quotes are necessary assuming the question does in fact call for a single string.
Documentation on the %Q: string literals.
You could use inspect as suggested in another answer, I'd say that's personal preference. I wouldn't, go look at the source code for that and choose for yourself.
Useful aside: array.to_sentence will give you a "1, 2, 3 and 4" style output, which can be nice!
["10", "20", "50","99"].map(&:inspect).join(', ') # => '"10", "20", "50", "99"'
Here:
array.map {|str| "\"#{str}\""}.join(',')
Several answers have offered solutions using #map, #inspect, #join. All of them fail to get certain details of CSV encoding correct for edge cases involving embedded commas and/or string delimiters in the elements.
It's probably a better idea to use the stdlib class CSV then to roll your own.
irb> require 'csv'
=> true
irb> a = [10,'1,234','J.R. "Bob" Dobbs',3.14159]
=> [10, "1,234", "J.R. \"Bob\" Dobbs", 3.14159]
irb> puts a.to_csv
10,"1,234","J.R. ""Bob"" Dobbs",3.14159
The map.join solutions are sufficient if this encoding doesn't need to care about embedded delimiters, or is intended for some internal representation only, but they will fail if generating data for exchange with other programs that expect Comma Separated Values (CSV) as a generally understood representation.
The simplest solution is to use the built in ".to_sentence" method.
So
["fred", "john", "amy"].to_sentence outputs "fred, john, and amy"
This is a slightly alternative solution, particularly handy if you need to convert an array with double quoted strings to a single quoted list (for say SQL queries):
"'#{["John Oliver", "Sam Tom"].join("','")}'"
to
'John Oliver', 'Sam Tom'
Attribution: https://alok-anand-ror.blogspot.com/2014/04/ruby-join-array-elements-with-single.html
This is how you can send push notifications using FCM for Android devices.
Assuming you want notify followers when ever a user posts something on their status this is how you do it. This is done in Rails 5.2.6 for Rest Apis--- But still you can use the same for web push notifications. This is for sending to many devices with registration_ids to target followers with notifications.
Gem : fcm
in your controller:
require "fcm"
def create_vibe(user)
#vibe = user.vibes.new(content: #content, picture: #picture, video: #video, isvideofile: #isvideofile, video_thumbnail: #video_thumbnail, source: #source, background_color: #background_color)
#followed = user.followers
if #followed.present?
#registration = #followed.map { |s| s.registration_id }
end
if #vibe.save
fcm = FCM.new("") # set your FCM_SERVER_KEY
options = {
data: {
notification_type: 1,
title: "#{#vibe.user.username} " "has posted a new Vibe",
body: "#{#vibe.content}",
user_data: {
vibe_id: #vibe.id,
user_id: #vibe.user.id,
background_color: #background_color,
},
},
}
response = fcm.send(#registration, options)
puts response
render :status => 200,
:json => { :success => true,
:info => "Vibe posted successfully",
:vibe_info => {
:content => #content,
:picture => #picture,
:video => #video,
:video_thumbnail => #video_thumbnail,
:isvideofile => #isvideofile,
:source => #source,
:fcm => options,
} }
else
render :status => 200, :json => { :success => false, :result => "Vibe can't be blank" }
end
end

How to format scientific data into proper data series for graph display in Ruby (Rails 3.1.x)?

Needing some guidance about how to properly graph data that is very small and stored as BigDecimal.
If anyone has had experience using BigDecimals in any graphing scenario I believe your input on how you placed these into a usable sequence would be invaluable.
Presently I'm using lazy_high_charts and it really seems that this is going to work out exceptionally well, however I've run into a hitch where I've not dealt with data on the minute BigDecimal scale hitherto.
Given my queries, I'll be pulling out about a 1,000 data points for a few different series of data ranging in accuracy from about 0.100E-9 to about 0.100E-1.
What would be the best way to prep these data series for presentation in such a graph that has a scientific application and therefore precision is important? I'm not sure if I could or should continue in BigDecimal or something else?
I'm presently querying the database with a line similar to:
series_a = dataset.order("date").select('data_set.data1').limit(1000).all.zip
I'd appreciate some guidance of going from this result (again, the output is an array of BigDecimals) to the appropriate format for what will go into the chart series.
An contextual example of the code I'm using to build the chart in my controller is:
#h = LazyHighCharts::HighChart.new('graph') do |f|
series_a = dataset.order("date").select('data_set.data1').limit(1000).all.zip
series_b = dataset.order("date").select('data_set.data3').limit(1000).all.zip
f.series(:name => 'Data 1', :data => series_a)
f.series(:name => 'Data 2', :data => series_b)
f.chart({:defaultSeriesType => "line" })
f.yAxis [
{:title => { :text => "Left Y Label", :margin => 10} },
{:title => { :text => "Right Y Label"}, :opposite => true }
]
f.xAxis(:title => { :text => "X Label"} )
f.title(:text => "Graph Title")
f.legend(:align => 'right', :verticalAlign => 'top', :y => 75, :x => -50, :layout => 'vertical')
end
I think I'm a bit further along in my understanding of this having decided to convert the BigDecimals to strings as per "How to access fields by attribute name from ActiveRecord row results?" However it appears ultimately this fails and gives erratic results as the series functions :data field expects, I think, numeric input. I am thinking that this ultimately emits json to highcharts, however I'm still stuck for the moment in my attempt to pass these values on in correctly.
When you do this:
series_a = dataset.order("date").select('data_set.data1').limit(1000).all.zip
you'll end up with an array of arrays as we already discussed elsewhere. If you get rid of the zip, you should end up with an array of objects in series_a. I'd guess that chart would be happier with an array of numbers so:
series_a = dataset.order("date").
select('data_set.data1').
limit(1000).
all.
map(&:data1)
or, if data1 is a BigDecimal (due to using a fixed precision type in the database) then maybe you'd want this:
series_a = dataset.order("date").
select('data_set.data1').
limit(1000).
all.
map { |o| o.data1.to_f }
to get an array of floating point values that the chart should know what to do with.
I'm not familiar with Highcharts so there is some guesswork here.

Google Maps, Ruby on Rails, Zoom level with one marker

I am adding google maps support with apneadiving / Google-Maps-for-Rails (thanks awesome gem)
I am finding one slight glitch, however, which very likely is my fault.
auto_zoom works great when there are multiple markers. However, when there is only one marker it is zoomed in to the max level which is not pretty.
"zoom" will only work when auto_zoom is false, so that's not what I want.
So therefore you could use "maxZoom" but now users cannot zoom in manually beyond that point which is not what I want.
Is there a way around this? Is my explanation making sense? Is this a limitation of Google Maps API?
Thanks...
This behavior is due to the auto_zoom built-in function in the google maps api.
One work around to this is to set it to false in the gmaps method:
<%= gmaps({
"map_options" => { "auto_zoom" => false},
"markers" => { "data" => #json }
})
%>
And then use the gmaps4rails_callback to fit your needs (be sure to have at least version 0.7.9)
<script type="text/javascript" charset="utf-8">
function gmaps4rails_callback() {
if (Gmaps4Rails.markers.length == 1) {
//only one marker, choose the zoom level you expect
Gmaps4Rails.map.setZoom(2);
}
else{
//more than one marker, let's auto_zoom
Gmaps4Rails.map_options.auto_zoom = true;
Gmaps4Rails.adjust_map_to_bounds();
}
}
</script>
I achieved this in a slightly different way as I know that I'll only ever have one marker on my map. I'm relatively new to rails, but this method feels a bit "cleaner" than using JS in your view.
I've got lat and lng stored in my model (encoded by geokit at time of creation), so did the following in my view:
<%= gmaps({
"map_options" => {"auto_zoom" => false, "zoom" => 15, "center_latitude" => #listing.lat, "center_longitude" => #listing.lng },
"markers" => {"data" => #markers }
})
%>
#markers is my JSON created by blah.to_gmaps4rails, and "listing" is my model.
thanks this helped me...
{"auto_zoom" => false, "zoom" => 15, "center_latitude" => #listing.lat, "center_longitude" => #listing.lng },
"markers" => {"data" => #markers }
})
%>

Resources