Google Maps, Ruby on Rails, Zoom level with one marker
This behavior is due to the auto_zoom
built-in function in the google maps api.
One work around to this is to set it to false
in the gmaps
method:
<%= gmaps({
"map_options" => { "auto_zoom" => false},
"markers" => { "data" => @json }
})
%>
And then use the gmaps4rails_callback
to fit your needs (be sure to have at least version 0.7.9)
<script type="text/javascript" charset="utf-8">
function gmaps4rails_callback() {
if (Gmaps4Rails.markers.length == 1) {
//only one marker, choose the zoom level you expect
Gmaps4Rails.map.setZoom(2);
}
else{
//more than one marker, let's auto_zoom
Gmaps4Rails.map_options.auto_zoom = true;
Gmaps4Rails.adjust_map_to_bounds();
}
}
</script>
gmaps4rails single marker auto zoom
Ok, the interface changed a lot since I posted the answer you quote.
<%= gmaps("markers" => {"data" => @json, "options" => {"auto_zoom" => false} }) %>
<% content_for :scripts do %>
<script type="text/javascript" charset="utf-8">
Gmaps.map.callback = function() {
if (Gmaps.map.markers.length == 1) {
//only one marker, choose the zoom level you expect
setTimeout(function() { Gmaps.map.serviceObject.setZoom(5);}, 50);
}
else{
//more than one marker, let's auto_zoom
Gmaps.map.map_options.auto_zoom = true;
Gmaps.map.adjustMapToBounds();
}
}
</script>
<% end %>
Gmaps4rails V2 - change default zoom
remove:
handler.fitMapToBounds();
Replace with:
handler.getMap().setZoom(yourValue);
how restrict zoom-out level Google maps?
in gmap4rails we use minZoom and maxZoom option and its works for us
below our code that we use thanks
= gmaps({"map_options" => {"auto_zoom" => false,"minZoom" => 2, "maxZoom" => 15,"zoom" => 2}, "markers" => {"data" => @latest_projects_json }})
Android Google Maps API - show marker up to certain zoom
This can be easily done using zoom listeners and setVisible functions.
Checkout this SO post for coding details:
How to make a marker appear or disappear based on zoom level on Google Maps v2
Show markers on google maps dynamically -Rails 3.2
well after struggling with jquery arrays
and json
,i was able to find this best solution...this is working in all scenarios and all i need is to handle empty and null conditions,which i already did and its working the way i needed.HOPE THIS HELPS SOMEONE
my controller
def show_nearby_locations
@nearbys = Place.near("#{params[:address]}", 5,:order => "distance",:units => :km)
@nearbys.first(10)
end
in my view file
<%= javascript_tag do%>
window.nearbys= <%=raw @nearbys.to_json %>;
<%end%>
this is my script inside view
function initialize() {
var map;
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
mapTypeId: 'roadmap'
};
// Display a map on the page
map = new google.maps.Map(document.getElementById("map-canvas-guest"), mapOptions);
map.setTilt(45);
var markers=[];
var infoWindowContent=[];
$.each(nearbys, function(index) {
console.log( data[0][index].latitude,data[0][index].longitude );
markers.push([nearbys[index]['title'], nearbys[index]['latitude'],nearbys[index]['longitude']]);
infoWindowContent.push(['<div class="info_content" >' +
'<h3 style="color: rgb(<%= rand(0..200)%>,<%= rand(0..200)%> ,<%= rand(0..200)%>);">'+nearbys[index]['title']+'</h3>' +'</br>'+
'<p>'+nearbys[index]['about']+'</p>' +
'</div>']);
});
// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow(), marker, i;
// Loop through our array of markers & place each one on the map
for( i = 0; i < markers.length; i++ ) {
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0]
});
// Allow each marker to have an info window
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infoWindow.setContent(infoWindowContent[i][0]);
infoWindow.open(map, marker);
}
})(marker, i));
// Automatically center the map fitting all markers on the screen
map.fitBounds(bounds);
}
// Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
this.setZoom(14);
google.maps.event.removeListener(boundsListener);
});
}//initialize ends
Related Topics
Ruby on Rails: How to Explicitly Define Plural Names and Singular Names in Rails
How to Find the Namespace/Module Name Programmatically in Ruby on Rails
How to Configure Ruby on Rails with Oracle
Flattening Nested Hash to a Single Hash with Ruby/Rails
How to Get the Current Time as 13-Digit Integer in Ruby
Rails: Postgres Permission Denied to Create Database on Rake Db:Create:All
How to Select Every Nth Item in an Array
Escaping Single and Double Quotes in a String in Ruby
Ruby Hash Default Value Behavior
How to Print Out the Contents of an Object in Rails for Easy Debugging
Differencebetween Integer and Fixnum
Error: Failed to Build Gem Native Extension When Installing Rails on MAC Mountian Lion Os
How to Install Ruby 1.9.3 in MAC Os X Lion
"Whenever" Gem Running Cron Jobs on Heroku