我想在地图上显示传感器的许多位置,我需要在标记上显示所有当前传感器值(不在信息窗口中,用户无需单击标记即可查看数据)
对我而言,创建许多不同的图标并更改图标是不现实的。
有没有办法在标记上显示动态数据?说在标记上方显示一个数字标签。我找到了一个,但是无法将其转换为ruby on rails代码。
这是我为java找到的链接。
这是用于在ROR上创建gmap的link有用
这就是我现在拥有的。使用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>
现在的问题是,当我尝试按照视频/教程并在视图中编辑文件时,整个google map窗口消失了。原因之一是添加buildMap(),另一个是更改脚本以
<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>
最佳答案
步骤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>
第三步:
在您的控制器中:
@hash = Gmaps4rails.build_markers(@noiseDevices) do |noiseDevice, marker|
marker.lat noiseDevice.latitude
marker.lng noiseDevice.longitude
marker.json({
custom_marker: "marker html here"
})
end