hifromkevin / Magic-Mirror-on-the-Wall

The UI for my Raspberry Pi magic mirror build.
2 stars 0 forks source link

Improve Time Complexity for API Call #1

Open hifromkevin opened 4 years ago

hifromkevin commented 4 years ago

In client/src/components/App.jsx, the helper getWeather() first uses an API call to get the user's IP. Then, it takes the user's IP, and inputs that value into the API call to get the user's geolocation coordinates. Finally, it takes the user's geolocation coordinates, and sends them to the weather API, which returns the weather and forecast for the coordinates provided.

Since this is being performed asynchronously, the API calls are made within each other, which can lead to vast loading times. Currently, the weather has the ability to have a loading icon, but ideally, the time complexity should be minimized.

wyattfry commented 4 years ago

i've been trying to use navigator.geolocation.getCurrentPosition() instead, but having trouble getting it to work, getting this:

PositionError
code: 2
message: "Network location provider at 'https://www.googleapis.com/' : No response received."

after googling, some said enable location services on macOS, i had. some said that all electron apps share the same google geocoding api key and thus is often hitting rate limit and advise using your own key, so i added my key in src/index.jsx like so:

process.env.GOOGLE_API_KEY='my_key';

didn't seem to make a difference

wyattfry commented 4 years ago

something like this might be a good way to eliminate a step, get lat & lng in 1 call: https://json.geoiplookup.io/ example response:

{
    "ip": "108.35.183.64",
    "isp": "MCI Communications Services, Inc. d\/b\/a Verizon Business",
    "org": "Verizon Online LLC",
    "hostname": "pool-108-35-183-64.nwrknj.fios.verizon.net",
    "latitude": 40.7357,
    "longitude": -74.1724,
    "postal_code": "07175",
    "city": "Newark",
    "country_code": "US",
    "country_name": "United States",
    "continent_code": "NA",
    "continent_name": "North America",
    "region": "New Jersey",
    "district": "Essex",
    "timezone_name": "America\/New_York",
    "connection_type": "Cable\/DSL",
    "asn_number": 701,
    "asn_org": "MCI Communications Services, Inc. d\/b\/a Verizon Business",
    "asn": "AS701 - MCI Communications Services, Inc. d\/b\/a Verizon Business",
    "currency_code": "USD",
    "currency_name": "US Dollar",
    "success": true,
    "premium": false,
    "cached": true
}