我目前正在开发销售代表跟踪工具,并且坚持要为地图中的地理编码对象显示标记数组。我使用geocoder的near函数来查找从我的位置通过URL传递的0.5km的地理编码对象。我的当前代码只要在该半径内只有一个对象就可以工作,并且我知道我需要使用Array来显示多个对象,但过去两周我一直坚持这样做。Ruby on Rails - 地理编码器对象作为Google地图标记阵列
stores_controller.rb
def index
@latitude = params[:latitude].to_f
@longitude = params[:longitude].to_f
@stores = current_user.stores
@locations = @stores.near([@latitude, @longitude], 0.5)
end
在我的商店地图/ index.html.erb
function initMap() {
<% @locations.each do |store| %>
var lat = <%= store.latitude %>
var lng = <%= store.longitude %>
var mylatlng = {lat: lat, lng: lng}
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: mylatlng
});
var marker = new google.maps.Marker({
position: mylatlng,
map: map,
animation: google.maps.Animation.DROP,
});
google.maps.event.addListener(marker, 'click', function() {
var c = confirm('Would you like to visit this store?')
if (c === true) {
window.location.href = '<%= store_path(store) %>';
}
if (c === false) {
window.location.href = '<%= dashboard_path %>';
}
});
var contentString = 'Please click on this Marker to visit' + ' ' + '<%= store.storename %>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
infowindow.open(map, marker);
});
}
<% end %>
应用程序设置会出现在在同一时间最多两个或3个对象的方式,我知道JSON是一种选择,但我希望有一种方法可以避免这样做。我读过的有关地图数组的所有资源都涉及到手动传递数组参数,但我需要动态地执行它,看起来好像对象会因代表不同而不同。
在此先感谢您的帮助。
感谢您的回复,我仍然对Rails,JS,JQuery和Google地图都很陌生,并且一直在关注这个主题的教程。我试过你的代码,它不会抛出错误,但不显示地图或任何标记。使用Chrome开发者工具,它也不会抛出任何错误,但似乎并不想工作。我也尝试了一下这些代码,但无济于事。难道我不得不为标记使用标记数组,看起来好像在位置数组中一次可以加载多个商店?再次感谢您的帮助。 –