I'm currently developing a sales rep tracking tool and am stuck at showing a Markers array for the geocoded objects in a map. I'm using geocoder's near function to find geocoded objects 0.5km's from my location passed via the url. My current code works as long as there is only one object within that radius and I know that I need to use an Array for showing more than one object, but I've been stuck at this for the past 2 weeks.
stores_controller.rb
def index
@latitude = params[:latitude].to_f
@longitude = params[:longitude].to_f
@stores = current_user.stores
@locations = @stores.near([@latitude, @longitude], 0.5)
end
The map on my stores/index.html.erb
function initMap() {
<% @locations.each do |store| %>
var lat = <%= store.latitude %>
var lng = <%= store.longitude %>
var mylatlng = {lat: lat, lng: lng}
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: mylatlng
});
var marker = new google.maps.Marker({
position: mylatlng,
map: map,
animation: google.maps.Animation.DROP,
});
google.maps.event.addListener(marker, 'click', function () {
var c = confirm('Would you like to visit this store?')
if (c === true) {
window.location.href = '<%= store_path(store) %>';
}
if (c === false) {
window.location.href = '<%= dashboard_path %>';
}
});
var contentString = 'Please click on this Marker to visit' + ' ' + '<%= store.storename %>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
infowindow.open(map, marker);
});
}
<% end %>
The way the app is set up there will be at most two or 3 objects at a time, and I know that json is an option, but I'm hopeful that there would be a way to avoid doing that. All of the resources I've read about map arrays involve manually passing the arrays parameter's, but I need to do it dynamically, seeing as though the object will vary from rep to rep.
Thanks in advance for any assistance.