Skip to content

Commit

Permalink
Use Array.map and Array.forEach
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed Dec 26, 2024
1 parent 8943dc4 commit 44628b1
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 304 deletions.
4 changes: 1 addition & 3 deletions src/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,7 @@ export class GeocoderControl extends EventedControl {
this._results = results;
L.DomUtil.removeClass(this._alts, 'leaflet-control-geocoder-alternatives-minimized');
L.DomUtil.addClass(this._container, 'leaflet-control-geocoder-options-open');
for (let i = 0; i < results.length; i++) {
this._alts.appendChild(this._createAlt(results[i], i));
}
this._results.forEach((result, i) => this._alts.appendChild(this._createAlt(result, i)));
} else {
L.DomUtil.addClass(this._container, 'leaflet-control-geocoder-options-error');
L.DomUtil.addClass(this._errorElement, 'leaflet-control-geocoder-error');
Expand Down
55 changes: 23 additions & 32 deletions src/geocoders/arcgis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { IGeocoder, GeocoderOptions, geocodingParams, GeocodingResult, reversePa

export interface ArcGisOptions extends GeocoderOptions {}



/**
* Implementation of the [ArcGIS geocoder](https://developers.arcgis.com/features/geocoding/)
*/
Expand Down Expand Up @@ -33,24 +31,18 @@ export class ArcGis implements IGeocoder {
this.options.serviceUrl + '/findAddressCandidates',
params
);
const results: GeocodingResult[] = [];
if (data.candidates && data.candidates.length) {
for (let i = 0; i <= data.candidates.length - 1; i++) {
const loc = data.candidates[i];
const latLng = L.latLng(loc.location.y, loc.location.x);
const latLngBounds = L.latLngBounds(
L.latLng(loc.extent.ymax, loc.extent.xmax),
L.latLng(loc.extent.ymin, loc.extent.xmin)
);
results[i] = {
name: loc.address,
bbox: latLngBounds,
center: latLng
};
}
}

return results;
return data.candidates.map((loc): GeocodingResult => {
const center = L.latLng(loc.location.y, loc.location.x);
const bbox = L.latLngBounds(
L.latLng(loc.extent.ymax, loc.extent.xmax),
L.latLng(loc.extent.ymin, loc.extent.xmin)
);
return {
name: loc.address,
bbox,
center
};
});
}

suggest(query: string): Promise<GeocodingResult[]> {
Expand All @@ -64,18 +56,18 @@ export class ArcGis implements IGeocoder {
f: 'json'
});
const data = await getJSON<any>(this.options.serviceUrl + '/reverseGeocode', params);
const result: GeocodingResult[] = [];
if (data && !data.error) {
const center = L.latLng(data.location.y, data.location.x);
const bbox = L.latLngBounds(center, center);
result.push({
name: data.address.Match_addr,
center: center,
bbox: bbox
});
if (!data || data.error) {
return [];
}

return result;
const center = L.latLng(data.location.y, data.location.x);
const bbox = L.latLngBounds(center, center);
return [
{
name: data.address.Match_addr,
center,
bbox
}
];
}
}

Expand Down Expand Up @@ -115,4 +107,3 @@ interface Candidate {
ymax: number;
};
}

48 changes: 20 additions & 28 deletions src/geocoders/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,16 @@ export class AzureMaps implements IGeocoder {
const url = this.options.serviceUrl + '/address/json';
const data = await getJSON<AzureMapsResponse>(url, params);

const results: GeocodingResult[] = [];
if (data.results && data.results.length > 0) {
for (const result of data.results) {
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)
});
}
}
return results;
return (data.results || []).map(
(result): GeocodingResult => ({
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)
})
);
}

/**
Expand All @@ -67,20 +63,16 @@ export class AzureMaps implements IGeocoder {
const url = this.options.serviceUrl + '/address/reverse/json';
const data = await getJSON<any>(url, params);

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)
});
}
}
return results;
return (data.addresses || []).map(
(address): GeocodingResult => ({
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)
})
);
}
}

Expand Down
31 changes: 10 additions & 21 deletions src/geocoders/bing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as L from 'leaflet';
import { getJSON } from '../util';
import { IGeocoder, GeocoderOptions, geocodingParams, GeocodingResult, reverseParams } from './api';
import { GeocoderOptions, geocodingParams, GeocodingResult, IGeocoder, reverseParams } from './api';

export interface BingOptions extends GeocoderOptions {}

Expand All @@ -26,19 +26,7 @@ export class Bing implements IGeocoder {
key: this.options.apiKey
});
const data = await getJSON<any>(this.options.serviceUrl, params);
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)
};
}
}
return results;
return this._parseResults(data);
}

async reverse(location: L.LatLngLiteral, scale: number): Promise<GeocodingResult[]> {
Expand All @@ -49,17 +37,18 @@ export class Bing implements IGeocoder {
this.options.serviceUrl + location.lat + ',' + location.lng,
params
);
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] = {
return this._parseResults(data);
}

private _parseResults(data) {
return data.resourceSets[0].resources.map((resource): GeocodingResult => {
const bbox = resource.bbox;
return {
name: resource.name,
bbox: L.latLngBounds([bbox[0], bbox[1]], [bbox[2], bbox[3]]),
center: L.latLng(resource.point.coordinates)
};
}
return results;
});
}
}

Expand Down
54 changes: 17 additions & 37 deletions src/geocoders/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,7 @@ export class Google implements IGeocoder {
address: query
});
const data = await getJSON<GoogleResponse>(this.options.serviceUrl, params);
const results: GeocodingResult[] = [];
if (data.results && data.results.length) {
for (let i = 0; i <= data.results.length - 1; i++) {
const loc = data.results[i];
const latLng = L.latLng(loc.geometry.location);
const latLngBounds = L.latLngBounds(
L.latLng(loc.geometry.viewport.northeast),
L.latLng(loc.geometry.viewport.southwest)
);
results[i] = {
name: loc.formatted_address,
bbox: latLngBounds,
center: latLng,
properties: loc.address_components
};
}
}

return results;
return this._parseResults(data);
}

async reverse(location: L.LatLngLiteral, scale: number): Promise<GeocodingResult[]> {
Expand All @@ -49,25 +31,23 @@ export class Google implements IGeocoder {
latlng: location.lat + ',' + location.lng
});
const data = await getJSON<any>(this.options.serviceUrl, params);
const results: GeocodingResult[] = [];
if (data.results && data.results.length) {
for (let i = 0; i <= data.results.length - 1; i++) {
const loc = data.results[i];
const center = L.latLng(loc.geometry.location);
const bbox = L.latLngBounds(
L.latLng(loc.geometry.viewport.northeast),
L.latLng(loc.geometry.viewport.southwest)
);
results[i] = {
name: loc.formatted_address,
bbox: bbox,
center: center,
properties: loc.address_components
};
}
}
return this._parseResults(data);
}

return results;
private _parseResults(data: GoogleResponse) {
return (data.results || [])?.map((loc): GeocodingResult => {
const center = L.latLng(loc.geometry.location);
const bbox = L.latLngBounds(
L.latLng(loc.geometry.viewport.northeast),
L.latLng(loc.geometry.viewport.southwest)
);
return {
name: loc.formatted_address,
bbox,
center,
properties: loc.address_components
};
});
}
}

Expand Down
81 changes: 35 additions & 46 deletions src/geocoders/here.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,20 @@ export class HERE implements IGeocoder {

async getJSON(url: string, params: any): Promise<GeocodingResult[]> {
const data = await getJSON<any>(url, params);
const results: GeocodingResult[] = [];

if (data.response.view && data.response.view.length) {
for (let i = 0; i <= data.response.view[0].result.length - 1; i++) {
const loc = data.response.view[0].result[i].location;
const center = L.latLng(loc.displayPosition.latitude, loc.displayPosition.longitude);
const bbox = L.latLngBounds(
L.latLng(loc.mapView.topLeft.latitude, loc.mapView.topLeft.longitude),
L.latLng(loc.mapView.bottomRight.latitude, loc.mapView.bottomRight.longitude)
);
results[i] = {
name: loc.address.label,
properties: loc.address,
bbox: bbox,
center: center
};
}
}
return results;
return (data.response.view?.[0]?.result || []).map((result): GeocodingResult => {
const loc = result.location;
const center = L.latLng(loc.displayPosition.latitude, loc.displayPosition.longitude);
const bbox = L.latLngBounds(
L.latLng(loc.mapView.topLeft.latitude, loc.mapView.topLeft.longitude),
L.latLng(loc.mapView.bottomRight.latitude, loc.mapView.bottomRight.longitude)
);
return {
name: loc.address.label,
properties: loc.address,
bbox,
center
};
});
}
}

Expand Down Expand Up @@ -131,34 +126,28 @@ export class HEREv2 implements IGeocoder {

async getJSON(url: string, params: any): Promise<GeocodingResult[]> {
const data = await getJSON<HEREv2Response>(url, params);
const results: GeocodingResult[] = [];

if (data.items && data.items.length) {
for (let i = 0; i <= data.items.length - 1; i++) {
const item = data.items[i];
const latLng = L.latLng(item.position.lat, item.position.lng);
let bbox: L.LatLngBounds;
if (item.mapView) {
bbox = L.latLngBounds(
L.latLng(item.mapView.south, item.mapView.west),
L.latLng(item.mapView.north, item.mapView.east)
);
} else {
// Using only position when not provided
bbox = L.latLngBounds(
L.latLng(item.position.lat, item.position.lng),
L.latLng(item.position.lat, item.position.lng)
);
}
results[i] = {
name: item.address.label,
properties: item.address,
bbox: bbox,
center: latLng
};
return (data.items || []).map((item): GeocodingResult => {
const center = L.latLng(item.position.lat, item.position.lng);
let bbox: L.LatLngBounds;
if (item.mapView) {
bbox = L.latLngBounds(
L.latLng(item.mapView.south, item.mapView.west),
L.latLng(item.mapView.north, item.mapView.east)
);
} else {
// Using only position when not provided
bbox = L.latLngBounds(
L.latLng(item.position.lat, item.position.lng),
L.latLng(item.position.lat, item.position.lng)
);
}
}
return results;
return {
name: item.address.label,
properties: item.address,
bbox,
center
};
});
}
}

Expand Down
Loading

0 comments on commit 44628b1

Please sign in to comment.