无法在谷歌api上获得标记

时间:2014-12-26 13:55:57

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

我正在使用gem "geocoder"从地址获取lang和long,但我似乎无法在地图上找到标记

  - lat_long = Geocoder.coordinates(Mode.full_address).to_s.gsub('[', '').gsub(']', '')
  %script{:src => "https://maps.googleapis.com/maps/api/js?v=3.exp"}
  :javascript
    var map;
    function initialize() {
      var mapOptions = {
        zoom: 8,
        center: new google.maps.LatLng(#{lat_long})
      };
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    }
    google.maps.event.addDomListener(window, 'load', initialize);

这会将地图放在应有的位置,但它不会给我一个标记

1 个答案:

答案 0 :(得分:2)

添加google.maps.Marker

var map;

function initialize() {

    var mapOptions = {
        zoom: 8,
        center: new google.maps.LatLng(#{lat_long})
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    // Create a marker on map center
    var marker = new google.maps.Marker({
        position: map.getCenter(),
        map: map,
    });
}

google.maps.event.addDomListener(window, 'load', initialize);