2017-03-04 127 views
0

我找不出如何更改Google地图的标记图标。 这是我的代码。大部分标记已经显示。我只想更改我目前的位置。如何自定义Google地图标记图标?

控制器

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 

这里是视图脚本。 面积/ _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" 

我会很感激的任何建议。

回答

-1

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

否则,markers=icon:URLofIcon|markerLocation

URLofIcon是图标的网址,你想用,这应该是URL编码(使用缩短服务,这将可能为您编码URL,并使代码看起来好多了)

+0

您的答案应该更好地指定,即放置解决方案的位置,一些代码示例。 – Massimo

0

最后,我解决了。喜欢这个。

@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" 
     } 

赞赏所有贡献!