【发布时间】:2013-05-16 01:56:12
【问题描述】:
我有一个页面show_map.html.erb 显示谷歌地图(目前运行良好)。我想在地图上添加图像而不是图钉标记。我在 Google 上搜索了很多关于这个问题的信息,并找到了诸如 this question on StackOverflow 之类的资源。但是,我目前的解决方案不起作用。
在使用之前我必须上传图片吗?此外,为什么我不能使用像public/alert.png 或app/asset/images/alert.png 这样的图像?我也提到了the Google Maps API documentation,但是我看不懂例子。
我的控制器:
def show_map
@location = Location.find(params[:id])
@address = @location.landmark.gsub(","+")+",+"+@location.place.gsub(","+")+",+"+@location.country
p @address
end
我的看法:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=<my api key>&sensor=true"> type="text/javascript"></script>
<script type="text/javascript">
var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h2 id="firstHeading" class="firstHeading" ><font color="#FF0000"><%=@location.title%></font></h2>'+
'<div id="bodyContent" >'+
'<p><%=@location.description%></p>'+
'<p><a href="http:www.google.com">'+
'more info</a> (created on <%=(@location.created_at).strftime("%A, %d/%m/%Y")%>)</p>'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
//set the image
//var image = '/assets/images/alert.png';
// Enable the visual refresh
google.maps.visualRefresh = true;
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 17,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
//var image = "#{Rails.root}/app/assets/images/alert.png";
//code to search the address
//var address = document.getElementById("address").value;
var address='<%= @address.parameterize %>';
//alert(address);
geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title:"<%=@location.title%>",
animation: google.maps.Animation.DROP,
visible: true,
draggable: true,
icon: image
});
//animation for marker
function toggleBounce() {
if (marker.getAnimation() != null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.DROP);
}
}
//commented as show the info ON click on the marker
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
//listener for animation
google.maps.event.addListener(marker, 'click', toggleBounce);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
<body onload="initialize()">
<div id="map" align="right" style="min-height: 500px; height: 400px; width: 400px"></div>
</body>
【问题讨论】:
-
您在使用 gmaps4rails gem 吗? github.com/apneadiving/Google-Maps-for-Rails
标签: ruby-on-rails ruby-on-rails-3 google-maps-api-3 google-maps-markers