11import type { Faker } from '../..' ;
2+ import { deprecated } from '../../internal/deprecated' ;
23
34/**
45 * Module to generate addresses and locations.
@@ -368,6 +369,27 @@ export class LocationModule {
368369 ) ;
369370 }
370371
372+ /**
373+ * Generates a random GPS coordinate within the specified radius from the given coordinate.
374+ *
375+ * @param options The options for generating a GPS coordinate.
376+ * @param options.origin The original coordinate to get a new coordinate close to.
377+ * If no coordinate is given, a random one will be chosen.
378+ * @param options.radius The maximum distance from the given coordinate to the new coordinate. Defaults to `10`.
379+ * @param options.isMetric If `true` assume the radius to be in kilometers. If `false` for miles. Defaults to `false`.
380+ *
381+ * @example
382+ * faker.location.nearbyGPSCoordinate() // [ 33.8475, -170.5953 ]
383+ * faker.location.nearbyGPSCoordinate({ origin: [33, -170] }) // [ 33.0165, -170.0636 ]
384+ * faker.location.nearbyGPSCoordinate({ origin: [33, -170], radius: 1000, isMetric: true }) // [ 37.9163, -179.2408 ]
385+ *
386+ * @since 8.0.0
387+ */
388+ nearbyGPSCoordinate ( options ?: {
389+ origin ?: [ latitude : number , longitude : number ] ;
390+ radius ?: number ;
391+ isMetric ?: boolean ;
392+ } ) : [ latitude : number , longitude : number ] ;
371393 /**
372394 * Generates a random GPS coordinate within the specified radius from the given coordinate.
373395 *
@@ -382,14 +404,74 @@ export class LocationModule {
382404 * faker.location.nearbyGPSCoordinate([33, -170], 1000, true) // [ 37.9163, -179.2408 ]
383405 *
384406 * @since 8.0.0
407+ *
408+ * @deprecated Use `faker.location.nearbyGPSCoordinate({ origin, radius, isMetric })` instead.
385409 */
386410 nearbyGPSCoordinate (
387411 coordinate ?: [ latitude : number , longitude : number ] ,
388- radius : number = 10 ,
389- isMetric : boolean = false
412+ radius ?: number ,
413+ isMetric ?: boolean
414+ ) : [ latitude : number , longitude : number ] ;
415+ /**
416+ * Generates a random GPS coordinate within the specified radius from the given coordinate.
417+ *
418+ * @param options The options for generating a GPS coordinate.
419+ * @param options.origin The original coordinate to get a new coordinate close to.
420+ * If no coordinate is given, a random one will be chosen.
421+ * @param options.radius The maximum distance from the given coordinate to the new coordinate. Defaults to `10`.
422+ * @param options.isMetric If `true` assume the radius to be in kilometers. If `false` for miles. Defaults to `false`.
423+ * @param legacyRadius Deprecated, use `options.radius` instead.
424+ * @param legacyIsMetric Deprecated, use `options.isMetric` instead.
425+ *
426+ * @example
427+ * faker.location.nearbyGPSCoordinate() // [ 33.8475, -170.5953 ]
428+ * faker.location.nearbyGPSCoordinate({ origin: [33, -170] }) // [ 33.0165, -170.0636 ]
429+ * faker.location.nearbyGPSCoordinate({ origin: [33, -170], radius: 1000, isMetric: true }) // [ 37.9163, -179.2408 ]
430+ *
431+ * @since 8.0.0
432+ */
433+ nearbyGPSCoordinate (
434+ options ?:
435+ | [ latitude : number , longitude : number ]
436+ | {
437+ origin ?: [ latitude : number , longitude : number ] ;
438+ radius ?: number ;
439+ isMetric ?: boolean ;
440+ } ,
441+ legacyRadius ?: number ,
442+ legacyIsMetric ?: boolean
443+ ) : [ latitude : number , longitude : number ] ;
444+ nearbyGPSCoordinate (
445+ options :
446+ | [ latitude : number , longitude : number ]
447+ | {
448+ origin ?: [ latitude : number , longitude : number ] ;
449+ radius ?: number ;
450+ isMetric ?: boolean ;
451+ } = { } ,
452+ legacyRadius : number = 10 ,
453+ legacyIsMetric : boolean = false
390454 ) : [ latitude : number , longitude : number ] {
391- // If there is no coordinate, the best we can do is return a random GPS coordinate.
392- if ( coordinate === undefined ) {
455+ if ( Array . isArray ( options ) ) {
456+ deprecated ( {
457+ deprecated :
458+ 'faker.location.nearbyGPSCoordinate(coordinate, radius, isMetric)' ,
459+ proposed :
460+ 'faker.location.nearbyGPSCoordinate({ origin, radius, isMetric })' ,
461+ since : '8.0' ,
462+ until : '9.0' ,
463+ } ) ;
464+ options = { origin : options } ;
465+ }
466+
467+ const {
468+ origin,
469+ radius = legacyRadius ,
470+ isMetric = legacyIsMetric ,
471+ } = options ;
472+
473+ // If there is no origin, the best we can do is return a random GPS coordinate.
474+ if ( origin == null ) {
393475 return [ this . latitude ( ) , this . longitude ( ) ] ;
394476 }
395477
@@ -414,22 +496,22 @@ export class LocationModule {
414496
415497 const distanceInDegree = distanceInKm / kmPerDegree ; // in °
416498
417- const newCoordinate : [ latitude : number , longitude : number ] = [
418- coordinate [ 0 ] + Math . sin ( angleRadians ) * distanceInDegree ,
419- coordinate [ 1 ] + Math . cos ( angleRadians ) * distanceInDegree ,
499+ const coordinate : [ latitude : number , longitude : number ] = [
500+ origin [ 0 ] + Math . sin ( angleRadians ) * distanceInDegree ,
501+ origin [ 1 ] + Math . cos ( angleRadians ) * distanceInDegree ,
420502 ] ;
421503
422504 // Box latitude [-90°, 90°]
423- newCoordinate [ 0 ] = newCoordinate [ 0 ] % 180 ;
424- if ( newCoordinate [ 0 ] < - 90 || newCoordinate [ 0 ] > 90 ) {
425- newCoordinate [ 0 ] = Math . sign ( newCoordinate [ 0 ] ) * 180 - newCoordinate [ 0 ] ;
426- newCoordinate [ 1 ] += 180 ;
505+ coordinate [ 0 ] = coordinate [ 0 ] % 180 ;
506+ if ( coordinate [ 0 ] < - 90 || coordinate [ 0 ] > 90 ) {
507+ coordinate [ 0 ] = Math . sign ( coordinate [ 0 ] ) * 180 - coordinate [ 0 ] ;
508+ coordinate [ 1 ] += 180 ;
427509 }
428510
429511 // Box longitude [-180°, 180°]
430- newCoordinate [ 1 ] = ( ( ( newCoordinate [ 1 ] % 360 ) + 540 ) % 360 ) - 180 ;
512+ coordinate [ 1 ] = ( ( ( coordinate [ 1 ] % 360 ) + 540 ) % 360 ) - 180 ;
431513
432- return [ newCoordinate [ 0 ] , newCoordinate [ 1 ] ] ;
514+ return [ coordinate [ 0 ] , coordinate [ 1 ] ] ;
433515 }
434516
435517 /**
0 commit comments