Ruby on Rails Gem for Google Map Integration

Ruby on rails gem for google map integration

I've used the YM4R, Georuby and spatial adapter gems with good results before.
see this description.

Ror Gem to point out location on google map by clicking on link

I like gmaps4rails.

For other potential options:

  • Check out the Geocoding & Maps section at The Ruby Toolbox
  • Search RubyGems for "maps"
  • Search google for "google maps rails gem"

Adding Google Maps (API V3) support to a Ruby on Rails app

Fyi, I've just released a new version of gmaps4rails.

It's based on Rails 3 and uses Google maps API V3:

https://github.com/apneadiving/Google-Maps-for-Rails

Google maps - setting it up to work with Rails 5 - geocoder not geocoding

As usual, start simple and slowly add more code.
I used the basic Rails app from my previous answer.

I added gem 'geocoder' to Gemfile.

After bundle install, I added

geocoded_by :name   # can also be an IP address
after_validation :geocode

to my Marker class, which already has latitude and longitude defined as :decimal attributes.

In rails c :

Marker.create(:name => 'New Dehli')
Marker.create(:name => 'New-York')

Done, they appear on the map!

NOTE: If for some reason some Marker doesn't have coordinates, the map will not be displayed, and you'll get a "too much recursion" Error in js console.
To avoid this, you can make sure all your Markers have coordinates by enabling after_validation :geocode in your model, and launching this in in your console :

Marker.where(latitude: nil).or(Marker.where(longitude: nil)).each{|marker| marker.save}

Saving the marker triggers the validation, which triggers the geocoding.

If for some reason you still have non-geocoded Markers, change your js to :

function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4
});
var bounds = new google.maps.LatLngBounds();

var n = markers.length;
for (var i = 0; i < n; i++) {
var lat = markers[i].latitude;
var lng = markers[i].longitude;
if (lat == null || lng ==null){
console.log(markers[i].name + " doesn't have coordinates");
}else {
var marker = new google.maps.Marker({
position: {lat: parseFloat(lat), lng: parseFloat(lng)},
title: markers[i].name,
map: map
});
bounds.extend(marker.position);
}
}
map.fitBounds(bounds);
}

How does Rails Geocoder Gem access Google API?

Geocoder.configure(
:lookup => :google_premier,
:api_key => [ 'GOOGLE_CRYPTO_KEY', 'GOOGLE_CLIENT_ID', 'GOOGLE_CHANNEL' ],
:timeout => 5,
:units => :km,
)

https://github.com/alexreisner/geocoder

Here is one more link : http://hankstoever.com/posts/11-Pro-Tips-for-Using-Geocoder-with-Rails

under
Some common configuration options are:

You should look into this answer : Does Geocoder gem work with google API key?

It says:

Geocoder supports Google api keys for Google Premier accounts only.

But you can use the Client-Side framework to do that, instead of putting it on the server

https://developers.google.com/maps/articles/geocodestrat

I wrote something like that a few days back in ruby, if it helps :

require 'net/http'
require "resolv-replace.rb"
require 'json'

url = URI("https://maps.googleapis.com/maps/api/geocode/json")
puts "enter country code"
c_code = gets
puts "enter zip code "
zip = gets

url.query= URI.encode_www_form({ address: "#{c_code.chomp}+#{zip.chomp}" })
res = Net::HTTP::get_response(url)

json_data = res.body if res.is_a?(Net::HTTPSuccess)

data = JSON.parse(json_data)
p data

Rails Google Api to Display map in homepage with points saved in database

try below code:

<iframe id="map"  width="100%" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=gateshead&aq=&sll=54.9489662,-1.6148245&sspn=0.149605,0.338173&ie=UTF8&hq=&hnear=169+Saltwell+Rd,+Gateshead,+Tyne+and+Wear+NE8+4XH,+Kerajaan+Inggris&ll=54.9489662,-1.6148245&spn=0.299124,0.676346&t=m&z=11&output=embed"></iframe>

Add your lat lang in above url



Related Topics



Leave a reply



Submit