Question

I apologize in advance if this question is stupid/not being asked correctly, this is my first post in stackoverflow and I'm completely new at RoR...

I have a rails application which uses the gem gmaps4rails v2.0.4. The application takes in an address (street, city and country), uses geocoder to calculate the latitude and longitude then plugs them into my marker builder.

Every time I refresh my show page, the marker in google maps changes position...I even tried to plug in the actual latitude and longitude ([{"lat":49.245253,"lng":-123.0651361}]) instead of "raw @hash.to_json" and it still renders the marker in different positions every time I refresh.

does anyone know how to fix this?

Note: I tried to copy and paste that coordinate in google maps as well and the marker is very consistent in its position. My controller.rb and show.html.erb are pasted below. Thanks in advance!!

controller.rb

@hash = Gmaps4rails.build_markers(@branches) do |branch, marker|
  marker.lat branch.latitude
  marker.lng branch.longitude
end

show.html.erb

<script type="text/javascript">
  handler = Gmaps.build('Google');
  handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
    markers = handler.addMarkers(<%=raw @hash.to_json %>);
    handler.bounds.extendWith(markers);
    handler.fitMapToBounds();
  });
</script>
Was it helpful?

Solution

Its a feature to prevent overlapping. Its documented in the code here.

You can customise or remove the behaviour, example:

handler = Gmaps.build('Google', { markers: { maxRandomDistance: null } });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top