'您采用的 javascript_tag 方法不支持“观察”变量的变化......'
是的,没错,即使我在应用程序周围单击并重新加载页面,javascript_tag 中的值也保持不变。就像在我的应用程序启动时加载我的 js 文件时,@user 的纬度和经度值保持不变,即使它们应该改变,并且地图总是使用相同的坐标绘制。
'Gon' 宝石对我也不起作用。文档很少,尤其是对于像我这样的新手。
什么成功:
在我希望生成地图的页面上,show.html.erb:
<!-- This is here, to generate the user's latitude and longitude dynamically -->
<!-- The data-lat and lng attributes are used in 'initialise_google_maps' function, in scripts.js' -->
<div id="user-position" class="hidden" data-lat="<%= @user.lat %>" data-lng="<%= @user.lng %>"></div>
然后在我的 scripts.js 中:
function initialize_google_maps() {
var user_longitude = $("#user-position").attr("data-lng");
var user_latitude = $("#user-position").attr("data-lat");
var currentlatlng = new google.maps.LatLng(user_latitude, user_longitude);
var zoom = 10;
var myOptions = {
zoom: zoom,
center: currentlatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP, // ROADMAP, SATELLITE, HYBRID
streetViewControl: false
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({map: map, position: currentlatlng, icon:{oppacity:0}});
var circle = new google.maps.Circle({
map: map,
fillOpacity: 0,
strokeWeight: 2,
strokeOpacity: 0.7,
radius: 10000,
});
circle.bindTo('center', marker, 'position');
}
现在可以享受美食了。