Skip to content

Commit 0951831

Browse files
committed
azure: update to leaflet-control-geocoder 3.0.0
1 parent bf04a88 commit 0951831

File tree

1 file changed

+50
-73
lines changed

1 file changed

+50
-73
lines changed

src/geocoders/azure.ts

Lines changed: 50 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,76 @@
11
import * as L from 'leaflet';
2-
import { GeocodingCallback, GeocodingResult, IGeocoder } from './api';
2+
import { getJSON } from '../util';
3+
import { GeocodingResult, IGeocoder } from './api';
34

45
export interface AzureMapsOptions {
56
apiKey: string; // Azure Maps API Key
6-
serviceUrl?: string; // Optional: Base URL for the Azure Maps API
7+
serviceUrl: string; // Optional: Base URL for the Azure Maps API
78
}
89

910
/**
1011
* Azure Maps Geocoder class
1112
*/
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-
};
13+
export class AzureMaps implements IGeocoder {
14+
private options: AzureMapsOptions = {
15+
apiKey: '',
16+
serviceUrl: 'https://atlas.microsoft.com/search'
17+
};
2018

19+
constructor(options: Partial<AzureMapsOptions>) {
20+
L.Util.setOptions(this, options);
2121
if (!this.options.apiKey) {
2222
throw new Error('Azure Maps Geocoder requires an API key.');
2323
}
2424
}
2525

26-
geocode(query: string, cb: GeocodingCallback, context?: any): void {
26+
async geocode(query: string): Promise<GeocodingResult[]> {
2727
const params = {
2828
'api-version': '1.0',
29-
query: query,
30-
'subscription-key': this.options.apiKey,
29+
query,
30+
'subscription-key': this.options.apiKey
3131
};
32+
const url = this.options.serviceUrl + '/address/json';
33+
const data = await getJSON<any>(url, params);
3234

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-
});
35+
const results: GeocodingResult[] = [];
36+
if (data.results && data.results.length > 0) {
37+
for (const result of data.results) {
38+
results.push({
39+
name: result.address.freeformAddress,
40+
bbox: L.latLngBounds(
41+
[result.viewport.topLeftPoint.lat, result.viewport.topLeftPoint.lon],
42+
[result.viewport.btmRightPoint.lat, result.viewport.btmRightPoint.lon]
43+
),
44+
center: L.latLng(result.position.lat, result.position.lon)
45+
});
46+
}
47+
}
48+
return results;
6049
}
6150

62-
reverse(location: L.LatLngLiteral, scale: number, cb: GeocodingCallback, context?: any): void {
51+
async reverse(location: L.LatLngLiteral, scale: number): Promise<GeocodingResult[]> {
6352
const params = {
6453
'api-version': '1.0',
65-
query: `${location.lat},${location.lng}`,
66-
'subscription-key': this.options.apiKey,
54+
query: location.lat + ',' + location.lng,
55+
'subscription-key': this.options.apiKey
6756
};
57+
const url = this.options.serviceUrl + '/address/reverse/json';
58+
const data = await getJSON<any>(url, params);
6859

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-
});
60+
const results: GeocodingResult[] = [];
61+
if (data.addresses && data.addresses.length > 0) {
62+
for (const address of data.addresses) {
63+
results.push({
64+
name: address.address.freeformAddress,
65+
bbox: L.latLngBounds(
66+
[address.viewport.topLeftPoint.lat, address.viewport.topLeftPoint.lon],
67+
[address.viewport.btmRightPoint.lat, address.viewport.btmRightPoint.lon]
68+
),
69+
center: L.latLng(location.lat, location.lng)
70+
});
71+
}
72+
}
73+
return results;
9774
}
9875
}
9976

@@ -102,5 +79,5 @@ export class AzureMapsGeocoder implements IGeocoder {
10279
* @param options the options
10380
*/
10481
export function azure(options: AzureMapsOptions) {
105-
return new AzureMapsGeocoder(options);
106-
}
82+
return new AzureMaps(options);
83+
}

0 commit comments

Comments
 (0)