简体   繁体   English

如何在Angular组件中向Leaflet地图添加标记

[英]How to add marker to leaflet map in Angular component

I am using leaflet on an Angular app and I am showing some markers located in the coordinates that I retrieve from the back-end. 我在Angular应用程序上使用传单,我正在显示一些位于我从后端检索的坐标中的标记。

With the code at the end of the post, markers are shown but they don´t keep the position on differents zoom level, as you can see on the following images. 使用帖子末尾的代码,会显示标记,但它们不会将位置保持在不同的缩放级别,如下图所示。

在此输入图像描述 在此输入图像描述 在此输入图像描述

The markers should be over the red points, but they are in another location, and it seems that they don´t keep the position when I do zoom in or out. 标记应该在红点之上,但是它们位于另一个位置,并且当我放大或缩小时它们似乎不保持位置。 It looks like that the points are on a different layer with a different reference system. 看起来这些点位于具有不同参考系统的不同层上。

I have tried to keep the mMarker.prototype.options.icon on a variable and use addTo(map), but it throws an error that says "addTo" is not a function. 我试图将mMarker.prototype.options.icon保留在变量上并使用addTo(map),但它会抛出一个错误,指出“addTo”不是一个函数。

What can I do? 我能做什么? Thanks 谢谢

import { Component, OnInit } from '@angular/core';
import { MapService } from '../../services/map.service';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Map } from '../../models/map';

import { Icon, icon, Marker, marker } from 'leaflet';

declare let L;
var map;

@Component({
  selector: 'map-component',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {

  private defaultIcon: Icon = icon({
    iconUrl: 'assets/leaflet/marker-icon.png',
    shadowUrl: 'assets/leaflet/marker-shadow.png'
  });

  public coordinates: [number];
  constructor(
    private _mapService: MapService, private _router: Router
  ) {

  }

  ngOnInit() {

    Marker.prototype.options.icon = this.defaultIcon;
    map = L.map('map').setView([0, 0], 2); L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    this.getLocations();
  }

  getLocations() {

    var geoJsonMulti = {
      "type": "GeometryCollection",
      "geometries": [

      ]
    }

    this._mapService.getCoordinates().subscribe(
      response => {
        if (!response) {
          this._router.navigate(['/']);
        } else {
          var layer = response.cityFeatures.cities;
          console.log(layer.cities)
          layer.forEach(layer => {
            geoJsonMulti.geometries.push(layer);
          })
          console.log(geoJsonMulti)
        }
        var myStyle = {
          "color": "#ff7800",
          "weight": 5,
          "opacity": 0.65
        };
        L.geoJSON(geoJsonMulti, {
          style: myStyle
        }).addTo(map);
      },

      error => {
        let errorMessage = <any>error;
        if (errorMessage !== null) {
          let body = JSON.parse(error._body);
        }
      }
    );
  }
}

You have to specify the icon size and the anchor position, something like that : 您必须指定图标大小和锚位置,如下所示:

  private defaultIcon: Icon = icon({
    iconUrl: 'assets/leaflet/marker-icon.png',
    shadowUrl: 'assets/leaflet/marker-shadow.png',
    iconSize: [41, 51], // => random values you have to choose right ones for your case
    iconAnchor: [20, 51] // => random values too
  });

See here : https://leafletjs.com/examples/custom-icons/ 请看这里: https//leafletjs.com/examples/custom-icons/

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

相关问题 如何在角度组件中删除Leaflet地图的标记 - How to remove marker to Leaflet map in angular component 从另一个角度组件在传单地图上添加标记 - Add marker on Leaflet map from another angular component Angular 2 / Leaflet map,如何从标记弹出窗口链接到组件? ... routerLink? - Angular 2 / leaflet map, How to link to a component from marker popup ? … routerLink? 如何在 Leaflet 标记的弹出窗口中生成 Angular 4 组件? - How to spawn Angular 4 component inside a Leaflet marker's popup? 如何在Angular和ngx-leaflet编写的传单地图中添加图例 - How to add legend to leaflet map written by Angular and ngx-leaflet Angular - leaflet 标记事件打开模态组件 - Angular - leaflet marker event to open modal component 如何在Google Map Angle2中添加标记? - how to add marker to google map angular2? 将组件添加到 ngx-leaflet 标记弹出窗口 - Add component to ngx-leaflet marker popup 如何使用有角度的传单打开街道地图检测标记在圆圈内外 - How to detect marker goes inside and outside of the circle using leaflet open street map in angular 如何使用Angular 2在传单地图上添加搜索控件? - How to add search control on leaflet map using Angular 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM