Skip to content

Commit

Permalink
Implement Azure Maps Geocoder (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamka427 authored Dec 21, 2024
1 parent 51ed3ea commit bf04a88
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/geocoders/azure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import * as L from 'leaflet';
import { GeocodingCallback, GeocodingResult, IGeocoder } from './api';

export interface AzureMapsOptions {
apiKey: string; // Azure Maps API Key
serviceUrl?: string; // Optional: Base URL for the Azure Maps API
}

/**
* Azure Maps Geocoder class
*/
export class AzureMapsGeocoder implements IGeocoder {
private options: AzureMapsOptions;

constructor(options: AzureMapsOptions) {
this.options = {
serviceUrl: 'https://atlas.microsoft.com/search',
...options,
};

if (!this.options.apiKey) {
throw new Error('Azure Maps Geocoder requires an API key.');
}
}

geocode(query: string, cb: GeocodingCallback, context?: any): void {
const params = {
'api-version': '1.0',
query: query,
'subscription-key': this.options.apiKey,
};

const url = `${this.options.serviceUrl}/address/json?${new URLSearchParams(params).toString()}`;

fetch(url)
.then(response => response.json())
.then(data => {
const results: GeocodingResult[] = [];

if (data.results && data.results.length > 0) {
for (const result of data.results) {
console.log(result);
results.push({
name: result.address.freeformAddress,
bbox: L.latLngBounds(
[result.viewport.topLeftPoint.lat, result.viewport.topLeftPoint.lon],
[result.viewport.btmRightPoint.lat, result.viewport.btmRightPoint.lon]
),
center: L.latLng(result.position.lat, result.position.lon),
});
}
}

cb.call(context, results);
})
.catch(error => {
console.error('Geocoding error:', error);
cb.call(context, []);
});
}

reverse(location: L.LatLngLiteral, scale: number, cb: GeocodingCallback, context?: any): void {
const params = {
'api-version': '1.0',
query: `${location.lat},${location.lng}`,
'subscription-key': this.options.apiKey,
};

const url = `${this.options.serviceUrl}/address/reverse/json?${new URLSearchParams(
params
).toString()}`;

fetch(url)
.then(response => response.json())
.then(data => {
const results: GeocodingResult[] = [];

if (data.addresses && data.addresses.length > 0) {
for (const address of data.addresses) {
results.push({
name: address.address.freeformAddress,
bbox: L.latLngBounds(
[address.viewport.topLeftPoint.lat, address.viewport.topLeftPoint.lon],
[address.viewport.btmRightPoint.lat, address.viewport.btmRightPoint.lon]
),
center: L.latLng(location.lat, location.lng),
});
}
}

cb.call(context, results);
})
.catch(error => {
console.error('Reverse geocoding error:', error);
cb.call(context, []);
});
}
}

/**
* [Class factory method](https://leafletjs.com/reference.html#class-class-factories) for {@link Azure}
* @param options the options
*/
export function azure(options: AzureMapsOptions) {
return new AzureMapsGeocoder(options);
}
1 change: 1 addition & 0 deletions src/geocoders/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './api';
export * from './arcgis';
export * from './bing';
export * from './azure';
export * from './google';
export * from './here';
export * from './latlng';
Expand Down

0 comments on commit bf04a88

Please sign in to comment.