From 09f6a4eb61a66845560a9e53985e5f338e5f6c59 Mon Sep 17 00:00:00 2001 From: Simon Legner Date: Sun, 15 Dec 2024 13:37:40 +0100 Subject: [PATCH] bing: replace jsonp with AJAX --- src/geocoders/bing.ts | 64 ++++++++++++++++++------------------------- src/util.ts | 26 ------------------ 2 files changed, 26 insertions(+), 64 deletions(-) diff --git a/src/geocoders/bing.ts b/src/geocoders/bing.ts index ebd8dcc..d037d72 100644 --- a/src/geocoders/bing.ts +++ b/src/geocoders/bing.ts @@ -1,5 +1,5 @@ import * as L from 'leaflet'; -import { jsonp } from '../util'; +import { getJSON } from '../util'; import { IGeocoder, GeocoderOptions, @@ -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; @@ -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); + }); } } diff --git a/src/util.ts b/src/util.ts index c99fe08..0bf149c 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,9 +1,3 @@ -import * as L from 'leaflet'; -/** - * @internal - */ -let lastCallbackId = 0; - // Adapted from handlebars.js // https://github.com/wycats/handlebars.js/ /** @@ -54,26 +48,6 @@ export function htmlEscape(string?: string): string { return string.replace(badChars, escapeChar); } -/** - * @internal - */ -export function jsonp( - url: string, - params: Record, - 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 */