【问题标题】:How do I customize Google Map Marker Icon?如何自定义谷歌地图标记图标?
【发布时间】:2017-03-04 14:52:29
【问题描述】:

我不知道如何更改谷歌地图的标记图标。 这是我的代码。大部分标记已经显示。我只想更改我当前的位置。

控制器

  def restaurant_nearby
        @restaurants = Restaurant.near(params[:lat], params[:lng], params[:num], params[:lang])
        @hash = Gmaps4rails.build_markers(@restaurants.to_hash) do |r, marker|
                        marker.lat r["lat"]
                        marker.lng r["lng"]
                        marker.infowindow render_to_string(partial: "area/infowindow", locals: { place: r })
                        marker.json({title: r["restaurantname"]})
        end

        @hash_current_pos = {
                        lat: params[:lat],
                        lng: params[:lng],
                        icon: '/here.png',
                        infowindow: render_to_string(partial: "area/infowindow_current_pos"),
                        title: "You are here"
        }

        @hash = @hash.push(@hash_current_pos)

        content = render_to_string(:partial => 'area/restaurant_map', locals: {hash: @hash})
        render json: {html: content}, status: :ok
  end

这是视图脚本。 area/_restaurant_map.html.slim

javascript:
  jQuery(function(){

       mapStyle = [[{"featureType":"administrative.country","elementType":"geometry.fill","stylers":[{"saturation":"-35"}]}]];

      handler = Gmaps.build('Google');
      handler.buildMap({ provider: {styles: mapStyle}, internal: {id: 'map'}}, function(){
      markers = handler.addMarkers( #{raw hash.to_json});
      handler.bounds.extendWith(markers);
      handler.fitMapToBounds();
      handler.getMap().setZoom(15);
      });
 });

/! Le HTML5 shim, for IE6-8 support of HTML elements
/[if lt IE 9]
  = javascript_include_tag "//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js"

我会很感激任何建议。

【问题讨论】:

    标签: javascript ruby-on-rails google-maps


    【解决方案1】:

    最后,我锻炼了。像这样。

    @hash_current_pos = {
                            lat: params[:lat],
                            lng: params[:lng],
                            picture: {url: '/here.png', width: 140, height: 180},
                            infowindow: render_to_string(partial: "area/infowindow_current_pos"),
                            title: "You are here"
            }
    

    感谢所有贡献!

    【讨论】:

      【解决方案2】:

      如果你只是想自定义谷歌的图标,你可以设置一个sizecolor

      否则,markers=icon:URLofIcon|markerLocation

      URLofIcon 是您要使用的图标的 URL,它应该是 URL 编码的(使用可能会为您进行 URL 编码并使代码看起来更好的缩短服务)

      【讨论】:

      • 你的答案应该更好地指定,即在哪里放置解决方案,一些代码示例..
      猜你喜欢
      • 1970-01-01
      • 2015-06-06
      • 2018-01-13
      • 2013-08-21
      • 2014-05-27
      • 2014-07-18
      • 2019-10-29
      相关资源
      最近更新 更多