简体   繁体   English

根据坐标谷歌地图添加标记

[英]add marker based on coordinates google maps

I really need some help on this, it's quite hard for me to figure out how would the best way to do this. 我确实需要一些帮助,这对我来说很难找出最好的方法。

Controller: 控制器:

def merchant_ids
    merchants = Merchant.where(id: params[:id]).pluck(:merchant_name, :id, :merchant_city, :physical_address, :merchant_phone, :latitude, :longitude)
    render json: merchants
  end

this is for showing the user the information based on the selected merchant . 这是为了向用户显示基于所选merchant的信息。

example: 例:

[["Burger King",1,"Aguadilla","Burger King Plaza Real","78778778787",18.3703084883462,-66.0766804218292]]

Javascript: Javascript:

// this one display the information based on the selected value
$('#complaint_merchant_id').chosen().change(function(){
    var clean_html = $('#merchant_name, #physical_address, #merchant_phone');
    var clean_value = $('#merchants_latitude, #merchants_longitude');
    clean_html.html('');
    clean_value.val('');
    $.ajax('/dashboard/merchants/merchant_ids/' + this.value).done(function(data){
        for (var city in data);
        $('#merchant_name').append(data[city][0]);
        $('#physical_address').append(data[city][3]);
        $('#merchant_phone').append(data[city][4]);
        $('#merchants_latitude').val(data[city][5]);
        $('#merchants_longitude').val(data[city][6]);
    }) ;
});

I have the gmaps.js storing the latitude and longitude in a float in the database in the merchants record 我有gmaps.js在商家记录的数据库中以float存储纬度和经度

schema: 模式:

  create_table "merchants", force: :cascade do |t|
    t.string   "merchant_name"
    t.string   "merchant_city"
    t.string   "postal_address"
    t.string   "physical_address"
    t.string   "email"
    t.string   "merchant_phone"
    t.string   "merchant_secondary_phone"
    t.datetime "created_at",               null: false
    t.datetime "updated_at",               null: false
    t.integer  "user_id"
    t.float    "latitude"
    t.float    "longitude"
  end

in the view I have two fields to put the latitude and longitude that gets pulled from the database depending on which merchant is selected: 在视图中,我有两个字段来放置从数据库中提取的纬度和经度,具体取决于选择的商人:

<div class="col-md-4">
      <%= label '', 'Nombre de Proveedor/Agencia:' %><br>
      <span class="color-main" id="merchant_name"></span>
    </div>
    <div class="col-md-4">
      <%= label '', 'Dirección local:' %><br>
      <span class="color-main" id="physical_address"></span>
     </div>
     <div class="col-md-4">
       <%= label '', 'Teléfono:' %><br>
       <span class="color-main" id="merchant_phone"></span>
      </div>

      <div class="col-md-12">
        <input type="text" id="merchants_latitude"/>
        <input type="text" id="merchants_longitude"/>
      </div>

What I need to do is when they select a merchant, it will put a marker based on the latitude and longitude of the current selected merchant and center the map to the marker. 我需要做的是当他们选择商家时,它将基于当前所选商家的经度和纬度放置一个标记,并将地图居中放置到标记上。

I only know how to do the eventListener for clicking in the map but to refresh the marker when someone selects an option in a tag outside of the map I currently don't know how to. 我只知道如何执行eventListener来单击地图,但是当有人在地图外的标签中选择一个选项时(我目前不知道)来刷新标记。

I have the map loading in a 我将地图加载到

<div id="map-complaints" style="width:100%;height:400px;"></div>

Thank you for your time, really appreciate. 谢谢您的时间,非常感谢。

edit: 编辑:

this is how I select the merchant: 这是我选择商家的方式:

<div class="col-md-12">
  <%= f.label :merchant_id, 'Seleccione proveedor o agencia de servicios' %>
  <% merchants_array = @merchants.map { |merchant| [merchant.merchant_name, merchant.id] } %>
  <%= f.select :merchant_id, merchants_array, {include_blank: true}, {class: 'chosen-select form-control', tabindex: '-1'} %>
 </div>

I think that the best solution was to use such an onclick event on the by which selection is made of the merchant. 我认为最好的解决方案是在选择商家时使用onclick事件。 This onClick must contain a call to a javascript function that creates the marker. 此onClick必须包含对创建标记的javascript函数的调用。 Since the div have the coordinates where to place the marker, the function call javascript may contain the information necessary for proper placement on the map. 由于div具有放置标记的坐标,因此函数调用javascript可能包含正确放置在地图上所需的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM