Google 地图和 Rails 3

发布于 2024-10-29 04:41:35 字数 264 浏览 1 评论 0原文

我当时使用 geokit 和 y4mr-gm 在 Rails 2.3x 上构建了一个应用程序。

我现在正在构建一个应用程序和 Rails 3,并且很好奇是否有更好的解决方案。

基本上我有一个有地址的模型企业。当用户搜索或浏览企业时,我希望底部有一个地图,显示每个企业的标记。

我可以通过 geokit 和 y4mr-gm 来做到这一点(尽管我听说 Rails 3 可能存在一些问题),但我希望有一个更优雅的解决方案。最近有什么我可以使用的吗?

谢谢!

I built an app back in the day on rails 2.3x using geokit and y4mr-gm.

I am now building an app and Rails 3 and curious if there are any better solutions.

Basically I have a model Business that has an address. When a user searches for or browses the Businesses I want to have a map at the bottom that displays a marker for each business.

I could do this via geokit and y4mr-gm (though I hear there can be some trouble with Rails 3) but I am hoping for a more elegant solution. Is there anything recent I could use?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月棠 2024-11-05 04:41:35

我对 Geocoder 非常满意。它的维护非常活跃并且非常简单。在保存模型之前,我要求提供街道地址并让地理编码器填写纬度/经度字段。实施起来真的很轻松。

当涉及到在地图上显示内容时,我仍然自己编写 JS,但使用 jQuery 来完成。如果您正在寻找 JS 与 jQuery 的工作示例,您可以在此处查看我的示例。

I have been really happy with Geocoder. Its actively maintained and very simple. I ask for a street address and get geocoder to fill in the lat/lng fields before I save my model. It's really painless to implement.

When it comes to displaying the stuff on a map, I am still writing the JS myself, but using jQuery to do it. If you are looking for a working example of the JS with jQuery you can see mine here.

我一向站在原地 2024-11-05 04:41:35

我想说的是,拧紧插件,直接使用 Google Maps API v3 中的 javascript...它真的没有那么多代码,而且 G 有非常准确的地理编码器。只需在您的视图中创建一个 js 企​​业数组,然后使用一些 js 加载地图和标记即可。

未经测试的代码,但它应该非常接近:

<script type="text/javascript">
addresses = [];
<% @businessses.each do |biz| %>
  addresses.push('<%=biz.address%>');
<% end %>

function renderYoMap(addresses){
  var gmapOptions = {
    zoom: 12,
    center: new google.maps.LatLng(38,-97),  //near the middle of the US
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  // initialize the map with your options
  gmap = new google.maps.Map(document.getElementById("map_canvas"),gmapOptions);

  // probably want to recenter on the first marker, so lets use a flag
  recentered = false;

  //iterate through your addresses
  for (i in addresses) {
    address = addresses[i];
    geocoder = new google.maps.Geocoder();
    //geocode it to get marker position
    geocoder.geocode( { 'address': address }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      lat = results[0].geometry.location.lat();
      lng = results[0].geometry.location.lng();
      position = new google.maps.LatLng(lat,lng);
      markerOptions = {
        map: gmap,
        position: position
      };
      //create your marker on the map
      new google.maps.Marker(markerOptions);

      //recenter that thang if it hasnt been already
      if (!recentered){
        gmap.setCenter(position);
        recentered = true;
      }
    }
  }
}

renderYoMap(businesses);
</script>

当然,您可以变得更奇特,但这应该为您提供您正在寻找的基本功能。谷歌地理编码器限制是每天 1000 个,但那是客户端,所以这应该不是问题。

I would say screw the plugins and just use straight up javascript from Google Maps API v3... its really not that much code and G has very accurate geocoder. Just create a js array of businesses in your view and use the some js to load the map and markers.

UNTESTED code, but it should be pretty close:

<script type="text/javascript">
addresses = [];
<% @businessses.each do |biz| %>
  addresses.push('<%=biz.address%>');
<% end %>

function renderYoMap(addresses){
  var gmapOptions = {
    zoom: 12,
    center: new google.maps.LatLng(38,-97),  //near the middle of the US
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  // initialize the map with your options
  gmap = new google.maps.Map(document.getElementById("map_canvas"),gmapOptions);

  // probably want to recenter on the first marker, so lets use a flag
  recentered = false;

  //iterate through your addresses
  for (i in addresses) {
    address = addresses[i];
    geocoder = new google.maps.Geocoder();
    //geocode it to get marker position
    geocoder.geocode( { 'address': address }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      lat = results[0].geometry.location.lat();
      lng = results[0].geometry.location.lng();
      position = new google.maps.LatLng(lat,lng);
      markerOptions = {
        map: gmap,
        position: position
      };
      //create your marker on the map
      new google.maps.Marker(markerOptions);

      //recenter that thang if it hasnt been already
      if (!recentered){
        gmap.setCenter(position);
        recentered = true;
      }
    }
  }
}

renderYoMap(businesses);
</script>

Of course you can get alot fancier, but this should give you the basic functionality you are looking for. The google geocoder limit is 1000 a day, but that is client-side, so that should not be a problem.

夏末 2024-11-05 04:41:35

好吧,你有几个选择, ruby 工具箱 可以提供帮助,但看起来没有最新。

我建议 https://github.com/joshuamiller/cartographer

Cartographer 轻松生成 Google
Rails 应用程序的地图。它
支持 Google 地图 API v3 和来了
拥有所有的好东西(MarkerManager &&
MarkerClusterer)用于管理大型
以最少的努力完成标记的数量。

玩得开心

Well, you have several options, ruby tool box can help, but it doesn't looks up to date.

I suggest https://github.com/joshuamiller/cartographer

Cartographer generates painless Google
Maps for your Rails application. It
supports Google Maps API v3 & comes
with all the goodies (MarkerManager &&
MarkerClusterer) for managing large
number of markers with least effort.

have fun

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文