Skip to content

Commit

Permalink
bing: replace jsonp with AJAX
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed Dec 15, 2024
1 parent 053e5a2 commit 09f6a4e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 64 deletions.
64 changes: 26 additions & 38 deletions src/geocoders/bing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as L from 'leaflet';
import { jsonp } from '../util';
import { getJSON } from '../util';
import {
IGeocoder,
GeocoderOptions,
Expand Down Expand Up @@ -28,38 +28,9 @@ export class Bing implements IGeocoder {
query: query,
key: this.options.apiKey
});
jsonp(
this.options.serviceUrl,
params,
data => {
const results: GeocodingResult[] = [];
if (data.resourceSets.length > 0) {
for (let i = data.resourceSets[0].resources.length - 1; i >= 0; i--) {
const resource = data.resourceSets[0].resources[i],
bbox = resource.bbox;
results[i] = {
name: resource.name,
bbox: L.latLngBounds([bbox[0], bbox[1]], [bbox[2], bbox[3]]),
center: L.latLng(resource.point.coordinates)
};
}
}
cb.call(context, results);
},
this,
'jsonp'
);
}

reverse(location: L.LatLngLiteral, scale: number, cb: GeocodingCallback, context?: any): void {
const params = reverseParams(this.options, {
key: this.options.apiKey
});
jsonp(
this.options.serviceUrl + location.lat + ',' + location.lng,
params,
data => {
const results: GeocodingResult[] = [];
getJSON(this.options.serviceUrl, params, data => {
const results: GeocodingResult[] = [];
if (data.resourceSets.length > 0) {
for (let i = data.resourceSets[0].resources.length - 1; i >= 0; i--) {
const resource = data.resourceSets[0].resources[i],
bbox = resource.bbox;
Expand All @@ -69,11 +40,28 @@ export class Bing implements IGeocoder {
center: L.latLng(resource.point.coordinates)
};
}
cb.call(context, results);
},
this,
'jsonp'
);
}
cb.call(context, results);
});
}

reverse(location: L.LatLngLiteral, scale: number, cb: GeocodingCallback, context?: any): void {
const params = reverseParams(this.options, {
key: this.options.apiKey
});
getJSON(this.options.serviceUrl + location.lat + ',' + location.lng, params, data => {
const results: GeocodingResult[] = [];
for (let i = data.resourceSets[0].resources.length - 1; i >= 0; i--) {
const resource = data.resourceSets[0].resources[i],
bbox = resource.bbox;
results[i] = {
name: resource.name,
bbox: L.latLngBounds([bbox[0], bbox[1]], [bbox[2], bbox[3]]),
center: L.latLng(resource.point.coordinates)
};
}
cb.call(context, results);
});
}
}

Expand Down
26 changes: 0 additions & 26 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import * as L from 'leaflet';
/**
* @internal
*/
let lastCallbackId = 0;

// Adapted from handlebars.js
// https://github.com/wycats/handlebars.js/
/**
Expand Down Expand Up @@ -54,26 +48,6 @@ export function htmlEscape(string?: string): string {
return string.replace(badChars, escapeChar);
}

/**
* @internal
*/
export function jsonp(
url: string,
params: Record<string, any>,
callback: (message: any) => void,
context: any,
jsonpParam?: string
) {
const callbackId = '_l_geocoder_' + lastCallbackId++;
params[jsonpParam || 'callback'] = callbackId;
(window as any)[callbackId] = L.Util.bind(callback, context);
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = url + getParamString(params);
script.id = callbackId;
document.getElementsByTagName('head')[0].appendChild(script);
}

/**
* @internal
*/
Expand Down

0 comments on commit 09f6a4e

Please sign in to comment.