LoginSignup
1

More than 5 years have passed since last update.

【Gmaps for Rails】マーカー位置調整時の座標取得

Last updated at Posted at 2016-07-05

環境

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>

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1