如何在您的网站中嵌入风格的谷歌地图

时间:2017-04-18 07:27:46

标签: javascript html json google-maps google-maps-api-3

我从这里使用过谷歌地图编辑服务:https://developers.google.com/maps/documentation/javascript/styling

现在这个链接提供了两件事: 一个json代码 2. google api链接

但我不知道如何在我的HTML中使用json,早些时候我已经在我的网站中嵌入了简单的谷歌地图javascript [像var google new map]。 PLZ帮助我将这个json代码添加到我的javascript或html。

这是JSON代码

[
{
"elementType": "geometry",
"stylers": [
  {
    "color": "#242f3e"
  }
]
},
{
"elementType": "labels.text.fill",
"stylers": [
  {
    "color": "#746855"
  }
]
},
{
"elementType": "labels.text.stroke",
"stylers": [
  {
    "color": "#242f3e"
  }
]
},
{
"featureType": "administrative.country",
"elementType": "geometry",
"stylers": [
  {
    "color": "#af01ab"
  }
]
},
{
"featureType": "administrative.locality",
"elementType": "geometry",
"stylers": [
  {
    "color": "#fe70fb"
  }
]
},
{
"featureType": "administrative.locality",
"elementType": "labels.text.fill",
"stylers": [
  {
    "color": "#d59563"
  }
]
},
{
"featureType": "administrative.province",
"elementType": "geometry",
"stylers": [
  {
    "color": "#fe01f8"
  }
]
},
{
"featureType": "poi",
"elementType": "labels.text.fill",
"stylers": [
  {
    "color": "#d59563"
  }
]
},
{
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [
  {
    "color": "#169c03"
  }
]
},
{
"featureType": "poi.park",
"elementType": "labels.text.fill",
"stylers": [
  {
    "color": "#6b9a76"
  }
]
},
{
"featureType": "road",
"elementType": "geometry",
"stylers": [
  {
    "color": "#38414e"
  }
]
},
{
"featureType": "road",
"elementType": "geometry.stroke",
"stylers": [
  {
    "color": "#212a37"
  }
]
},
{
"featureType": "road",
"elementType": "labels.text.fill",
"stylers": [
  {
    "color": "#9ca5b3"
  }
]
},
{
"featureType": "road.arterial",
"elementType": "geometry",
"stylers": [
  {
    "color": "#7274d3"
  }
]
},
{
"featureType": "road.highway",
"elementType": "geometry",
"stylers": [
  {
    "color": "#4d4ddb"
  }
]
},
{
"featureType": "road.highway",
"elementType": "geometry.stroke",
"stylers": [
  {
    "color": "#1f2835"
  }
]
},
{
"featureType": "road.highway",
"elementType": "labels.text.fill",
"stylers": [
  {
    "color": "#f3d19c"
  }
]
},
{
"featureType": "road.local",
"elementType": "geometry",
"stylers": [
  {
    "color": "#9898b8"
  }
]
},
{
"featureType": "transit",
"elementType": "geometry",
"stylers": [
  {
    "color": "#2f3948"
  }
]
},
{
"featureType": "transit.line",
"elementType": "geometry",
"stylers": [
  {
    "color": "#fafe52"
  }
]
},
{
"featureType": "transit.station",
"elementType": "labels.text.fill",
"stylers": [
  {
    "color": "#d59563"
  }
]
},
{
"featureType": "transit.station.rail",
"elementType": "geometry",
"stylers": [
  {
    "color": "#fafe52"
  }
]
},
{
"featureType": "water",
"elementType": "geometry",
"stylers": [
  {
    "color": "#169ba3"
  }
]
},
{
"featureType": "water",
"elementType": "labels.text.fill",
"stylers": [
  {
    "color": "#515c6d"
  }
]
},
{
"featureType": "water",
"elementType": "labels.text.stroke",
"stylers": [
  {
    "color": "#17263c"
  }
]
}
]

这是link

https://maps.googleapis.com/maps/api/staticmap?key=YOUR_API_KEY&center=29.921068146058925,433.86609008312223&zoom=17&format=png&maptype=roadmap&style=element:geometry%7Ccolor:0x242f3e&style=element:labels.text.fill%7Ccolor:0x746855&style=element:labels.text.stroke%7Ccolor:0x242f3e&style=feature:administrative.country%7Celement:geometry%7Ccolor:0xaf01ab&style=feature:administrative.locality%7Celement:geometry%7Ccolor:0xfe70fb&style=feature:administrative.locality%7Celement:labels.text.fill%7Ccolor:0xd59563&style=feature:administrative.province%7Celement:geometry%7Ccolor:0xfe01f8&style=feature:poi%7Celement:labels.text.fill%7Ccolor:0xd59563&style=feature:poi.park%7Celement:geometry%7Ccolor:0x169c03&style=feature:poi.park%7Celement:labels.text.fill%7Ccolor:0x6b9a76&style=feature:road%7Celement:geometry%7Ccolor:0x38414e&style=feature:road%7Celement:geometry.stroke%7Ccolor:0x212a37&style=feature:road%7Celement:labels.text.fill%7Ccolor:0x9ca5b3&style=feature:road.arterial%7Celement:geometry%7Ccolor:0x7274d3&style=feature:road.highway%7Celement:geometry%7Ccolor:0x4d4ddb&style=feature:road.highway%7Celement:geometry.stroke%7Ccolor:0x1f2835&style=feature:road.highway%7Celement:labels.text.fill%7Ccolor:0xf3d19c&style=feature:road.local%7Celement:geometry%7Ccolor:0x9898b8&style=feature:transit%7Celement:geometry%7Ccolor:0x2f3948&style=feature:transit.line%7Celement:geometry%7Ccolor:0xfafe52&style=feature:transit.station%7Celement:labels.text.fill%7Ccolor:0xd59563&style=feature:transit.station.rail%7Celement:geometry%7Ccolor:0xfafe52&style=feature:water%7Celement:geometry%7Ccolor:0x169ba3&style=feature:water%7Celement:labels.text.fill%7Ccolor:0x515c6d&style=feature:water%7Celement:labels.text.stroke%7Ccolor:0x17263c&size=480x360

1 个答案:

答案 0 :(得分:0)

您需要以下步骤才能显示地图

  1. 获取Google地图Api key
  2. 启用Google Static Maps API https://console.developers.google.com/apis/api/static_maps_backend/overview?PROJECTNAME
  3. 将Google静态代码视为可以使用HTML或CSS显示图像的图像。
  4. 示例

    <img width="600" src="https://maps.googleapis.com/maps/api/staticmap?key=YOUR_API_KEY&center=29.921068146058925,433.86609008312223&zoom=17&format=png&maptype=roadmap&style=element:geometry%7Ccolor:0x242f3e&style=element:labels.text.fill%7Ccolor:0x746855&style=element:labels.text.stroke%7Ccolor:0x242f3e&style=feature:administrative.country%7Celement:geometry%7Ccolor:0xaf01ab&style=feature:administrative.locality%7Celement:geometry%7Ccolor:0xfe70fb&style=feature:administrative.locality%7Celement:labels.text.fill%7Ccolor:0xd59563&style=feature:administrative.province%7Celement:geometry%7Ccolor:0xfe01f8&style=feature:poi%7Celement:labels.text.fill%7Ccolor:0xd59563&style=feature:poi.park%7Celement:geometry%7Ccolor:0x169c03&style=feature:poi.park%7Celement:labels.text.fill%7Ccolor:0x6b9a76&style=feature:road%7Celement:geometry%7Ccolor:0x38414e&style=feature:road%7Celement:geometry.stroke%7Ccolor:0x212a37&style=feature:road%7Celement:labels.text.fill%7Ccolor:0x9ca5b3&style=feature:road.arterial%7Celement:geometry%7Ccolor:0x7274d3&style=feature:road.highway%7Celement:geometry%7Ccolor:0x4d4ddb&style=feature:road.highway%7Celement:geometry.stroke%7Ccolor:0x1f2835&style=feature:road.highway%7Celement:labels.text.fill%7Ccolor:0xf3d19c&style=feature:road.local%7Celement:geometry%7Ccolor:0x9898b8&style=feature:transit%7Celement:geometry%7Ccolor:0x2f3948&style=feature:transit.line%7Celement:geometry%7Ccolor:0xfafe52&style=feature:transit.station%7Celement:labels.text.fill%7Ccolor:0xd59563&style=feature:transit.station.rail%7Celement:geometry%7Ccolor:0xfafe52&style=feature:water%7Celement:geometry%7Ccolor:0x169ba3&style=feature:water%7Celement:labels.text.fill%7Ccolor:0x515c6d&style=feature:water%7Celement:labels.text.stroke%7Ccolor:0x17263c&size=480x360" alt="Google Map of Albany, NY">
    

    有关详情,请参阅here

    如果您想通过JSON进行操作,则需要查看Google official example