简体   繁体   English

如何做一个Ajax GET请求从rails获取数据并将其传递给javascript(谷歌地图)?

[英]How to do an Ajax GET request to get data from rails and pass it to javascript(google maps)?

I have a model Locations with two columns latitude and longitude. 我有一个模型位置,有两列纬度和经度。 I want to find a way where I can get the list of locations and pass them to google maps using Ajax and javascript. 我想找到一种方法,我可以获取位置列表,并使用Ajax和javascript将它们传递给谷歌地图。 So far my code is as follows: 到目前为止,我的代码如下:

map.js: map.js:

function initialize() 
{   
    var map;
    var latlng = new google.maps.LatLng(37.09, -95.71);
    var options = {
    zoom: 5, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI: true,
    disableDoubleClickZoom: true,
    noClear: true,
    navigationControl: true,
    navigationControlOptions: {
        position: google.maps.ControlPosition.TOP_RIGHT
    }
    };
   map = new google.maps.Map(document.getElementById('map'), options);

   var marker = new google.maps.Marker({
    position: latlng, 
    map: map, 
    title: 'Click me', 
    });
}

locations_controller.rb locations_controller.rb

def show
    @location = Location.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @location }
    end
  end

Page displaying the map: 显示地图的页面:

<div id="map" onload="initialize();">
</div>

SO now I want to find a way to make an AJAX request from map.js so I can get the locations from the Location model and pass it to the marker object so that when a map is loading all the locations pre-existing in the database are passed to marker and those markers appear. 所以现在我想找到一种从map.js发出AJAX请求的方法,这样我就可以从Location模型中获取位置并将其传递给marker对象,这样当map加载数据库中预先存在的所有位置时被传递给标记并出现那些标记。

Thank you. 谢谢。

Oh I was able to find my way to get the list of locations. 哦,我找到了获取位置列表的方法。 The controller and view are left untouched. 控制器和视图保持不变。

I added the following ajax in map.js which did the work for me. 我在map.js中添加了以下ajax,它为我完成了工作。

$.ajax({
        type: "GET",
        dataType: "json",
        url: "/locations",
        success: function(data){}
    }); 

Now data can be passed to the marker object in google maps. 现在,数据可以传递到谷歌地图中的标记对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从jQuery ajax GET请求传递到Google Maps脚本 - pass data from jQuery ajax GET request to google maps script 我如何使用javascript中的ajax调用将参数或ajax数据传递给python脚本并在成功时获得响应? - how do i pass parameter or ajax data to a python script using ajax call in javascript and get response if it is successful? 如何从JavaScript发送GET请求到rails - How to send GET request to rails from javascript 如何使用 Javascript 和 Ajax 从 API 获取数据? - How do I get the data from API using Javascript and Ajax? 如何从ajax请求获取数据响应? - How to get the data response from ajax request? 如何从Ajax请求获取数据? - how to get data from Ajax request? 在Rails中使用AJAX进行DELETE请求时,如何从表单中获得不同的ID值? - How do I get different id values from a form when making a DELETE request using AJAX in Rails? 您如何获取JavaScript以将通过AJAX请求提取的tinyint(1)数据视为布尔值? - How do you get Javascript to see tinyint(1) data pulled via AJAX request as boolean? 如何使用Rails和JavaScript计时器使AJAX工作? - How do I get AJAX working using rails and a javascript timer? 通过ajax()请求获取Google Maps API的地址坐标 - Get address coordinates of Google Maps API by ajax() request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM