2018-09-08 19:22:46 +00:00
function DataManager ( pointSearch ) {
var $this = $ ( this ) ,
that = this ,
2021-08-24 23:10:16 +00:00
excludeRadiusMiles = 10 ;
includeRadiusMiles = 30 ;
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
var _locations = [ ] ;
2021-03-23 18:02:42 +00:00
this . locations = _locations ;
2018-09-08 19:22:46 +00:00
this . location = function ( woeid ) {
return _locations . find ( x => x . woeid === woeid ) ;
}
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
this . init = function ( searchString ) {
_locations [ 0 ] = new Location ( ) ;
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
$ ( _locations [ 0 ] )
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
. on ( 'refresh' , function ( ) { $this . trigger ( 'refresh' ) } )
2021-03-23 18:02:42 +00:00
. on ( 'ready' , function ( ) {
2018-09-08 19:22:46 +00:00
$this . trigger ( 'ready:main' ) ;
} )
. on ( 'init' , initLocations ) ;
2021-03-23 18:02:42 +00:00
_locations [ 0 ] . first = true ;
2018-09-08 19:22:46 +00:00
_locations [ 0 ] . init ( searchString ) ;
} ;
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
// kicks off after main location is returned.
// create the list of neighboring cities
function initLocations ( ) {
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
// find reporting stations
var observationData = _locations [ 0 ] . observations ( 0 ) ,
2021-08-24 23:10:16 +00:00
lat = observationData . latitude ,
lon = observationData . longitude ,
2018-09-08 19:22:46 +00:00
locList = [ ] ;
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
// begin the forcast pull
_locations [ 0 ] . initForecasts ( ) ;
2021-08-24 23:10:16 +00:00
// get a list of observation stations info //https://api.weather.com/v3/location/near?geocode=33.74,-84.39&product=observation&format=json&apiKey=yourApiKey
2021-08-25 01:05:55 +00:00
$ . getJSON ( 'https://api.weather.com/v3/location/near?geocode=' + lat + ',' + lon + '&product=observation&format=json&apiKey=' + api _key , function ( data ) {
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
var feature , geo , station , dist ;
2021-08-24 23:10:16 +00:00
for ( var i = 0 ; i < data . location . stationName . length || i <= 3 ; i ++ ) {
feature = data . location ;
latgeo = feature . latitude [ i ] ;
longeo = feature . longitude [ i ] ;
dist = feature . distanceMi [ i ] ;
2021-03-23 18:02:42 +00:00
2021-04-02 01:53:05 +00:00
if ( dist < includeRadiusMiles && dist > excludeRadiusMiles ) {
2021-08-24 23:10:16 +00:00
locList . push ( { lat : latgeo , long : longeo , distance : dist , stationUrl : feature . stationId [ i ] } ) ;
2018-09-08 19:22:46 +00:00
}
}
if ( locList . length === 0 ) {
$this . trigger ( 'allinit' ) ;
return
}
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
// sort list by distance
locList . sort ( function ( a , b ) {
return parseInt ( a . distance ) - parseInt ( b . distance ) ;
} ) ;
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
// set the station for location 0
_locations [ 0 ] . stationUrl = locList [ 0 ] . stationUrl
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
// create location objects, get inital pull
for ( var loc of locList ) {
loc . location = new Location ( ) ;
$ ( loc . location ) . on ( 'init' , onLocationInit ) ;
2021-08-24 23:10:16 +00:00
loc . location . init ( loc . lat + ',' + loc . long , loc . location . stationName ) ;
2018-09-08 19:22:46 +00:00
loc . location . stationUrl = loc . stationUrl ;
}
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
} ) ;
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
var initCount = 0 ;
function onLocationInit ( ) {
initCount ++ ;
if ( initCount === locList . length ) {
2021-03-23 18:02:42 +00:00
allLocationsInit ( ) ;
2018-09-08 19:22:46 +00:00
}
}
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
function allLocationsInit ( ) {
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
var location , cities = [ ] , city ;
// add locations removing any duplicate cities by name
for ( var loc of locList ) {
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
if ( _locations . filter ( e => e . city == loc . location . city ) . length === 0 ) {
_locations . push ( loc . location ) ;
2021-08-24 23:10:16 +00:00
//loc.location.initForecasts();
2021-03-23 18:02:42 +00:00
}
2018-09-08 19:22:46 +00:00
}
2021-03-23 18:02:42 +00:00
$this . trigger ( 'allinit' ) ;
2018-09-08 19:22:46 +00:00
}
2021-03-23 18:02:42 +00:00
2018-09-08 19:22:46 +00:00
}
}