简体   繁体   English

如何使用Ruby on Rails将MySQL纬度/经度数据转换为JSON

[英]How can I convert MySQL latitude / longitude data to JSON using Ruby on Rails

Thanks in advance for any help you can provide on this question: How can I use Ruby on Rails to create a JSON object from the "latitude" and "longitude" data in my MySQL table, "Locations"? 提前感谢您提供有关此问题的任何帮助:如何使用Ruby on Rails从我的MySQL表“Locations”中的“纬度”和“经度”数据创建JSON对象?

My ultimate goal is to create a Google Map with multiple markers pulled from a MySQL database. 我的最终目标是创建一个谷歌地图,其中包含从MySQL数据库中提取的多个标记。 I know that I can do this through PHP and the Google tutorial ( https://developers.google.com/maps/articles/phpsqlsearch_v3 ), but I'm not strong in PHP and am looking for a shorter way to do it through Rails. 我知道我可以通过PHP和Google教程( https://developers.google.com/maps/articles/phpsqlsearch_v3 )来做到这一点,但我在PHP方面并不强大,我正在寻找一种更短的方法来实现它轨。

I have tried this other tutorial ( http://mokisystemsblog.blogspot.com/2013/04/add-markers-to-google-map-with-ruby-on.html ), and here is my code for my controller: 我已经尝试了这个其他教程( http://mokisystemsblog.blogspot.com/2013/04/add-markers-to-google-map-with-ruby-on.html ),这是我的控制器的代码:

    class MapallController < ApplicationController
      # GET /mapall
      # GET /mapall.xml
      # GET /mapall.js

      def index
        respond_to do |format|
          format.html do
            @locations = Location.find(:all)
          end
          format.xml  { render :xml => @locations }
          format.js do
            ne = params[:ne].split(',').collect{|e|e.to_f}  
            sw = params[:sw].split(',').collect{|e|e.to_f}
            @locations = Location.find(:all, :limit => 100, :bounds => [sw, ne])
            render :json => @locations.to_json
          end
        end
      end
    end

However, when I visit www.example.com/mapall.js, I get an error code. 但是,当我访问www.example.com/mapall.js时,我收到错误代码。 I expect that this link would give me the complete set of results from my database. 我希望这个链接可以从我的数据库中获得完整的结果集。

Again, I appreciate your advice and patience as I learn this process! 在我学习这个过程时,我再次感谢您的建议和耐心!

EDIT 1 - ERROR CODE 编辑1 - 错误代码

Below is the log for what happens when I visit example.com/mapall, example.com/mapall.js, and example.com/mapall.xml. 下面是访问example.com/mapall,example.com/mapall.js和example.com/mapall.xml时会发生什么的日志。 When I visit example.com/mapall, I expect a Google Map that renders all of my locations from the MySQL database. 当我访问example.com/mapall时,我希望Google Map能够从MySQL数据库中呈现我的所有位置。 Barring that, I expected to see the lat / long data when I visited mapall.js. 除此之外,当我访问mapall.js时,我希望看到lat / long数据。 From the log below, I'm guessing the reason that I'm getting a 404 is that there is no route to mapall.js in my routes file. 从下面的日志中,我猜测我得到404的原因是我的路线文件中没有mapall.js的路由。 Is the solution to create a route to the routes file, and if so, how should that read? 解决方案是创建路由文件的路由,如果是,那该怎么读?

Thanks again for your help! 再次感谢你的帮助!

    Processing MapallController#index (for IP at DATE TIME) [GET]
    Rendering template within layouts/mapall
    Rendering mapall/index
    Completed in 779ms (View: 7, DB: 100) | 200 OK [http://example.com/mapall]

    Processing ApplicationController#index (for IP at DATE TIME) [GET]

    ActionController::RoutingError (No route matches "/mapall.js" with {:method=>:get}):
    /phusion_passenger ERROR INFO
    Rendering /home/example/web/current/public/404.html (404 Not Found)

    Processing ApplicationController#index (for IP at DATE TIME) [GET]

    ActionController::RoutingError (No route matches "/mapall.xml" with {:method=>:get}):
    /phusion_passenger ERROR INFO
    Rendering /home/example/web/current/public/404.html (404 Not Found)

EDIT 2 - New Controller 编辑2 - 新控制器

Thanks to @kyllo's feedback, I've been able to update the controller so that I can get ALL of my location data to appear at http://example.com/mapall.js . 感谢@ kyllo的反馈,我已经能够更新控制器,以便我可以将所有位置数据显示在http://example.com/mapall.js上 Only one step left: getting just the nickname, latitude, and longitude fields to appear. 只剩下一步:只显示要显示的昵称,纬度和经度字段。 The below shows ALL of the data. 下面显示了所有数据。 How should I change this to show only the nickname, latitude, and longitudinal fields? 我应该如何更改它以仅显示昵称,纬度和纵向字段?

    class MapallController < ApplicationController
    # GET /mapall
    # GET /mapall.xml
    # GET /mapall.js

    def index
    respond_to do |format|
@masterlocation = Masterlocation.find(:all)
    format.html do
    end
    format.xml  { render :xml => @masterlocation }
    format.js do
    @masterlocation = Masterlocation.find(:all)
    render :json => @masterlocation.to_json
    end
    end
    end
    end

I see a few problems here. 我在这看到一些问题。

Yes, you are getting a "no route matches" error so you need a route in routes.rb similar to this, so that you can access :format as a parameter to respond to: 是的,您收到“无路由匹配”错误,因此您需要在routes.rb中使用类似于此的路由,以便您可以访问:format作为响应的参数:

match 'mapall.:format'

Then in your controller, you have the following: 然后在您的控制器中,您有以下内容:

def index
    respond_to do |format|
      format.html do
        @locations = Location.find(:all)
      end
      format.xml  { render :xml => @locations }
      format.js do
        ne = params[:ne].split(',').collect{|e|e.to_f}  
        sw = params[:sw].split(',').collect{|e|e.to_f}
        @locations = Location.find(:all, :limit => 100, :bounds => [sw, ne])
        render :json => @locations.to_json
      end
    end
  end

Your @locations = Location.find(:all) needs to be outside of the format.html do block, otherwise @locations is undefined inside of your format.xml block and you will get an error. 您的@locations = Location.find(:all)需要在format.html format.html do块之外,否则在您的format.xml块中未定义@locations,您将收到错误。

format.js do will allow your user to access the JSON API by visiting http:/www.example.com/mapall.js . format.js do会允许您的用户访问http:/www.example.com/mapall.js来访问JSON API。 If you use format.json do then they can access it at http:/www.example.com/mapall.json . 如果您使用format.json do那么他们可以访问http:/www.example.com/mapall.json The choice is yours. 这是你的选择。

Hope that helps to put you on the right track! 希望有助于您走上正轨!

暂无
暂无

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

相关问题 如何使用Ruby on Rails将经度和纬度标记保存到mysql数据库中 - How to save a marker with latitude and longitude into mysql database using Ruby on Rails 使用 MySQL 将小数转换为点(经度、纬度) - Convert Decimals to Point (Longitude, Latitude) using MySQL 如何根据 PHP PDO 中的经度、纬度和半径从 MYSQL 中提取数据? - How can I pull data from MYSQL based on Longitude, Latitude and radius in PHP PDO? MySQL:如何计算特定半径(经度/纬度)的中位数 - MySQL: How can I calculate the median in a specific radius (longitude / latitude) 我们如何使用Android将纬度和经度发送到MySQL? - How we can send latitude and longitude to MySQL using Android? 如何在MySQL中使用经度和纬度搜索最近的用户,同时按性别和年龄进行过滤? - How can I search for nearest users using longitude and latitude but also filter by sex and age in an efficient way in MySQL? 如何将纬度/经度数据正确导入MySQL? - How to import latitude/longitude data correctly into MySQL? 我可以将WGS84纬度/经度直接存储为MySQL中的空间数据吗? - Can I store WGS84 latitude / longitude directly as spatial data in MySQL? 如何加快此MySQL查询的速度,以找到最接近给定纬度/经度的位置? - How can I speed up this MySQL query that finds the closest locations to a given latitude/longitude? Rails 和 MySQL 上的经纬度的最佳列类型 - Optimal column type for latitude and longitude on Rails and MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM