在我的Rails應用程序中,我有一個幫助程序方法location
可獲取給定IP地址的座標,並使其在所有控制器和視圖中都可用。例如location.latitude
返回用戶的緯度。你明白了。從JavaScript和Rails傳遞參數
我也有一些Javascript,它基於給定的經緯度對從Google Maps API繪製地圖。問題是我不知道如何將參數傳遞給JavaScript!
中的JavaScript駐留在「application.js中」,看起來像這樣:
$(document).ready(function()
{
//Map options...I want the params to go into the var 'MapOptions' below
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(40.764698,-73.978972),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//Setup and Draw the Map...
//....................................
};
地圖本身被稱爲像這樣的HTML。沒有一個明顯的方法來通過params。
<div id="map_canvas">
<!-- The Map gets drawn here! -->
</div>
我知道這可能是一個明顯的問題,但我從來沒有過這樣的感覺從我的應用程序傳遞參數爲JavaScript。
嘿喬!迎接你的另一個晚上! JavaScript可能是我最薄弱的一點,因爲我只是在必要時纔開始做前端。再次感謝人! – thoughtpunch
對於一個寫得好的答案和你的姓氏+1。 ;) – ahsteele