【问题标题】:add custom image to azure maps将自定义图像添加到天蓝色地图
【发布时间】:2021-03-18 23:30:27
【问题描述】:

我将this wrapper 用于天蓝色地图库。我目前正在实现symbol layer,并且使用其中一个默认标记效果很好,但我无法添加自己的标记。我尝试在 mapReady 函数中添加自定义标记,但响应始终未定义且未添加图像。

这是我的组件:

import {Component, Input, OnInit} from '@angular/core';
import * as atlas from 'azure-maps-control';

@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.scss']
})
export class MapComponent implements OnInit {
  private markerImagePath = 'assets/images/map-marker.png';

  public dataSource: atlas.source.DataSource;
  markerDescription: 'marker';

  public options: atlas.IconOptions = {
    image: this.markerDescription
  };

  points = [
    [52.52437, 13.41053],
    [51.50853, -0.12574]
  ];

  ngOnInit() { }

  mapReady(map: atlas.Map) {
    map.imageSprite.add(this.markerDescription, this.markerImagePath).then(r => {
      console.log(r);
      console.log(map.imageSprite.getImageIds());
      this.dataSource = new atlas.source.DataSource('markers');
      this.points.forEach(p => {
        const point = new atlas.Shape(new atlas.data.Point([p[1], p[0]]));
        this.dataSource.add([point]);
      });
    });
  }
}

这是我的html:

<section>
  <div class="row">
    <div class="col-12 map-dimensions my-2 mx-auto" azure-map zoom="2"
         [dataSources]="[dataSource]" (onReady)="mapReady($event.map)">
    <map-symbol-layer dataSourceId="markers"
                      [iconOptions]="options"></map-symbol-layer>
    </div>
  </div>
</section>

我怀疑我错误地访问了地图数据...你们有谁知道,我如何将自定义图像添加到 imageSprites 以便我将其用作符号层中的标记?

【问题讨论】:

    标签: azure azure-maps


    【解决方案1】:

    您的代码看起来不错。 imageSprite.add 返回 Promise&lt;void&gt;,因此您的 console.log 将始终记录 undefined。你的图标可能是问题吗?我一直在尝试类似的解决方案,并且一切正常:

    import { Component } from '@angular/core';
    import * as atlas from 'azure-maps-control';
    
    @Component({
      selector: 'app-root',
      template: '<azure-map zoom="2" [dataSources]="[dataSource]" (onReady)="mapReady($event.map)">' +
        '<map-symbol-layer [id]="blueLayerId" dataSourceId="blue" [iconOptions]="blueIconOptions"></map-symbol-layer>' +
        '</azure-map>',
      styleUrls: ['./app.component.scss']
    })
    export class AppComponent {
    
      public dataSource: atlas.source.DataSource;
    
      public blueLayerId: string = "blueLayer";
    
      public blueIconOptions: atlas.IconOptions = {
        image: 'campground'
      };
    
      mapReady(map: atlas.Map) {
        map.imageSprite.add('campground', 'assets/campground.png').then(() => {
          this.dataSource = new atlas.source.DataSource('blue');
          for (let i = 0; i < 10; i++) {
            const point = new atlas.Shape(new atlas.data.Point([i * 5, i * 5]));
            this.dataSource.add([point]);
          }
        });
      }
    }
    

    【讨论】:

    • 当我用它工作的实际值替换变量 this.markerDescription 时。很奇怪,但谢谢!
    • 我能再问你一个问题吗?
    • 我当然会创建一个新问题,这样您至少可以获得如此声誉。这是关于弹出功能。由于某种原因它不起作用,我真的找不到错误。
    • 当然,您也可以在 ng-azure-maps 的 Git 存储库上打开一个问题,以确保我不会错过它
    • 非常感谢!我已经问了这个问题,我已经搜索了几个小时的错误,但我找不到哪里出错了:stackoverflow.com/questions/66694648/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    • 2012-10-01
    相关资源
    最近更新 更多