2011-04-02 48 views
3

我使用geokit和y4mr-gm在rails 2.3x上在當天構建了一個應用程序。谷歌地圖和軌道3

我現在正在構建一個應用程序和Rails 3,並好奇如果有更好的解決方案。

基本上我有一個模型有一個地址的業務。當用戶搜索或瀏覽我想要在底部顯示每個企業的標記的地圖的企業時。

我可以通過geokit和y4mr-gm來做到這一點(雖然我聽說Rails 3可能會有些麻煩),但我希望能有一個更優雅的解決方案。最近有什麼可以使用的嗎?

謝謝!

回答

4

我一直Geocoder真的很高興。它的積極維護和非常簡單。在我保存模型之前,我要求提供一個街道地址並讓geocoder填寫lat/lng字段。實施起來真的很痛苦。

當談到在地圖上顯示東西時,我仍然自己寫JS,但是使用jQuery來完成它。如果你正在尋找一個jQuery的工作示例,你可以看到我的here

+0

你還有jQuery與你提到的jQuery,我很有趣......謝謝。 – Stanmx 2013-07-02 09:01:30

1

好了,你有幾種選擇,ruby tool box可以提供幫助,但它並沒有看起來是最新的。

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

製圖爲您的Rails應用程序生成無痛谷歌地圖 。它 支持谷歌地圖API V3 &自帶 與所有的好東西(MarkerManager & & MarkerClusterer),用於管理大量 多個標記,用最少的努力。

樂趣

2

我會說螺絲插件,只是直接從谷歌地圖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,但這是客戶端,所以這不應該是一個問題。