1+ import * as L from 'leaflet' ;
2+ import { GeocodingCallback , GeocodingResult , IGeocoder } from './api' ;
3+
4+ export interface AzureMapsOptions {
5+ apiKey : string ; // Azure Maps API Key
6+ serviceUrl ?: string ; // Optional: Base URL for the Azure Maps API
7+ }
8+
9+ /**
10+ * Azure Maps Geocoder class
11+ */
12+ export class AzureMapsGeocoder implements IGeocoder {
13+ private options : AzureMapsOptions ;
14+
15+ constructor ( options : AzureMapsOptions ) {
16+ this . options = {
17+ serviceUrl : 'https://atlas.microsoft.com/search' ,
18+ ...options ,
19+ } ;
20+
21+ if ( ! this . options . apiKey ) {
22+ throw new Error ( 'Azure Maps Geocoder requires an API key.' ) ;
23+ }
24+ }
25+
26+ geocode ( query : string , cb : GeocodingCallback , context ?: any ) : void {
27+ const params = {
28+ 'api-version' : '1.0' ,
29+ query : query ,
30+ 'subscription-key' : this . options . apiKey ,
31+ } ;
32+
33+ const url = `${ this . options . serviceUrl } /address/json?${ new URLSearchParams ( params ) . toString ( ) } ` ;
34+
35+ fetch ( url )
36+ . then ( response => response . json ( ) )
37+ . then ( data => {
38+ const results : GeocodingResult [ ] = [ ] ;
39+
40+ if ( data . results && data . results . length > 0 ) {
41+ for ( const result of data . results ) {
42+ console . log ( result ) ;
43+ results . push ( {
44+ name : result . address . freeformAddress ,
45+ bbox : L . latLngBounds (
46+ [ result . viewport . topLeftPoint . lat , result . viewport . topLeftPoint . lon ] ,
47+ [ result . viewport . btmRightPoint . lat , result . viewport . btmRightPoint . lon ]
48+ ) ,
49+ center : L . latLng ( result . position . lat , result . position . lon ) ,
50+ } ) ;
51+ }
52+ }
53+
54+ cb . call ( context , results ) ;
55+ } )
56+ . catch ( error => {
57+ console . error ( 'Geocoding error:' , error ) ;
58+ cb . call ( context , [ ] ) ;
59+ } ) ;
60+ }
61+
62+ reverse ( location : L . LatLngLiteral , scale : number , cb : GeocodingCallback , context ?: any ) : void {
63+ const params = {
64+ 'api-version' : '1.0' ,
65+ query : `${ location . lat } ,${ location . lng } ` ,
66+ 'subscription-key' : this . options . apiKey ,
67+ } ;
68+
69+ const url = `${ this . options . serviceUrl } /address/reverse/json?${ new URLSearchParams (
70+ params
71+ ) . toString ( ) } `;
72+
73+ fetch ( url )
74+ . then ( response => response . json ( ) )
75+ . then ( data => {
76+ const results : GeocodingResult [ ] = [ ] ;
77+
78+ if ( data . addresses && data . addresses . length > 0 ) {
79+ for ( const address of data . addresses ) {
80+ results . push ( {
81+ name : address . address . freeformAddress ,
82+ bbox : L . latLngBounds (
83+ [ address . viewport . topLeftPoint . lat , address . viewport . topLeftPoint . lon ] ,
84+ [ address . viewport . btmRightPoint . lat , address . viewport . btmRightPoint . lon ]
85+ ) ,
86+ center : L . latLng ( location . lat , location . lng ) ,
87+ } ) ;
88+ }
89+ }
90+
91+ cb . call ( context , results ) ;
92+ } )
93+ . catch ( error => {
94+ console . error ( 'Reverse geocoding error:' , error ) ;
95+ cb . call ( context , [ ] ) ;
96+ } ) ;
97+ }
98+ }
99+
100+ /**
101+ * [Class factory method](https://leafletjs.com/reference.html#class-class-factories) for {@link Azure}
102+ * @param options the options
103+ */
104+ export function azure ( options : AzureMapsOptions ) {
105+ return new AzureMapsGeocoder ( options ) ;
106+ }
0 commit comments