0

This rails app displays a map on the search result page when the user searches for hairdressers in different locations. I would like the center of the google maps to be on the marker representing the address of the hairdressers that are displayed in the search results. I have tried various things like centre_changed but cant seem to get it right, as the bounds are extended to include all markers at all times or fix on one marker despite the location searched for. Any help would be greatly appreciated.

this is the map.js file

window.myMap = {};

var map;
var bounds = new google.maps.LatLngBounds();
var marker;

myMap.init = function() {
  var coords = $('#map-canvas').data('coords');


var options = {
    zoom: 14,
    center: new google.maps.LatLng('43.6476863', '-79.3885854'),
    mapTypeId: google.maps.MapTypeId.ROADMAP
}

map = new google.maps.Map($('#map-canvas')[0], options);

if (coords) {
var _this = this;
coords.forEach(function(coord) {
  _this.addMarker(coord.latitude, coord.longitude, coord.image, coord.note);
});
map.fitBounds(bounds);
}

google.maps.event.addListener(map, 'center_changed', function() {
// 3 seconds after the center of the map has changed, pan back to the
// marker.
 window(function() {
  map.panTo(marker.getPosition());
  });
});

google.maps.event.addListener(marker, 'click', function() {
  map.setZoom(8);
  map.setCenter(marker.getPosition());
});

};

var infoWindow = new google.maps.InfoWindow({
  content: ''
});

myMap.addMarker = function(latitude, longitude, image, note) {
  marker = new google.maps.Marker({
  position: new google.maps.LatLng(latitude, longitude),
  map: map,
  note: note,
  icon: image,
  clickable: true
});
bounds.extend(marker.position);

google.maps.event.addListener(marker, 'mouseover', function() {
  infoWindow.content = this.note;
  infoWindow.open(this.map, this);
});

google.maps.event.addListener(map, 'click', function() {
  infoWindow.close();
});

map.setOptions({styles: styles});
}


var styles = [
 {
   stylers: [
    { hue: "#00ffe6" },
    { saturation: -50 }
   ]
 },{
   featureType: "road.arterial",
   elementType: "geometry",
   stylers: [
     { lightness: 100 },
     { visibility: "simplified" }
   ]
 },{
   featureType: "road",
   elementType: "labels",
   stylers: [
     { visibility: "off" }
   ]
 }
];

$(document).on('ready page:load', function() {
  if ($('#map-canvas').length) {
    myMap.init();
  }
});

searches_controller.rb

class SearchesController < ApplicationController
  # before_action :set_search, only: [:show, :edit, :update, :destroy]

  # respond_to :html
  def index 
    @hairdressers = Hairdresser.all.order("created_at DESC")
    @pictures = Picture.all
    @categories = Category.all
  end

  def new
    @search = Search.new
  end

  def create
    @search = Search.new(set_search)
    if @search.save
      redirect_to @search 
    else
      render "new"
    end
  end

  def show
    @search = Search.find(params[:id])
    @hairdressers = Hairdresser.all.order("created_at DESC")
    @coords = [] 
    @hairdressers.each do |r|
      @coords << {latitude: r.latitude.to_f, longitude: r.longitude.to_f, note: r.first_name + ' ' + r.last_name + ',' + ' ' + r.salon_address}
    end
  end


  private

  def set_search 
    params.require(:search).permit(:search, :area, :hairdresser_id, :first_name, :last_name, :price, :style, :name, :search_type,:perm_price, :cut_price, :treatment_price)

  end

end

search.rb

class Search < ActiveRecord::Base

     def hairdressers
      @hairdressers ||= find_hairdressers
     end

     def pictures
       @pictures ||= find_pictures
     end

  private

     def find_hairdressers
        hairdressers = Hairdresser.order(:first_name)

        hairdressers = hairdressers.where("first_name like ?", "%#{first_name}%") if first_name.present?
        hairdressers = hairdressers.where("price like ?", "%#{price}%") if price.present?
        hairdressers = hairdressers.where("style like ?", "%#{style}%") if style.present?
        hairdressers = hairdressers.where("area like ?", "%#{area}%") if area.present?
        hairdressers
     end

     def find_pictures
       pictures = Picture.order(:imageable_id)
       pictures = Picture.joins(:categories).where("categories.name like ? ",:"%#{name}%") if name.present?
       pictures
     end




end
amyru1
  • 1
  • 2
  • check the accepted answer here: http://stackoverflow.com/questions/10634199/find-center-of-multiple-locations-in-google-maps – cristian Jan 16 '15 at 18:06
  • thanks very much for responding. I have tried to use the answer from the other question but dont seem to get the results, maybe I am not putting the code in the right place as I am new to javascript. I have tried to add the code to where I have the extend bounds function and also underneath the chords function in initialize. – amyru1 Jan 16 '15 at 22:40

0 Answers0