[英]Azure Maps not loading in Angular
我正在嘗試將 Azure Maps 添加到一個簡單的 Angular 應用程序中。
我收到以下錯誤。 知道為什么會發生這種情況以及如何解決嗎?
atlas.min.js:2509 Error: AuthenticationManager finished initializing, but no token is available
at atlas.min.js:2509
at ZoneDelegate.invoke (zone-evergreen.js:365)
at Object.onInvoke (core.js:40794)
at ZoneDelegate.invoke (zone-evergreen.js:364)
at Zone.run (zone-evergreen.js:124)
at zone-evergreen.js:851
at ZoneDelegate.invokeTask (zone-evergreen.js:400)
at Object.onInvokeTask (core.js:40772)
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Zone.runTask (zone-evergreen.js:168)
我添加它的步驟是:
1 - 安裝 Azure Maps
npm i azure-maps-control
2 - 為地圖創建一個組件
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { AddressSearchService } from '../address-search.service';
import { AddressSearchJson, AddressSearchResult } from '../address-search-result.model';
import { Map, AuthenticationType, HtmlMarker } from 'azure-maps-control';
@Component({
selector: 'app-address-search',
templateUrl: './address-search.component.html',
styleUrls: ['./address-search.component.css']
})
export class AddressSearchComponent implements OnInit {
// Azure Active Directory Authentication Client ID
// or Shared Key Authentication KEY
// get it from portal.azure.com
key: 'sharedKeyPrimaryKey';
map: any;
ngOnInit(): void {
// Initialize a map instance.
this.map = new Map('mapContainer', {
authOptions: {
authType: AuthenticationType.subscriptionKey,
subscriptionKey: this.key
}
});
// Wait until the map resources are ready.
this.map.events.add('ready', () => {
// Create a HTML marker and add it to the map.
this.map.markers.add(new HtmlMarker({
color: 'DodgerBlue',
text: '10',
position: [0, 0]
}));
});
}
constructor(private adressSearchService: AddressSearchService) { }
}
3 - 創建視圖
<div id="mapContainer"></div>
4 - 修改angular.json
"styles": [
"src/styles.css",
"node_modules/azure-maps-control/dist/atlas.min.css"
],
"scripts": [
"node_modules/azure-maps-control/dist/atlas.min.js"
]
5 - 確保我使用的是正確的 Azure Maps 密鑰(共享密鑰身份驗證 - 主密鑰)
嘿嘿,我相信我能幫到你! 我在讓 Azure 地圖與 Angular 一起工作時遇到了很多麻煩。 我對 Angular 還是很陌生,不知道為什么你的代碼會給出那個錯誤,但我知道它在你的組件中可以做些什么,因為其他一切都是正確的,這正是我所擁有的。 我復制了我用於加載下面地圖的組件代碼,希望對您有所幫助。
import { Component, OnInit } from '@angular/core';
import * as atlas from 'azure-maps-control';
@Component({
selector: 'app-azureMap',
templateUrl: './azureMap.component.html',
styleUrls: ['./azureMap.component.css']
})
export class AzureMapComponent implements OnInit {
// Azure Active Directory Authentication Client ID
// or Shared Key Authentication KEY
// get it from portal.azure.com
key: string = 'your key';
map: any;
constructor(
) {
}
ngOnInit() {
//Initialize a map instance.
this.map = new atlas.Map('mapContainer', {
center: [-122.33, 47.6],
zoom: 12,
view: 'Auto',
authOptions: {
authType: atlas.AuthenticationType.subscriptionKey,
subscriptionKey: this.key
}
});
//Wait until the map resources are ready.
this.map.events.add('ready', () => {
//Create a HTML marker and add it to the map.
this.map.markers.add(new atlas.HtmlMarker({
color: 'DodgerBlue',
text: '10',
position: [0, 0]
}));
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.