我正在尝试向我的谷歌地图添加样式。但是在浏览器中打开页面时,样式有时不会加载,有时会加载。
这是问题之一,但我猜可能与下一个问题有某种关系:
在移动平台上打开页面时(尝试过 android chrome、ios safari 和 windows phone IE),样式永远不会加载。
如果这篇文章不正确或不符合标准,我很抱歉。这是我第一次在 stackoverflow 上发帖。
非常感谢您的帮助!
function initialize() {
var mapOptions = {
zoom: 13,
};
map = new google.maps.Map(document.getElementById('mapCanvas'),
mapOptions);
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'This is you'
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
handleNoGeolocation(false);
}
}
var styles = [
{
"elementType": "labels.text.fill",
"stylers": [
{ "invert_lightness": true },
{ "gamma": 0.01 },
{ "hue": "#e500ff" }
]
},{
"elementType": "geometry",
"stylers": [
{ "hue": "#00fff7" }
]
},{
"stylers": [
{ "gamma": 0.78 },
{ "visibility": "on" },
{ "invert_lightness": true }
]
}
]
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'This is not where you are, right?';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
var infowindow = new google.maps.InfoWindow(options);
map.setOptions({styles: styles});
map.setCenter(options.position);
}
google.maps.event.addDomListener(window, 'load', initialize);
然后 HTML 是:
<link rel="shortcut icon" href="images/favicon.ico">
<link href='css/style.css' rel='stylesheet' />
<script src="http://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
<script src='js/main.js'></script>
<style>
body {
margin:0; padding:0;
}
#mapCanvas {
position:absolute; top:0; bottom:0; width:100%;
z-index: 1;
}
</style>
</head>
<body onload="initialize()">
<div id="mapCanvas"></div>