将自定义地图标记图标添加到Google Map API

时间:2015-12-29 16:05:55

标签: javascript google-maps google-maps-api-3

我已经做了好几十次,但今天它似乎并不想工作。我有以下代码,地图引脚拒绝显示?

 class JiraParser
   def initialize(json_data)
     @data = json_data
   end

   def total
     @data['total']
   end

   def summary(i)
     @issues = @data['issues']
     @issue = @issues[i]
     @fields = @issue['fields']
     @fields['summary']
   end

   def to_json_total
     {:total => total}
    end

    def to_json_issue(i)
      {:summary => summary(i)}
    end
  end

3 个答案:

答案 0 :(得分:2)

在您运行用于创建地图的initialize()函数之前,我认为您正在创建标记。要么在initialize()函数中包含标记创建,要么更改创建它的顺序。

答案 1 :(得分:1)

解决方案是

  function initialize() {
    var mapCanvas = document.getElementById('map');
    var mapOptions = {
      center: new google.maps.LatLng(55.555555, -2.323702),
      zoom: 10,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(mapCanvas, mapOptions)

    var image = 'http://www.clker.com/cliparts/I/l/L/S/W/9/map-marker-hi.png';
    var beachMarker = new google.maps.Marker({
      position: {lat: 55.555555, lng: -2.323702},
      map: map,
      icon: image
    });

    }

答案 2 :(得分:0)

使用your code as posted,我在控制台中收到javascript错误:Uncaught ReferenceError: map is not defined,因为map变量是initialize函数的本地变量,而您的标记代码在该函数之外。在initialize函数内移动标记的创建。 (标记也有点大,所以下面的代码将它缩放到更合理的大小)。

function initialize() {
  var mapCanvas = document.getElementById('map');
  var mapOptions = {
    center: new google.maps.LatLng(55.555555, -2.323702),
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(mapCanvas, mapOptions);

  // move inside initialize function
  var image = 'http://www.clker.com/cliparts/I/l/L/S/W/9/map-marker-hi.png';
  var beachMarker = new google.maps.Marker({
    position: {
      lat: 55.555555,
      lng: -2.323702
    },
    map: map,
    icon: {
      url: image,
      scaledSize: new google.maps.Size(20, 32)
    }
  });
} // end of initialize
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>