@Vlad和@Alexey的答案非常有帮助,但我想为任何想使用
sample library
Fuzzy Search with Services Module
以最小的调整必要的角度使用。
import { Component, ViewChild, ElementRef, AfterViewInit } from "@angular/core";
import * as atlas from 'azure-maps-control';
import * as atlasRest from 'azure-maps-rest'; // install npm azure-maps-rest
@Component({
selector: 'map',
templateUrl: './map.component.html',
styles: ['#map {height: 300px; width: 1110px;}']
//Remember to set the dimensions of the map container or layers may appear
//offset or behind the map.
})
export class MapComponent implements AfterViewInit {
@ViewChild('input') public input: ElementRef;
@ViewChild('mapContainer') public mapContainer: ElementRef;
private key: string = '<Your Azure Maps Key>';
public map: any;
public dataSource: any;
public popup: any;
public searchURL: any;
ngAfterViewInit(): void {
this.map = new atlas.Map(this.mapContainer.nativeElement, {
view: 'Auto',
authOptions: {
authType: atlas.AuthenticationType.subscriptionKey,
subscriptionKey: this.key
}
});
//Create a pipeline using the Azure Maps subscription key.
var pipeline = atlasRest.MapsURL.newPipeline(new atlasRest.SubscriptionKeyCredential(atlas.getSubscriptionKey()));
//Create an instance of the SearchURL client.
this.searchURL = new atlasRest.SearchURL(pipeline);
//Wait until the map resources are ready.
this.map.events.add('ready', () => {
// Add zoom control
this.map.controls.add(new atlas.control.ZoomControl(), {
position: 'bottom-left'
});
//Create a data source and add it to the map.
this.dataSource = new atlas.source.DataSource();
this.map.sources.add(this.dataSource);
//Add a layer for rendering the results as symbols.
var resultsLayer = new atlas.layer.SymbolLayer(this.dataSource);
this.map.layers.add(resultsLayer);
//Create a popup but leave it closed so we can update it and display it later.
this.popup = new atlas.Popup({
position: [0, 0],
pixelOffset: [0, -18]
});
//Add a click event to the results symbol layer.
//Remember to bind the event to 'this' or the this.popup and this.map
//lines of the symbolClicked function will return undefined!
this.map.events.add('click', resultsLayer, this.symbolClicked.bind(this));
});
}
public closePopup(): void {
this.popup.close();
}
public search(): void {
var query = this.input.nativeElement.value;
//Remove any previous results from the map.
this.dataSource.clear();
this.searchURL.searchFuzzy(atlasRest.Aborter.timeout(10000), query, {
radius: 100000,
view: 'Auto'
}).then(results => {
//Get the results in GeoJSON format and add it to the data source.
var data = results.geojson.getFeatures();
this.dataSource.add(data);
//Set the camera to the bounds of the results.
this.map.setCamera({
bounds: data.bbox,
padding: 40
});
});
}
public symbolClicked(e): void {
//Make sure the event occurred on a point feature.
if (e.shapes && e.shapes.length > 0 && e.shapes[0].getType() === 'Point') {
var properties = e.shapes[0].getProperties();
//Using the properties, create HTML to fill the popup with useful information.
var html = ['<div style="padding:10px;"><span style="font-size:14px;font-weight:bold;">'];
var addressInTitle = false;
if (properties.type === 'POI' && properties.poi && properties.poi.name) {
html.push(properties.poi.name);
} else if (properties.address && properties.address.freeformAddress) {
html.push(properties.address.freeformAddress);
addressInTitle = true;
}
html.push('</span><br/>');
if (!addressInTitle && properties.address && properties.address.freeformAddress) {
html.push(properties.address.freeformAddress, '<br/>');
}
html.push('<b>Type: </b>', properties.type, '<br/>');
if (properties.entityType) {
html.push('<b>Entity Type: </b>', properties.entityType, '<br/>');
}
if (properties.type === 'POI' && properties.poi) {
if (properties.poi.phone) {
html.push('<b>Phone: </b>', properties.poi.phone, '<br/>');
}
if (properties.poi.url) {
html.push('<b>URL: </b>', properties.poi.url, '<br/>');
}
if (properties.poi.classifications) {
html.push('<b>Classifications:</b><br/>');
for (var i = 0; i < properties.poi.classifications.length; i++) {
for (var j = 0; j < properties.poi.classifications[i].names.length; j++) {
html.push(' - ', properties.poi.classifications[i].names[j].name, '<br/>');
}
}
}
}
html.push('</div>');
//Set the popup options.
this.popup.setOptions({
//Update the content of the popup.
content: html.join(''),
//Update the position of the popup with the pins coordinate.
position: e.shapes[0].getCoordinates()
});
//Open the popup.
this.popup.open(this.map);
}
}
}
html将类似于:
<input type="search" #input>
<button (click)="search()">Search</button>
<div #mapContainer id="map"></div>
(注意:这个示例的目的是尽可能接近示例代码,以使比较更简单,但可以使用Angular和TypeScript约定进行改进)。