Карта Bing V8 Прочитайте GeoJson Найдите Centroid и добавьте метку - PullRequest
0 голосов
/ 07 января 2019

Ниже приведен мой код GeoJson для чтения файла и отображения слоя данных на карте Bing. Я хотел бы найти центроид для каждого многоугольника / мультиполигона внутри функции GeoJson и добавить метку для каждого центроида.

Когда я пытаюсь найти центр с помощью shape [i] .getCenter (), он выдает ошибку «shape [i] .getCenter не является функцией».

Как найти центроид и добавить пин-код при чтении GeoJson из файла?

Я ссылаюсь на этот пример, чтобы найти центроид.

https://www.bing.com/api/maps/sdk/mapcontrol/isdk#getCentroid+JS

import { ViewContainerRef, Component, ElementRef, OnInit, ViewChild, NgZone } from '@angular/core';
import { Router } from '@angular/router';
import { MapService } from '../../services/map.service';
import { CookieService } from 'ngx-cookie';
import { Config } from '../../config/config';
import { CommonUtils } from './../../shared/CommonUtils';
import { fail, throws } from 'assert';
import { Toast, ToastsManager } from 'ng2-toastr/ng2-toastr';
import { OnDestroy } from '@angular/core/src/metadata/lifecycle_hooks';
import { DeviceDetectorService } from 'ngx-device-detector';

declare const Microsoft: any;

@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.scss']
})

export class MapComponent implements OnInit, OnDestroy {
  map: any;
  infobox: any;
  dataLayer: any;
  heatGradientData: any;

  @ViewChild('mapElement') someInput: ElementRef;
  myMap = document.querySelector('#myMap');

  constructor(private mapService: MapService,
    private router: Router, private config: Config,
    private utils: CommonUtils,
    private cookieService: CookieService,
    public toastr: ToastsManager, vRef: ViewContainerRef,
    private deviceService: DeviceDetectorService,
    private zone: NgZone) {
    this.toastr.setRootViewContainerRef(vRef);
  }

  ngOnInit() {

    if (document.readyState != 'complete') {
      document.onreadystatechange = () => {
        if (document.readyState === 'complete') {
          this.loadMapView(this.config.getConstants('mapTypeRoad'));
        } else {
          this.ngOnInit();
        }
      }
    } else {
      if (document.readyState === 'complete') {
        this.loadMapView(this.config.getConstants('mapTypeRoad'));
      }
    }

  }

  loadMapView(type: String) {

    var location = new Microsoft.Maps.Location(53.32, -110.29);

    this.map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
      credentials: this.config.get('BingMapKey'),
      center: location,
      mapTypeId: type == this.config.getConstants('mapTypeSatellite') ? Microsoft.Maps.MapTypeId.aerial : Microsoft.Maps.MapTypeId.road,
      zoom: 4,
      liteMode: true,
      enableClickableLogo: false,
      showLogo: false,
      showTermsLink: false,
      showMapTypeSelector: false,
      showTrafficButton: true,
      enableSearchLogo: false,
      showCopyright: false
    });

    //Store some metadata with the pushpin
    this.infobox = new Microsoft.Maps.Infobox(this.map.getCenter(), {
      visible: false
    });

    this.infobox.setMap(this.map);

    // Load the Spatial Math module.
    Microsoft.Maps.loadModule(['Microsoft.Maps.GeoJson'], function () { });

    // Create a layer to load pushpins to.
    // this.dataLayer = new Microsoft.Maps.EntityCollection();

    //Create a layer to add the GeoJSON data to.
    this.dataLayer = new Microsoft.Maps.Layer();
    this.map.layers.insert(this.dataLayer);

    //Add click event to the layer.
    //Microsoft.Maps.Events.addHandler(this.dataLayer, 'click', this.showInfobox);

    this.ReadGeoJson();

  }

  ReadGeoJson() {

    var self = this;

    //Load the GeoJSON module and read the GeoJSON feed.
    Microsoft.Maps.loadModule('Microsoft.Maps.GeoJson', function () {
      Microsoft.Maps.GeoJson.readFromUrl('../../assets/US_County_Boundaries.js', function (shapes) {
        //Add shapes to the layer.
        for (var i = 0, len = shapes.length; i < len; i++) {

          //Create custom Pushpin using an SVG string.
          // var pin = new Microsoft.Maps.Pushpin(shapes[i].getCenter(), {
          //   icon: '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30"><circle cx="15" cy="15" r="13" stroke="black" stroke-width="2" fill="{color}"/><text x="15" y="20" text-anchor="middle">{text}</text></svg>',
          //   color: 'yellow',
          //   text: '1',
          //   anchor: new Microsoft.Maps.Point(20, 20)
          // });

          // //Add the pushpin to the map.
          // self.map.entities.push(pin);
          Microsoft.Maps.Events.addHandler(shapes[i], 'click', showInfobox);
        }

        self.dataLayer.add(shapes);

        function showInfobox(e) {
          var shape = e.target;
          var loc = e.location; //Default to the location of the mouse event to show the infobox.

          //If the shape is a pushpin, use it's location to display the infobox.
          if (shape instanceof Microsoft.Maps.Pushpin) {
            loc = shape.getLocation();
          }
          //Display the infoboc
          self.infobox.setOptions({ location: loc, title: shape.metadata.NAME, visible: true });
        }
      });
    });
  }

  ngOnDestroy() {

  }

}

1 Ответ

0 голосов
/ 07 января 2019

Метод centroid находится в модуле SpatialMath, поэтому сначала вам нужно загрузить его, как вы это сделали с GeoJson:

Microsoft.Maps.loadModule(['Microsoft.Maps.GeoJson', 'Microsoft.Maps.SpatialMath'], function () {...});

Затем в функции обратного вызова вы можете использовать Microsoft.Maps.SpatialMath.Geometry.centroid(shapes[i]), чтобы получить местоположение центроида.

...