Recently, I've been developing a small app with the assistance of stackoverflow. The main goal is to plot a list of locations onto a searchable/pannable google map. This type of functionality can be seen all over the web. The locations are stored on the backend and then passed to the view by the controller. AJAX is used to avoid reloading the entire page. Here are the scenarios: a) When a user searches for a location via zipcode, the map loads the new location, sends a search request to the server, displays any markers within a set radius, and sets a default zoom level; b) If the user pans or zooms around, the map stays at the user's chosen position, sends a search request with the viewport bounding box to the server, and maps the results. Initially, the map defaults to Seattle, and it attempts to geolocate the user...
By using the gmaps4rails wiki and leveraging a modified version of an answer from a question on stackoverflow (Google Maps for Rails - update markers with AJAX), I have come very close to achieving my goal. The solution works almost perfectly, but there is a minor hitch. Below is an excerpt of how it currently operates:
sightings_controller.rb
// Controller code here
search.html.haml
// HTML code here
search.js.erb
// JavaScript code here
map.js
// More JavaScript code here
The issue I'm encountering revolves around handling the markers when the user pans and zooms without changing the search result. Ideally, I want to avoid calling the "resetMarkers" function in these instances to prevent a flicker of markers on the screen. However, my attempts to achieve this have been unsuccessful so far.
I experimented with creating additional variables to compare old and new marker values, but encountered challenges with maintaining their state throughout the page lifecycle. Another approach involved resubmitting the old hash as a parameter with each search request and setting a flag, but this method felt convoluted.
If anyone has insights on a more effective approach or if there's something crucial I'm overlooking, your input would be greatly appreciated. Thank you!