From 915a1afb2c16e3391815e9fdc89bb401cad90614 Mon Sep 17 00:00:00 2001 From: Alan W Date: Tue, 24 Oct 2017 20:31:42 -0400 Subject: [PATCH] add google map --- Gemfile | 2 +- app/assets/javascripts/google.js | 71 +++++++++++++++ app/assets/stylesheets/application.scss | 17 +++- app/controllers/listings_controller.rb | 29 +++++- app/views/listings/index.html.erb | 116 ++++++++++++------------ config/initializers/assets.rb | 1 + config/routes.rb | 1 + 7 files changed, 174 insertions(+), 63 deletions(-) create mode 100644 app/assets/javascripts/google.js diff --git a/Gemfile b/Gemfile index 2a0d017..36e38e8 100644 --- a/Gemfile +++ b/Gemfile @@ -53,7 +53,7 @@ end gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] group :development, :test do - + gem 'pry-byebug' gem 'rspec-rails' gem 'rails-controller-testing' diff --git a/app/assets/javascripts/google.js b/app/assets/javascripts/google.js new file mode 100644 index 0000000..6751e83 --- /dev/null +++ b/app/assets/javascripts/google.js @@ -0,0 +1,71 @@ +var map; +var markers; + +function getmarkers() { + $.ajax({ + type: 'POST', + dataType: 'json', + url: '/mappoints.json', + success: function({data}) { + addmarkers(data); + console.log(data); + } + }); +}; + +function addmarkers(data) { + var markers = []; + for( i = 0; i < data.length; i++ ) { + var position = new google.maps.LatLng(data[i][1], data[i][2]); + console.dir(position) + var marker = new google.maps.Marker({ + position: position, + map: map, + title: data[i][0] + }); + markers.push(marker); + }; + + fitToMarkers(markers); +}; + +function fitToMarkers(markers) { + var bounds = new google.maps.LatLngBounds(); + + bounds.extend(new google.maps.LatLng('42.6000', '-71.2356')); + bounds.extend(new google.maps.LatLng('42.0000', '-72.2781')); + + for (var i = 0; i < markers.length; i++) { + bounds.extend(markers[i].position); + } + map.setCenter(bounds.getCenter()); + map.fitBounds(bounds); +} + +jQuery(function($) { + var script = document.createElement('script'); + script.src = "//maps.googleapis.com/maps/api/js?sensor=false&callback=initialize"; + document.body.appendChild(script); +}); + +function initialize() { + var bounds = new google.maps.LatLngBounds(); + var mapOptions = { + mapTypeId: 'roadmap' + }; + + map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); + map.setTilt(45); + map.setCenter(new google.maps.LatLng(42.3601, -71.0589)); + + var infoWindow = new google.maps.InfoWindow(), + marker, i; + + getmarkers() + + var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) { + this.setZoom(14); + google.maps.event.removeListener(boundsListener); + }); + +} diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index f5dbde3..0b1243b 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -15,13 +15,13 @@ @import "bootstrap"; .Page-Title { - font-size:30px; - font-weight:bold; - text-align:center; + font-size:30px; + font-weight:bold; + text-align:center; } .panel-footer { - background-color: white; + background-color: white; } .contact-header { @@ -31,3 +31,12 @@ .questions { margin-left: 10%; } + +#map_wrapper { + height: 400px; +} + +#map_canvas { + width: 100%; + height: 100%; +} diff --git a/app/controllers/listings_controller.rb b/app/controllers/listings_controller.rb index 972a80e..bcc7db7 100644 --- a/app/controllers/listings_controller.rb +++ b/app/controllers/listings_controller.rb @@ -3,6 +3,33 @@ class ListingsController < ApplicationController skip_before_action :verify_authenticity_token + def get_map_points + map_points = [{lat:42.3601, long:-71.0589}] + + @listings = Listing.all + + @listings.each do |point| + array = [] + + if point.latitude.to_f > 41.6821 && + point.latitude.to_f < 42.9337 && + point.longitude.to_f < -69.9598 && + point.longitude.to_f > -72.2781 + + array << point.address + array << point.latitude + array << point.longitude + end + + map_points << array + end + + respond_to do |format| + format.html + format.json { render json: { data: map_points } } + end + end + def index @listings = Listing.paginate(:page => params[:page], :per_page => 50) respond_to do |format| @@ -44,7 +71,6 @@ def create def discriminatory @listings = Listing.discriminatory - binding.pry render_listings_formats end @@ -74,5 +100,4 @@ def render_listings_formats format.json {render json: @listings.to_json} end end - end diff --git a/app/views/listings/index.html.erb b/app/views/listings/index.html.erb index 92e34ac..8f806a4 100644 --- a/app/views/listings/index.html.erb +++ b/app/views/listings/index.html.erb @@ -36,64 +36,68 @@ -
-
-
- <%= button_to "Run Scraper", scrapers_path, class: 'btn btn-primary btn-block' %> -
+
+
+
+ +
+
+
+ <%= button_to "Run Scraper", scrapers_path, class: 'btn btn-primary btn-block' %>
-
-
- - - - - - - - - - + +
+
+
headinglisted ataddresslatitudelongitudedescriptiondiscriminatory
+ + + + + + + + + + + + + <% @listings.each do |listing| %> + + + + + + + + - - - <% @listings.each do |listing| %> - - - - - - - - - - <% end %> - -
headinglisted ataddresslatitudelongitudedescriptiondiscriminatory
<%= link_to listing.heading, listing_path(listing) %><%= listing.listed_at %><%= listing.address %><%= listing.latitude %><%= listing.longitude %><%= listing.description %><%= listing.discriminatory %>
<%= link_to listing.heading, listing_path(listing) %><%= listing.listed_at %><%= listing.address %><%= listing.latitude %><%= listing.longitude %><%= listing.description %><%= listing.discriminatory %>
-
diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index 01ef3e6..f01726a 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -9,3 +9,4 @@ # Precompile additional assets. # application.js, application.css, and all non-JS/CSS in app/assets folder are already added. # Rails.application.config.assets.precompile += %w( search.js ) +Rails.application.config.assets.precompile += %w( google.js ) diff --git a/config/routes.rb b/config/routes.rb index ad5dafb..39015de 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,4 +10,5 @@ post '/tools/trackchanges', to: 'trackchanges#get_changes' get '/tools/trackform', to: 'trackchanges#trackform' get '/Contact', to: 'contacts#index' + post '/mappoints', to: 'listings#get_map_points' end