向Angular2 Google Maps添加样式

时间:2017-02-15 12:10:44

标签: google-maps angular typescript

您好我是Angular2 / Typescript的新手,我正在尝试使用Angular2 Google Maps Components设置我添加到Angular2项目中的地图,但我无法弄清楚如何使用它尚未记录MapTypeStyle Interface 3}}。如何在我的模块和html中使用它?地图模块可以工作,但我没有应用样式。任何帮助赞赏。

根据Google MapsTypeStyle Reference

HTML:

<sebm-google-map [latitude]="lat" [longitude]="lng">
  <sebm-google-map-marker [latitude]="lat" [longitude]="lng"></sebm-google-map-marker>
</sebm-google-map>

模块(摘录)

export class GmapComponent implements OnInit {

  title: string = 'Current Location';
  lat: number = 50.937531;
  lng: number = 6.960278600000038;
  constructor() { }

  ngOnInit() {
  }
}

3 个答案:

答案 0 :(得分:6)

文档不是很有用,所以我不得不深入研究组件的代码。

<sebm-google-map [latitude]="lat" [longitude]="lng" [styles]="styles">
  <sebm-google-map-marker [latitude]="lat" [longitude]="lng"></sebm-google-map-marker>
</sebm-google-map>

只需添加styles即可,其中styles的类型为MapTypeStyle[] declared here

尝试将styles定义为:

let styles = [{
  "featureType": "water",
  "stylers": [{
      "color": "#ff0000"
    }]
}];

应该让你的水变红,但我还没有自己测试过,我只是把它从代码中解脱出来。

答案 1 :(得分:5)

这对我有用。 ☺:-p

<sebm-google-map *ngIf="map" [latitude]="placeLat" [longitude]="placeLng" [scrollwheel]="false" [zoom]="zoom" [disableDefaultUI]="true" [styles]='[
            {
                elementType : "labels.icon",
                stylers : [{
                  visibility : "off"
                }]
            }]'>

答案 2 :(得分:1)

我的最终解决方案。但我仍然不明白MapTypeStyle接口的使用位置和方式。

html添加[styles]="customStyle"

<sebm-google-map [latitude]="lat" [longitude]="lng" [styles]="customStyle" >
  <sebm-google-map-marker [latitude]="lat" [longitude]="lng" ></sebm-google-map-marker>
</sebm-google-map>

组件(摘录)添加public customStyle = [{ "JSON style declaration goes here" }]

export class GmapComponent implements OnInit {

  public customStyle = [
    {
      "elementType": "geometry",
      "stylers": [
        {
          "hue": "#ff4400"
        },
        {
          "saturation": -100
        },
        {
          "lightness": -4
        },
        {
          "gamma": 0.72
        }
      ]
    },
    {
      "featureType": "road",
      "elementType": "labels.icon"
    },
    {
      "featureType": "landscape.man_made",
      "elementType": "geometry",
      "stylers": [
        {
          "hue": "#0077ff"
        },
        {
          "gamma": 3.1
        }
      ]
    },
    {
      "featureType": "water",
      "stylers": [
        {
          "hue": "#00ccff"
        },
        {
          "gamma": 0.44
        },
        {
          "saturation": -33
        }
      ]
    },
    {
      "featureType": "poi.park",
      "stylers": [
        {
          "hue": "#44ff00"
        },
        {
          "saturation": -23
        }
      ]
    },
    {
      "featureType": "water",
      "elementType": "labels.text.fill",
      "stylers": [
        {
          "hue": "#007fff"
        },
        {
          "gamma": 0.77
        },
        {
          "saturation": 65
        },
        {
          "lightness": 99
        }
      ]
    },
    {
      "featureType": "water",
      "elementType": "labels.text.stroke",
      "stylers": [
        {
          "gamma": 0.11
        },
        {
          "weight": 5.6
        },
        {
          "saturation": 99
        },
        {
          "hue": "#0091ff"
        },
        {
          "lightness": -86
        }
      ]
    },
    {
      "featureType": "transit.line",
      "elementType": "geometry",
      "stylers": [
        {
          "lightness": -48
        },
        {
          "hue": "#ff5e00"
        },
        {
          "gamma": 1.2
        },
        {
          "saturation": -23
        }
      ]
    },
    {
      "featureType": "transit",
      "elementType": "labels.text.stroke",
      "stylers": [
        {
          "saturation": -64
        },
        {
          "hue": "#ff9100"
        },
        {
          "lightness": 16
        },
        {
          "gamma": 0.47
        },
        {
          "weight": 2.7
        }
      ]
    }
  ];



  title: string = 'Current Location';
  lat: number = 50.937531;
  lng: number = 6.960278600000038;

  constructor() {

  }

  ngOnInit() {
  }

}