在我的谷歌地图代码中,我添加地图样式的代码
Where in my Google Map code do I add the code for the map style?
我有以下自定义的谷歌地图:
<!DOCTYPE html>
<html>
<head>
<style>
#map {
width: 700px;
height: 700px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
var mapCanvas = document.getElementById('map');
var mapOptions = {
center: new google.maps.LatLng(41.2048114,8.0734625),
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions)
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>
来源:https://developers.google.com/maps/tutorials/fundamentals/adding-a-google-map
使用官方指南,我不知道应该在哪里添加以下代码:
map.set('styles', [
{
featureType: 'road',
elementType: 'geometry',
stylers: [
{ color: '#000000' },
{ weight: 1.6 }
]
}, {
featureType: 'road',
elementType: 'labels',
stylers: [
{ saturation: -100 },
{ invert_lightness: true }
]
}, {
featureType: 'landscape',
elementType: 'geometry',
stylers: [
{ hue: '#ffff00' },
{ gamma: 1.4 },
{ saturation: 82 },
{ lightness: 96 }
]
}, {
featureType: 'poi.school',
elementType: 'geometry',
stylers: [
{ hue: '#fff700' },
{ lightness: -15 },
{ saturation: 99 }
]
}
]);
来源:https://developers.google.com/maps/documentation/javascript/styling
你应该把它添加到地图选项中:
var mapOptions = {
center: new google.maps.LatLng(41.2048114,8.0734625),
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: [
{
featureType: 'road',
elementType: 'geometry',
stylers: [
{ color: '#000000' },
{ weight: 1.6 }
]
}, {
featureType: 'road',
elementType: 'labels',
stylers: [
{ saturation: -100 },
{ invert_lightness: true }
]
}, {
featureType: 'landscape',
elementType: 'geometry',
stylers: [
{ hue: '#ffff00' },
{ gamma: 1.4 },
{ saturation: 82 },
{ lightness: 96 }
]
}, {
featureType: 'poi.school',
elementType: 'geometry',
stylers: [
{ hue: '#fff700' },
{ lightness: -15 },
{ saturation: 99 }
]
}
]
}
另一个选项(除了将其放在google.maps.Map构造函数中的mapOptions中)是在定义了map
变量之后,将代码放入initialize
函数中:
小提琴
代码片段:
function initialize() {
var mapCanvas = document.getElementById('map');
var mapOptions = {
center: new google.maps.LatLng(41.2048114, 8.0734625),
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions);
map.set('styles', [{
featureType: 'road',
elementType: 'geometry',
stylers: [{
color: '#000000'
}, {
weight: 1.6
}]
}, {
featureType: 'road',
elementType: 'labels',
stylers: [{
saturation: -100
}, {
invert_lightness: true
}]
}, {
featureType: 'landscape',
elementType: 'geometry',
stylers: [{
hue: '#ffff00'
}, {
gamma: 1.4
}, {
saturation: 82
}, {
lightness: 96
}]
}, {
featureType: 'poi.school',
elementType: 'geometry',
stylers: [{
hue: '#fff700'
}, {
lightness: -15
}, {
saturation: 99
}]
}]);
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
width: 100%;
height: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>
相关文章:
- 谷歌地图Api和JS代码不工作
- 谷歌地图&地理代码
- 谷歌地图代码不起作用
- 谷歌地图,基于国家代码可视化国家
- 谷歌地图自动完成,即使使用工作代码也无法工作
- 如何通过JavaScript代码显示谷歌地图并在设备上获取坐标
- 谷歌地图 v2 到 v3 代码
- 谷歌地图背后的代码
- 在代码笔中嵌入谷歌地图
- 我的代码在下面,我想更改 ID 与地图外部的
- 使用纬度和经度代码脚本生成谷歌地图
- 我的谷歌地图代码有问题
- 点击操作不适用于 Google 地图中的以下代码
- 如何将可嵌入的 Google 地图导出到等效的 Map API JavaScript 代码中
- 谷歌地图多个标记.我的代码有什么问题
- 谷歌地图API:显示代码中的鼠标悬停窗口
- 修改谷歌地图 API 代码
- 必应地图,查找多个地理代码搜索结果(你的意思是:功能)
- 谷歌地图代码显示当前位置,并在Php:-Firefox中触摸时创建新标记
- 如何在地图API v3代码点火器上设置cusctom多标记