Azure Maps 未在 Angular 中加载
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]
}));
});
}
}
我正在尝试将 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]
}));
});
}
}