【问题标题】:google map API simple markers谷歌地图 API 简单标记
【发布时间】:2015-10-30 15:20:43
【问题描述】:

我有一个使用谷歌地图 API 的应用程序。我正在尝试在地图上显示简单的标记。标记是我数据库中的项目(包括纬度和经度值)。我可以在地图上显示一个标记,但我无法使用数据库中的所有项目填充地图。

我只能对每个单独的项目调用数据库。任何建议将不胜感激。

我的帮助文件:

def name(x)
    @name = Place.find(x).name
    return @name
end

def latitude(x)
    @latitude = Place.find(x).latitude
    return @latitude
end

def longitude(x)
    @longitude = Place.find(x).longitude
    return @longitude
end

这是我为地图创建的变量:

...

var myLatlng = new google.maps.LatLng(<%= latitude(1) %>, <%= longitude(1)%>)

...

            var marker1 = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: "<%= name(1) %>"
            });

...

【问题讨论】:

    标签: ruby-on-rails database google-maps google-maps-markers


    【解决方案1】:

    这是 html.haml 语法——在你的布局上初始化一个 js 数组,比如

    window.markers = [];
    

    然后在您的查看页面上-

    @places = Place.all
    @places.each do |place|
    #javascript code
      $(document).ready(function(){      
        window.markers.push([ new google.maps.LatLng(#{place.latitude},#
        {place.longitude}), "#{place.name}"]);
      });
    end
    

    查看js代码

    $.each(window.markers,function(i,e){
      var marker = new google.maps.Marker({
      position: e[0],
      title: e[1],            
      map: map
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 2015-05-09
      • 2020-10-12
      相关资源
      最近更新 更多