http://www.killswitchcollective.com/articles/65_google_maps_on_rails
The Google Maps API is without a doubt one of the most popular APIs available on the web today. It offers webmasters the ability to add mapping functionality, similar to maps.google.com, to their websites. All of Google Maps functionalities, including markers, directions, zoom options and satellite view are only a few lines of code away. But before we get started with the code portion, you will need a Google Maps API key. Getting a Google Maps API key takes a minute and costs nothing. Just sign in to your Google Account (I am sure you have one) and head to http://code.google.com/apis/maps. On the right hand side you will see ‚How Do I Start, where you will just simply click on Step 1 "Sign up for a Google Maps API key". Then enter your site's live domain, accept the terms and click the "Generate API Key" button. On the next page your unique API key will appear. Make sure that you copy and paste it to a safe location. Below the API key will be JavaScript examples to quickly get you started. In this tutorial, we will generate similar code with the use of some rails plug-ins. The most popular plug-in is the Yellow Maps for Ruby, also known as YM4R. Since there are other mapping APIs, this plug-in comes in a few flavors. Since we're doing Google Maps, we will use YM4R/GM.
1. Lets start by generating a new Rails application:
rails map_example -d mysql
2. Create a 'map_example_development' database and remove index.html from the public folder
cd map_example
rake db:create
rm public/index.html
3. The lets add a controller and the index action
script/generate controller location index
4. Lets install the YM4R/GM plugin
script/plugin install git://github.com/queso/ym4r-gm.git
5. Add frontend.html.erb in app/views/layouts with the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Google Maps Rails Example</title>
<%= GMap.header %>
<%= @map.to_html unless @map.blank? %>
</head>
<body>
<%= yield %>
</body>
</html>
====
Note that we have included two YM4R/GM lines in the head section. The GMap.header will include needed JavaScript files while the @map.to_html line generates JavaScript based on the parameters passed into the map object. We will be creating the @map object in our location controller.
6. Don't forget to specify the layout in /app/controllers/location_controller:
class LocationController < ApplicationController
layout 'frontend'
def index
coordinates = [23.11374751041939, 113.39768183810425]
@map = GMap.new("map")
@map.control_init(:large_map => true, :map_type => true)
@map.center_zoom_init(coordinates,14)
@map.overlay_init(GMarker.new(coordinates,:title => "Navy Pier", :info_window => "Navy Pier"))
end
end
---
First we set our map coordinates into an array. The first number is the latitude and second is the longitude. These coordinates point to Navy Pier in Chicago, Illinois. If you would like to try a different area for this example, this easy tool (http://stevemorse.org/jcal/latlon.php) provides you the latitude and longitude for any address you want to use.. Next, we set the @map object to pass into our view. The @map object contains a new instance of the GMap class; the "map" string is the id of the div that will contain the map. The next line activates controls for our map. The large map activates the zoom option while the map type activates various views such as a regular map, satellite or terrain. Then we set the default map center and zoom. We first pass in the coordinates array so our location will be centered, then we pass in a integer to determine zoom level. This integer can be between 0 and 22, the higher the number the more the zoomed in the map will be. Finally, we add a marker overlay. The first argument is the coordinates and it is the only required argument need to add a marker. The title and info_window are both optional, title is the text that will display when your mouse is idle over the marker, while info_window is the text that will appear in the pop up box that appears after clicking the marker.
--
8. Next open our view file located at app/views/location/index.html.erb and add the following:
<h1>Google Maps Rails Example</h1>
<%= @map.div(:width => 800, :height => 500) %>
9. Map the homepage to the index action of location by adding the following to config/routes.rb.
map.root :controller => "location", :action => "index"
10. Fire up your rails application and a map should appear.
...
- 大小: 359.3 KB
分享到:
相关推荐
综上所述,《Beginning Google Maps Applications with Rails and Ajax》这本书为初学者提供了一条清晰的学习路径,从基础概念到实践案例,全面覆盖了如何使用Google Maps API、Rails以及Ajax技术创建高质量的地图...
本书旨在为读者提供一个全面了解如何使用Ruby on Rails(简称Rails)框架结合Ajax技术开发Google Maps应用的基础教程。无论是初学者还是有一定经验的开发者,都能从中获得实用的知识和技能。 - **目标读者**: - ...
Google Maps for Rails 是一个强大的 Ruby on Rails 框架插件,它允许开发者轻松地将 Google Maps 集成到他们的 Rails 应用程序中。这个库提供了与 Google Maps JavaScript API 的无缝对接,使得在 Ruby 中操作地图...
《初识Google Maps应用:基于Rails和Ajax》是一本由Apress出版的技术书籍,专注于讲解如何使用Ruby on Rails框架和Ajax技术构建与Google Maps集成的应用程序。这本书详细介绍了如何利用Google Maps API,结合Web开发...
直到2005年,Google推出了使用XMLHttpRequest技术的一系列高度视觉化的Web应用(如Google Maps),引起了广泛关注。同年2月,Jesse James Garrett在他的文章中首次提出了“Ajax”这一名称,标志着这一技术的正式命名...
- Ruby on Rails 应用程序谷歌地图 API #HTTParty 连接到 openweathermap.org 这是一个 ruby on Rails 应用程序,它使用谷歌地图 API 来确定百老汇大会地址。 我使用 HTTParty gem 连接到 openweathermap.org ...
诺姆斯特Nomster是一个yelp克隆,与PostgreSQL数据库,Ruby on Rails,HTML5,CSS,BootStrap和Google Maps API集成在一起。 该应用程序包括一些功能,例如更新地点(特定餐厅的焦点),加载地图,用户评论每个地点...
为了在网页上展示地图,通常会使用JavaScript库如Leaflet或Google Maps API。在Rails视图中嵌入JavaScript代码,将地理坐标与地图标记关联,用户就能看到位置信息了。 **7. 地理编码(Geocoding)** 地理编码是将...
在Ruby on Rails框架中,Typeahead Addresspicker是一款强大的组件,它结合了Twitter的Typeahead.js库和Google Maps的Geocoding API,为用户提供了动态、自动完成的地址输入功能。本文将深入探讨Typeahead ...
安全性方面,TakeoutTaxi_FrontEnd采用了Devise,这是一个灵活的身份验证解决方案,用于Ruby on Rails应用。它提供了用户注册、登录、密码重置等功能,确保用户账户的安全。 在前端开发上,TakeoutTaxi_FrontEnd...
JTRailsAddress使用Ruby On Rails和Javascript中的Google Maps API简化了邮政地址管理和地理编码。 安装 JTRailsAddress作为gem分发,这就是应在您的应用程序中使用它的方式。 在您的Gemfile中包含gem: gem 'jt-...
规划文件:内容使用的技术后端后端框架:Ruby on Rails(v5.1.5) 数据库:PostgreSQL(v2.1.3) 用户身份验证:使用BCrypt(v3.1.7)创建外部API:Google Maps API和Open Weather Map API前端前端框架:Javascript...
Google通过其一系列应用程序,如Google Maps,展示了Ajax在提升Web应用UI体验方面的巨大潜力。在此之前,Web应用通常依赖于完整的页面刷新来更新内容,这导致效率低下,尤其是在数据量大或频繁交互的场景中。 传统...
该堆栈包括一个结合了PostgreSQL数据库的Ruby-on-Rails后端和一个React-Redux前端。 Google Maps Directions API可用于创建和显示用户创建的跑步/骑行路线。 Google Static Maps API与Google Elevation Services...
利用Google Maps API和ActiveAdmin的EmberCLI和Ruby on Rails演示应用程序。 一些信息是从《欧洲啤酒指南》网站( )借来的。 先决条件 您需要在计算机上正确安装以下物品。 (带有NPM) 安装 git clone 此存储库 ...
该应用程序的创建是在前端使用React.js和Redux,在后端使用Ruby on Rails / PostgreSQL。 该项目的设计和建造在一周内完成,随后又进行了一些其他改进。 强调 集成Google Maps API以创建运行路线并启用基于地理...
Google Maps和Google Places允许用户选择一个固定位置以用于其帐户。 可以根据用户喜好禁用或更新此功能。 使用首选编程语言标记帐户的可标记的。 这与结合在一起以搜索要标记的技能。 用户可以通过名称,位置或...