Jim
Jim

Reputation: 5617

In gmaps4rails, how do I move the map so the marker is in view?

I have a set of locations that I want to bring up, individually through ajax calls, and some of them aren't within the current bounds of the map. Is there a way to move the map so the marker is in view?

I don't need it to be in the center, just as long as it's in view.

Upvotes: 1

Views: 1967

Answers (3)

Jeremy F.
Jeremy F.

Reputation: 1366

Jim's answer was probably right when he posted it. But as of now the right way to do it would be :

var centerpoint = new google.maps.LatLng(lat_value, long_value);
Gmaps.map.map.setCenter(centerpoint);

Upvotes: 2

apneadiving
apneadiving

Reputation: 115511

What you expect is automatically done as long as you pass the auto_adjust setting to true. See here.

Then, you should just use the js function Gmaps4Rails.add_markers described here.

Upvotes: 0

Jim
Jim

Reputation: 5617

I figured it out after some research. I'd still be curious to know how to NOT have to center if the marker is visible on the map.

var centerpoint = new google.maps.LatLng(lat_value, long_value);
Gmaps4Rails.map.setCenter(centerpoint)

edit: Found answer to how to not center map every time. Pseudo code version.
1. Get values from Gmaps4Rails.map.getBounds().
2. Use resulting ta and la values to see if the marker is within those values.
3. If marker is outside of those values, center map, otherwise place marker without centering map.

Thanks guys. I should post more questions. It really helps me to think through my problems.

Upvotes: 2

Related Questions