2017-02-15 108 views
4

嗨我是Angular2/Typescript的新手,我嘗試使用Angular2 Google Maps Components添加到我的Angular2項目中,但我無法弄清楚如何使用尚未記錄的地圖MapTypeStyle Interface。我如何在我的模塊和html中使用它?地圖模塊的工作原理,但我沒有得到應用的樣式。任何幫助讚賞。將風格添加到Angular2 Google地圖

的根據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() { 
    } 
} 
+0

你試圖應用什麼風格? [入門指南](https://angular-maps.com/docs/getting-started.html)只是說在組件聲明中使用'styleUrls:['app.component.css']'。您的示例沒有提供有關您嘗試應用的樣式的詳細信息。 – Adam

+0

在組件css中,我只向組件本身添加樣式,例如地圖的寬度和高度。我想實現的目標是將JASON格式化的樣式應用於https://mapstyle.withgoogle.com/的示例,並且從我的未確認的需要通過api添加(?) –

回答

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" 
    }] 
}]; 

應該讓你的水紅色,但我還沒有測試此我自己呢,我只是立足它關閉的代碼。

+0

您好,我希望更改標籤測試顏色它可能嗎? –

0

我最終的工作解決方案。但我仍然不明白MapTypeStyle接口在哪裏以及如何使用。

HTML添加添加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() { 
    } 

} 
+1

自定義樣式是「MapTypeStyle」類型。在文檔的頂部,如果您從'angular2-google-maps'添加了import {MapTypeStyle},那麼您可以將'public customStyle = ['''行改爲'public customStyle:MapTypeStyle = [',這樣可以確保自定義樣式是正確的類型。這不是必須的,因爲它爲你工作。 – Adam

2

這是爲我工作[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> 

組件(節選)。 ☺:-p

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

請添加一些信息,更改內容,錯誤信息以及解決方法。只需發佈固定代碼不會幫助任何人導致同樣的錯誤將再次發生... –

+1

當我使用[styles] =「styles」並將json數據傳遞給變量'styles'時,它不起作用,那麼我已經通過在json中的數據直接。 – saddam