【发布时间】:2017-07-03 05:13:38
【问题描述】:
我目前正在开发一个销售代表跟踪工具,并且一直在为地图中的地理编码对象显示标记数组。我正在使用地理编码器的近功能来查找通过 url 传递的距我的位置 0.5 公里的地理编码对象。只要该半径内只有一个对象,我当前的代码就可以工作,并且我知道我需要使用数组来显示多个对象,但过去两周我一直坚持这一点。
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 %>
应用程序的设置方式一次最多有两个或三个对象,我知道 json 是一种选择,但我希望有办法避免这样做。我读过的所有关于地图数组的资源都涉及手动传递数组参数,但我需要动态地进行,好像对象会因代表而异。
提前感谢您的帮助。
【问题讨论】:
标签: javascript ruby-on-rails arrays ruby google-maps-api-3