我有一个工作代码,它使用 geocoder
在谷歌地图上显示多个标记,例如
@nearbys = Place.near("#{params[:address]}", 5,:order => "distance",:units => :km)
@nearbys.first(5)
我使用下面的代码显示标记
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);
// adding Multiple Markers from ruby object
var markers = [
['<%= @nearbys[0].title %>', '<%= @nearbys[0].latitude %>','<%= @nearbys[0].longitude %>'],
['<%= @nearbys[1].title %>', '<%= @nearbys[1].latitude %>','<%= @nearbys[1].longitude %>'],
['<%= @nearbys[2].title %>', '<%= @nearbys[2].latitude %>','<%= @nearbys[2].longitude %>'],
['<%= @nearbys[3].title %>', '<%= @nearbys[3].latitude %>','<%= @nearbys[3].longitude %>']
,
['<%= @nearbys[4].title %>', '<%= @nearbys[4].latitude %>','<%= @nearbys[4].longitude %>']
];
// adding Info Window Content
var infoWindowContent = [
['<div class="info_content" >' +
'<h3><%= @nearbys[0].title %></h3>' +'</br>'+
'<p><%= @nearbys[0].about %></p>' +
'</div>'],
['<div class="info_content">' +
'<h3><%= @nearbys[1].title %></h3>' +
'<p><%= @nearbys[1].about %></p>' +
'</div>'],
['<div class="info_content">' +
'<h3><%= @nearbys[2].title %></h3>' +'</br>'+
'<p><%= @nearbys[2].about %></p>' +
'</div>'],
['<div class="info_content">' +
'<h3><%= @nearbys[3].title %></h3>' +'</br>'+
'<p><%= @nearbys[2].about %>/p>' +
'</div>'],
['<div class="info_content">' +
'<h3><%= @nearbys[4].title %></h3>' +'</br>'+
'<p><%= @nearbys[4].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
但问题是:- 如果@nearbys 的计数少于 5,那么这段代码会失败。所以我一直在尝试使用 jquery 数组使其动态化,但它不起作用,因为我认为我的代码缺少一些可以工作的东西它是动态的。 它应该是这样的..
var array=<%= @nearbys%>;
var markers=[];
var infowindows=[];
array.each(function(index){
markers.push('<%= @nearbys[0].title %>', '<%= @nearbys[0].latitude %>','<%= @nearbys[0].longitude %>');
infowindows.push('<div class="info_content" >'+'<h3><%= @nearbys[0].title %></h3>' +'</br>'+
'<p><%= @nearbys[0].about %></p>'+'</div>');
})
..那么我如何才能实现这个........使这个动态化,这样上面的工作代码就不会随着新添加的代码而中断。
最佳答案
在与 jquery arrays
和 json
斗争之后,我找到了这个最好的解决方案......这在所有情况下都有效,我需要的只是处理空条件和空条件,我已经做到了并且它按照我需要的方式工作。希望这对某人有帮助
我的 Controller
def show_nearby_locations
@nearbys = Place.near("#{params[:address]}", 5,:order => "distance",:units => :km)
@nearbys.first(10)
end
在我的 View 文件中
<%= javascript_tag do%>
window.nearbys= <%=raw @nearbys.to_json %>;
<%end%>
这是我在 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
关于ruby-on-rails - 在谷歌地图上动态显示标记-Rails 3.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24832979/