Google 地图 API --地图未显示
Google Maps API --Map not displaying
第一次使用 Google 地图 API,地图没有显示。我已经阅读了很多关于导致问题的高度和宽度声明的内容,我尝试使用所有相关样式从 % 切换到 px 并且没有任何变化。
我在 rails 4.2.1
工作
地图 Javascript 最初是 Bootstrap 模板的一部分
这是我的 Javascript:
function init() {
var mapOptions = {
zoom: 15,
center: new google.maps.LatLng(40.6700, -73.9400), // New York
// Disables the default Google Maps UI components
disableDefaultUI: true,
scrollwheel: false,
draggable: false,
// How you would like to style the map.
styles: [{
"featureType": "road",
"elementType": "geometry",
"stylers": [
{
"lightness": 100
},
{
"visibility": "simplified"
}
]
},
{
"featureType": "water",
"elementType": "geometry",
"stylers": [
{
"visibility": "on"
},
{
"color": "#C6E2FF"
}
]
},
{
"featureType": "poi",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#C5E3BF"
}
]
},
{
"featureType": "road",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#D1D1B8"
}]
}]
};
// When the window has finished loading create our google map below
google.maps.event.addDomListener(window, 'load', init);
// Get the HTML DOM element that will contain your map
var mapElement = document.getElementById('map');
// Create the Google Map using our element and options defined above
var map = new google.maps.Map(mapElement, mapOptions);
在index.html.erb,
地图应该显示的 div 位置:
<div id="map"></div>
脚本标签 link 到 API:
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=my_api_key_here">
</script>
我的 .less 文件中的关联样式:
#map {
width: 100%;
height: 200px;
margin-top: 100px;
}
@media(min-width:767px) {
#map {
height: 400px;
margin-top: 250px;
}
}
我的 .css 文件中的关联样式:
#map {
width: 100%;
height: 200px;
margin-top: 100px;
}
@media(min-width:767px) {
#map {
height: 400px;
margin-top: 250px;
}
}
任何建议将不胜感激,谢谢!
我找到了修复此解决方案的答案。
我在 html:
中的脚本标签中添加了 callback=initialize
选项
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize"></script>
这 post 帮助了:
Async Google Maps API v3 undefined is not a function
第一次使用 Google 地图 API,地图没有显示。我已经阅读了很多关于导致问题的高度和宽度声明的内容,我尝试使用所有相关样式从 % 切换到 px 并且没有任何变化。
我在 rails 4.2.1
工作
地图 Javascript 最初是 Bootstrap 模板的一部分
这是我的 Javascript:
function init() {
var mapOptions = {
zoom: 15,
center: new google.maps.LatLng(40.6700, -73.9400), // New York
// Disables the default Google Maps UI components
disableDefaultUI: true,
scrollwheel: false,
draggable: false,
// How you would like to style the map.
styles: [{
"featureType": "road",
"elementType": "geometry",
"stylers": [
{
"lightness": 100
},
{
"visibility": "simplified"
}
]
},
{
"featureType": "water",
"elementType": "geometry",
"stylers": [
{
"visibility": "on"
},
{
"color": "#C6E2FF"
}
]
},
{
"featureType": "poi",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#C5E3BF"
}
]
},
{
"featureType": "road",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#D1D1B8"
}]
}]
};
// When the window has finished loading create our google map below
google.maps.event.addDomListener(window, 'load', init);
// Get the HTML DOM element that will contain your map
var mapElement = document.getElementById('map');
// Create the Google Map using our element and options defined above
var map = new google.maps.Map(mapElement, mapOptions);
在index.html.erb,
地图应该显示的 div 位置:
<div id="map"></div>
脚本标签 link 到 API:
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=my_api_key_here">
</script>
我的 .less 文件中的关联样式:
#map {
width: 100%;
height: 200px;
margin-top: 100px;
}
@media(min-width:767px) {
#map {
height: 400px;
margin-top: 250px;
}
}
我的 .css 文件中的关联样式:
#map {
width: 100%;
height: 200px;
margin-top: 100px;
}
@media(min-width:767px) {
#map {
height: 400px;
margin-top: 250px;
}
}
任何建议将不胜感激,谢谢!
我找到了修复此解决方案的答案。
我在 html:
中的脚本标签中添加了callback=initialize
选项
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize"></script>
这 post 帮助了:
Async Google Maps API v3 undefined is not a function