【问题标题】:Ruby on Rails: Adding a local image in a Google Maps markerRuby on Rails:在 Google 地图标记中添加本地图像
【发布时间】:2013-05-16 01:56:12
【问题描述】:

我有一个页面show_map.html.erb 显示谷歌地图(目前运行良好)。我想在地图上添加图像而不是图钉标记。我在 Google 上搜索了很多关于这个问题的信息,并找到了诸如 this question on StackOverflow 之类的资源。但是,我目前的解决方案不起作用。

在使用之前我必须上传图片吗?此外,为什么我不能使用像public/alert.pngapp/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>

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 google-maps-api-3 google-maps-markers


【解决方案1】:

如果您使用gmaps4rails gem 并假设您已添加所有依赖项和脚本,您的控制器中将有与以下内容非常相似的内容。您将替换“XYZ”并添加您自己的网址。

def index
  @XYZs = XYZ.all

  @hash = Gmaps4rails.build_markers(@XYZ) do |XYZ, marker|
  marker.lat XYZ.latitude
  marker.lng XYZ.longitude
  marker.infowindow XYZ.artist
  marker.picture({
    url: "WWW.YourWebAddressHere.com",
    width: 32,
    height: 32,
})
end

有我发现非常有用的 gem 的实时示例和视频文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 2011-09-27
    • 1970-01-01
    • 2018-10-21
    相关资源
    最近更新 更多