環境
Ruby 2.1.0
Rails 4.1.0
gmaps4rails 2.1.2
GoogleMapsでマーカー位置座標の取得
Railsで飲食店検索サイトを作成しています。
GoogleMaps表示にはgmaps4rails
を用いています。
店舗情報は店舗が直接入力出来るようにしています。
gemgeocoder
を用いて入力してもらった住所から緯度・経度を取得し、GoogleMapsにマーカーを落とすようにしましたが実際の位置とはズレている事が多いです。
位置にズレがある場合は入力画面、もしくは確認画面で補正する方法はありませんでしょうか。
その際表示するGoogleMaps上でマーカーを直接動かして頂き、その位置の緯度・経度を取得してデータベースに保存できればベストだと考えています。
マーカー位置をドラッグでの移動は出来ましたが、その際の座標取得方法が分からずにいます。
通常でしたらGetPosition
メソッドで取得可能なようですが、、
Gmaps4Rails
を用いた場合の座標の取得教えて頂けませんでしょうか。
shops_contrller.rb
def edit
@shop = Shop.find(params[:id])
@hash = Gmaps4rails.build_markers(@shop) do |shop, marker|
marker.lat shop.latitude
marker.lng shop.longitude
marker.infowindow shop.name
marker.json({ title: shop.name })
end
respond_with(@shop)
end
shops/show.html.erb
<div style='width: 800px;'>
<div id="map" style='width: 800px; height: 400px;'></div>
</div>
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>,{draggable: true});
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>