我想在地图上显示许多传感器位置,我需要在标记上显示所有当前传感器值(不在信息窗口,用户可以看到没有点击标记的数据)
创建许多不同的图标和更改图标对我来说是不现实的。 有没有办法在标记上显示动态数据?比如在标记上显示数字标签。我找到了一个,但无法将其转换为ruby on rails代码。 这是我为java找到的链接。 这里有一个link用于在ROR上创建gmaps
这就是我现在所拥有的。使用gem Gmaps4rails。
在app / asssts / gmaptest.js.coffee中
class CustomMarkerBuilder extends Gmaps.Google.Builders.Marker
create_marker: ->
options = _.extend @marker_options(), @rich_marker_options()
@serviceObject = new RichMarker options
rich_marker_options: ->
marker = document.createElement("div")
marker.setAttribute('class', 'custom_marker_content')
marker.innerHTML = this.args.custom_marker
{ content: marker }
create_infowindow: ->
return null unless _.isString @args.custom_infowindow
boxText = document.createElement("div")
boxText.setAttribute("class", 'custom_infowindow_content')
boxText.innerHTML = @args.custom_infowindow
@infowindow = new InfoBox(@infobox(boxText))
infobox: (boxText)->
content: boxText
pixelOffset: new google.maps.Size(-140, 0)
boxStyle:
width: "280px"
@buildMap=(markers)->
handler = Gmaps.build("Google", builders: { Marker: CustomMarkerBuilder } )
handler.buildMap { internal: id: "custom_builder" }, ->
marker = handler.addMarker
lat: 40.689167
lng: -74.044444
custom_marker: "<img src='images/star.jpg' width='30' height='30'> Some text here"
custom_infowindow: "<img src='images/statue.jpg' width='90' height='140'> some test here"
handler.map.centerOn marker
handler.getMap().setZoom(15)
@hash = Gmaps4rails.build_markers(@noiseDevices) do |noiseDevice, marker|
marker.lat noiseDevice.latitude
marker.lng noiseDevice.longitude
end
在app / controller / gmaptest_controller.rb
中@noiseDevices = NoiseDevice.all
@hash = Gmaps4rails.build_markers(@noiseDevices) do |noiseDevice, marker|
marker.lat noiseDevice.latitude
marker.lng noiseDevice.longitude
marker.json({custom_marker: "hi all"})
end
在app / view / gmaptest / index.html.erb
中<script src="//maps.google.com/maps/api/js?v=3.13&sensor=false&libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/trunk/richmarker/src/richmarker-compiled.js' type='text/javascript'></script>
<h1>Listing gmaptests</h1>
<table>
<tr>
<th>Latitude</th>
<th>Longitude</th>
<th>Address</th>
<th>Description</th>
<th>Title</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @noiseDevices.each do |noiseDevice| %>
<tr>
<td><%= noiseDevice.latitude %></td>
<td><%= noiseDevice.longitude %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New gmaptest', new_g1test_path %>
<div style='width: 800px;'>
<div id="custom_builder" style='width: 800px; height: 400px;'></div>
</div>
<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 %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>
现在的问题是,当我尝试按照视频/教程编辑视图中的文件时,整个谷歌地图窗口消失了。原因之一是添加buildMap(&lt;%= raw@hash.to_json%>),和其他是改变脚本的视图
<script type="text/javascript">
var handler = Gmaps.build("Google", builders: { Marker: CustomMarkerBuilder } )
handler.buildMap({ internal: id: "map" }, function(){
var markers = handler.addMarkers(<%=raw @hash.to_json %>);
});
</script>
答案 0 :(得分:1)
第1步:
创建一个咖啡文件,将其命名为gmaps4rails_builder.coffee
:
class @CustomMarkerBuilder extends Gmaps.Google.Builders.Marker
create_marker: ->
options = _.extend @marker_options(), @rich_marker_options()
@serviceObject = new RichMarker options
rich_marker_options: ->
marker = document.createElement("div")
marker.setAttribute('class', 'custom_marker_content')
marker.innerHTML = this.args.custom_marker
{ content: marker }
步骤2:
在您看来:
<script src='//google-maps-utility-library-v3.googlecode.com/svn/trunk/richmarker/src/richmarker-compiled.js' type='text/javascript'></script>
<script type="text/javascript">
var handler = Gmaps.build("Google", builders: { Marker: CustomMarkerBuilder } )
handler.buildMap({ internal: id: "map" }, function(){
var markers = handler.addMarkers(<%=raw @hash.to_json %>);
});
</script>
步骤3:
在您的控制器中:
@hash = Gmaps4rails.build_markers(@noiseDevices) do |noiseDevice, marker|
marker.lat noiseDevice.latitude
marker.lng noiseDevice.longitude
marker.json({
custom_marker: "marker html here"
})
end