Len*_*een 40 google-maps responsive-design
如何从代码中制作响应式谷歌地图
<div class="map">
<iframe>...</iframe>
</div>
Run Code Online (Sandbox Code Playgroud)
我在css中使用完整的地图
.map{max-width:100%;}
Run Code Online (Sandbox Code Playgroud)
和小设备
.map{max-width:40%;}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
有人有想法吗?谢谢.
小智 64
将其添加到初始化函数:
<script type="text/javascript">
function initialize() {
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
// Resize stuff...
google.maps.event.addDomListener(window, "resize", function() {
var center = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(center);
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
Nim*_*les 53
添加100%的iframe 的width和height属性对我来说一直很有用.例如
<iframe width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.in/maps?f=q&source=s_q&hl=en&geocode=+&q=surat&ie=UTF8&hq=&hnear=Surat,+Gujarat&ll=21.195,72.819444&spn=0.36299,0.676346&t=m&z=11&output=embed"></iframe><br />
Run Code Online (Sandbox Code Playgroud)
小智 25
没有javascript的解决方案:
HTML:
<div class="google-maps">
<iframe>........//Google Maps Code//..........</iframe>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
.google-maps {
position: relative;
padding-bottom: 75%; // This is the aspect ratio
height: 0;
overflow: hidden;
}
.google-maps iframe {
position: absolute;
top: 0;
left: 0;
width: 100% !important;
height: 100% !important;
}
Run Code Online (Sandbox Code Playgroud)
如果您有自定义地图尺寸,请删除"身高:100%!重要"
小智 9
最好使用Google Map API.我在这里创建了一个例子:http://jsfiddle.net/eugenebolotin/8m1s69e5/1/
主要功能是使用功能:
//Full example is here: http://jsfiddle.net/eugenebolotin/8m1s69e5/1/
map.setCenter(map_center);
// Scale map to fit specified points
map.fitBounds(path_bounds);
Run Code Online (Sandbox Code Playgroud)
它处理调整大小事件并自动调整大小.
小智 6
在 iframe 标记中,您可以轻松添加 width='100%' 而不是地图给您的预设值
像这样:
<iframe src="https://www.google.com/maps/embed?anyLocation" width="100%" height="400" frameborder="0" style="border:0;" allowfullscreen=""></iframe>
Run Code Online (Sandbox Code Playgroud)
检查此解决方案:http://jsfiddle.net/panchroma/5AEEV/
不过,我不能把代码归功于它,它来自于使谷歌地图 iframe 嵌入响应式的快速而简单的方法。
祝你好运!
CSS
#map_canvas{
width:50%;
height:300px !important;
}
Run Code Online (Sandbox Code Playgroud)
超文本标记语言
<!DOCTYPE html>
<html>
<head>
<title>Google Maps JavaScript API v3 Example: Map Simple</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
<meta charset="utf-8"/>
<style>
html, body, #map_canvas {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var map;
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
114891 次 |
| 最近记录: |