Google 地图 rails 5
Google Maps rails 5
我有 "Imovels"(属性)的 CRUD,所以在数据库中我有不同字段的地址。即:
<%= f.text_field :logradouro %> # street name
<%= f.number_field :numero %> # number
<%= f.collection_select :bairro_id, @bairros, :id, :nome, {} %> # neighborhood
<%= f.collection_select :cidade_id, @cidades, :id, :nome, {} %> # city
我正在使用 gem gmaps4rails 在地图上显示位置。我按照 gem 文档中的教程进行操作,但地图没有呈现。
Puma 日志显示两个错误
ActionController::RoutingError (No route matches [GET] "/assets/underscore-min.map"):
NoMethodError (undefined method `geocoded_by' for #<Class:0x007fe7c02f2fb0>):
型号:
class Imovel < ApplicationRecord
geocoded_by :enderecoCompleto
after_validation :geocode
end
连接:
def show
@enderecoCompleto = "#{@imovel.logradouro}, #{@imovel.cidade.nome}"
end
观点:
在这里,我想显示基于 endereçoCompleto 的地图,即街道名称和城市。我真的不知道如何在这里进行。
<div style='width: 800px;'>
<div id="map" style='width: 800px; height: 400px;'></div>
</div>
<script>
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers([
{
"lat": 0,
"lng": 0,
"picture": {
"url": "http://people.mozilla.com/~faaborg/files/shiretoko/firefoxIcon/firefox-32.png",
"width": 32,
"height": 32
},
"infowindow": "hello!"
}
]);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>
</div>
如果我将该代码放在模型上,返回的错误是:
NoMethodError (undefined method `geocoded_by' for #<Class:0x007fe7c02f2fb0>):
我上面怎么说的。
我如何根据数据库中的地址(没有经纬度)渲染这张地图?
在@simple lime 的帮助下解决了地理编码问题,但现在页面呈现但地图不显示。实际上搞砸了其他 javascript 代码。好像跟turbo links有关。
在视图中:
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=XXXXXX&callback=initMap">
</script>
<script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js"></script>
<script src='//cdn.rawgit.com/printercu/google-maps-utility-library-v3-read-only/master/infobox/src/infobox_packed.js' type='text/javascript'></script>
在 application.js:
//= require underscore
//= require gmaps/google
在控制台上:
查看 the gem + the youtube video on it's GitHub page. It appears 您还需要安装 geocoder
gem。这就是给你 geocoded_by
方法的原因。
查看 geocoder,如果您将 latitude:float longitude:float
添加到您的模型,它应该会自动获取并更新您模型中的 after_validation :geocode
中的那些。从那里,您可以将 latitude
和 longitude
传递到您的视图并替换
"lat": 0,
"lng": 0,
和他们在一起。请务必通读 geocoder
gem 页面,那里有一些有用的东西。
编辑
对于最新一期,我对 javascript 不太满意,但我会尝试从您的 google 地图中删除 async defer
和 &callback=initMap
javascript 标记(因为您没有使用该名称定义的函数)。
原始答案,可能是问题中的错字而不是代码的实际问题。
NoMethodError
是由拼写错误引起的 gecoded_by
应该是 geocoded_by
。 RoutingError
仅适用于不应引起任何问题的源映射文件。
我有 "Imovels"(属性)的 CRUD,所以在数据库中我有不同字段的地址。即:
<%= f.text_field :logradouro %> # street name
<%= f.number_field :numero %> # number
<%= f.collection_select :bairro_id, @bairros, :id, :nome, {} %> # neighborhood
<%= f.collection_select :cidade_id, @cidades, :id, :nome, {} %> # city
我正在使用 gem gmaps4rails 在地图上显示位置。我按照 gem 文档中的教程进行操作,但地图没有呈现。
Puma 日志显示两个错误
ActionController::RoutingError (No route matches [GET] "/assets/underscore-min.map"):
NoMethodError (undefined method `geocoded_by' for #<Class:0x007fe7c02f2fb0>):
型号:
class Imovel < ApplicationRecord
geocoded_by :enderecoCompleto
after_validation :geocode
end
连接:
def show
@enderecoCompleto = "#{@imovel.logradouro}, #{@imovel.cidade.nome}"
end
观点: 在这里,我想显示基于 endereçoCompleto 的地图,即街道名称和城市。我真的不知道如何在这里进行。
<div style='width: 800px;'>
<div id="map" style='width: 800px; height: 400px;'></div>
</div>
<script>
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers([
{
"lat": 0,
"lng": 0,
"picture": {
"url": "http://people.mozilla.com/~faaborg/files/shiretoko/firefoxIcon/firefox-32.png",
"width": 32,
"height": 32
},
"infowindow": "hello!"
}
]);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>
</div>
如果我将该代码放在模型上,返回的错误是:
NoMethodError (undefined method `geocoded_by' for #<Class:0x007fe7c02f2fb0>):
我上面怎么说的。 我如何根据数据库中的地址(没有经纬度)渲染这张地图?
在@simple lime 的帮助下解决了地理编码问题,但现在页面呈现但地图不显示。实际上搞砸了其他 javascript 代码。好像跟turbo links有关。
在视图中:
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=XXXXXX&callback=initMap">
</script>
<script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js"></script>
<script src='//cdn.rawgit.com/printercu/google-maps-utility-library-v3-read-only/master/infobox/src/infobox_packed.js' type='text/javascript'></script>
在 application.js:
//= require underscore
//= require gmaps/google
在控制台上:
查看 the gem + the youtube video on it's GitHub page. It appears 您还需要安装 geocoder
gem。这就是给你 geocoded_by
方法的原因。
查看 geocoder,如果您将 latitude:float longitude:float
添加到您的模型,它应该会自动获取并更新您模型中的 after_validation :geocode
中的那些。从那里,您可以将 latitude
和 longitude
传递到您的视图并替换
"lat": 0,
"lng": 0,
和他们在一起。请务必通读 geocoder
gem 页面,那里有一些有用的东西。
编辑
对于最新一期,我对 javascript 不太满意,但我会尝试从您的 google 地图中删除 async defer
和 &callback=initMap
javascript 标记(因为您没有使用该名称定义的函数)。
原始答案,可能是问题中的错字而不是代码的实际问题。
NoMethodError
是由拼写错误引起的 gecoded_by
应该是 geocoded_by
。 RoutingError
仅适用于不应引起任何问题的源映射文件。