Javascript 如何在Gmaps4rails中居中标记
阅读之后,我仍然无法让标记显示在页面中心 我的代码如下 在视图中Javascript 如何在Gmaps4rails中居中标记,javascript,ruby-on-rails-4,google-maps-api-3,gmaps4rails,Javascript,Ruby On Rails 4,Google Maps Api 3,Gmaps4rails,阅读之后,我仍然无法让标记显示在页面中心 我的代码如下 在视图中 <div class="Flexible-container"> <!--Google Maps Div--> <div id="map" style='width: 425; height: 425px;' frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></div> </di
<div class="Flexible-container">
<!--Google Maps Div-->
<div id="map" style='width: 425; height: 425px;' frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></div>
</div>
<!-- End of Responsive iFrame -->
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.map.centerOn(markers[0]);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
handler.map.serviceObject.setZoom(16);
});
</script>
CSS
目前,页面上的标记位于容器div的一角。如何使标记显示在中心?有没有办法改进视图中的map div
谢谢大家! 你必须知道你想要什么:
// to center on a marker
handler.map.centerOn(markers[0]);
// to set the map zoom
handler.getMap().setZoom(16);
// to center the map AND adjust zoom to see ALL markers
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
因此,保留您喜欢的代码,但不要混用,只需是否需要在centerOn之后设置Zoom?
/* Flexible iFrame */
.Flexible-container {
position: relative;
padding-bottom: 56.25%;
height: 0;
overflow: hidden;
}
.Flexible-container iframe,
.Flexible-container object,
.Flexible-container embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
// to center on a marker
handler.map.centerOn(markers[0]);
// to set the map zoom
handler.getMap().setZoom(16);
// to center the map AND adjust zoom to see ALL markers
handler.bounds.extendWith(markers);
handler.fitMapToBounds();