0

我有这个模型:

class Location < ActiveRecord::Base
  acts_as_gmappable
  def gmaps4rails_address
    "#{self.city}, #{self.country}"
  end
end

具有此架构:

t.float    "latitude"
t.float    "longitude"
t.boolean  "gmaps"
t.datetime "created_at"
t.datetime "updated_at"
t.string   "country"
t.string   "city"
t.string   "street"

我想允许用户指定起点和终点,并给他它们之间的距离。我想让他在地图上选择它们。你能指导我完成这个吗?

4

1 回答 1

1

没有直接的方法来实现你想要的gmaps4rails,至少不是它的两个部分:

  • 单击地图并创建标记属于您

  • 检索到这两个地方后,gem 可以处理显示目的地。

在您看来,您应该添加以下 ruby​​ 代码(以以下选项为例,这里的帮助程序的唯一目的是创建地图):

<%= gmaps( "map_options" => { "center_on_user" => true, "zoom" => 5 }) %>

然后你可以包括javascript:

//starting point
Gmaps4Rails.direction_conf.origin = from_var_string;
//where to go
Gmaps4Rails.direction_conf.destination = to_var_string;
//if you want to display a panel with the instructions
Gmaps4Rails.direction_conf.display_panel = true;
//pass here the id of the panel
Gmaps4Rails.direction_conf.panel_id = 'instructions';
//set the travel mode
Gmaps4Rails.direction_conf.travelMode = 'DRIVING';
//add one or more waypoints
Gmaps4Rails.direction_conf.waypoints = [{"stopover":true,"location": waypoint1_string},{"stopover":true,"location": waypoint2_string}];
//trigger the creation
Gmaps4Rails.create_direction();
于 2011-06-06T17:50:24.857 回答