使用 Rails 3.2.13、ruby 1.9.3p374 和 Gmaps4rails 1.5.6。
我一直在为一些事情而苦恼,一开始我认为应该相当简单:我想显示一个标记,当点击它时,它会重定向到一个特定的路径,并在同一个窗口中加载该路径。
Gmaps4rails 在他们的 description 中没有简单的选择.所以我花了无数个小时在网上搜索,我在 StackOverflow 的此处找到了两个最佳匹配:this闻起来像是正确的道路,但是 this one似乎也很有用。
最后,我的 Controller 看起来像:
@gmapsjson = current_user.houses.all.to_gmaps4rails do | house, marker |
marker.title house.reference
marker.json({:link => polymorphic_url(house, :routing_type => :path)})
end
这是正确的(我认为)在处理后的 html 中生成以下内容:
<script src="//maps.google.com/maps/api/js?v=3.8&sensor=false&client=&key=&libraries=geometry&language=&hl=&region=" type="text/javascript"></script>
<script type="text/javascript">
Gmaps.map = new Gmaps4RailsGoogle();
Gmaps.load_map = function() {
Gmaps.map.map_options.auto_zoom = false;
Gmaps.map.map_options.zoom = 3;
Gmaps.map.initialize();
Gmaps.map.markers = [{"title":"First House","link":"/houses/1","lat":-3.4671425,"lng":12.5264373},{"title":"Second House","link":"/houses/2","lat":-4.5543296,"lng":-3.4151647}];
Gmaps.map.create_markers();
Gmaps.map.adjustMapToBounds();
Gmaps.map.callback();
};
Gmaps.oldOnload = window.onload;
window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); };
</script>
标记出现在正确的位置。但是现在,解释 first relevant reference 上的描述,我直接在 View 上输入,确保它会出现在生成的 html 中所有其他 javascripts Gmaps4Rails 转储的下方:
<script type="text/javascript">
function redirect_to(url) {
window.location = url
};
Gmaps.callback = function() {
for (var i = 0; i < Gmaps.map.markers.length; ++i) {
google.maps.event.addListener(Gmaps.map.markers[i].google_object, 'click', redirect_to(Gmaps.map.markers[i].link));
}
};
</script>
但是现在,每当我加载页面时,我的 Firefox javascript 控制台都会说:
TypeError: Gmaps[load_function_name] is not a function
for (key in Gmaps) {
value = Gmaps[key];
searchLoadIncluded = key.search(/load/);
if (searchLoadIncluded === -1) {
load_function_name = "load_" + key;
_results.push(Gmaps[load_function_name]()); <=== Points to this line
} else {
_results.push(void 0);
}
}
那么,有什么问题吗?
提前致谢
马可
最佳答案
与其在 JavaScript 中执行此操作,您还可以生成所有 html,包括链接是您的 Controller 。 注意:为了正确生成链接,我单独设置了它并将其分配给一个变量
这是我的 locations_controller.rb 包含的内容:
@json = Location.all.to_gmaps4rails do |location, marker|
location_link = view_context.link_to location.name, location_path(location)
marker.title location.name
marker.infowindow "<h4><u>#{location_link}</u></h4>
<i>#{location.address}</i>"
end
当你正确设置它时,你会得到看起来像这样的散列
{"title":"Home",
"description":"<h4><u><a href=\"/locations/1\">Place</a></u></h4>
<i>123 E Main St. Anytown, USA</i>",
"lat":29.585019,
"lng":-81.319479}
维基页面 here帮助过我。希望对你有帮助
关于ruby-on-rails - gmaps4rails:如何在标记上实现可点击链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17961145/