繁体   English   中英

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM