gpt4 book ai didi

google-maps - 您如何从Google Maps Platform提取天气数据?

转载 作者:行者123 更新时间:2023-12-03 13:29:53 29 4
gpt4 key购买 nike

我有一个位置列表,我想知道是否有可能从Google Maps API获取天气数据。

最佳答案

当我必须在Go中执行此操作时,我做了如下操作:

package main

import (
"bufio"
"context"
"database/sql"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"sync"
"time"

"googlemaps.github.io/maps"

_ "github.com/lib/pq"
)

var db *sql.DB

var (
apiKey string
dsn string
locationsFile string
)

type HourlyForecast struct {
Type string
Properties Properties
}

type Properties struct {
Periods []Period
}

type Period struct {
Temperature int
}

func main() {
flag.StringVar(&apiKey, "api-key", "", "Google Maps API key")
flag.StringVar(&dsn, "dsn", "", "The database connection string")
flag.StringVar(&locationsFile, "locations", "locations.txt", "The locations file")
flag.Parse()

if apiKey == "" {
apiKey = os.Getenv("GOOGLE_MAPS_API_KEY")
}

if apiKey == "" {
log.Fatal("The --api-key flag or GOOGLE_MAPS_API_KEY env var must be set and non-empty")
}

apiKey = strings.TrimSpace(apiKey)

if dsn == "" {
dsn = os.Getenv("DSN")
}

var err error
db, err = sql.Open("postgres", dsn)
if err != nil {
log.Fatal(err)
}

for {
err := db.Ping()
if err != nil {
log.Println(err.Error())
time.Sleep(3 * time.Second)
continue
}
break
}

data, err := os.Open(locationsFile)
if err != nil {
log.Fatal(err)
}

var locations []string

scanner := bufio.NewScanner(data)
for scanner.Scan() {
locations = append(locations, scanner.Text())

}

var wg sync.WaitGroup

for _, location := range locations {
wg.Add(1)
location := location

go func() {
defer wg.Done()

mc, err := maps.NewClient(maps.WithAPIKey(apiKey))
if err != nil {
log.Fatal(err)
}

r := maps.FindPlaceFromTextRequest{
Input: location,
InputType: maps.FindPlaceFromTextInputTypeTextQuery,
}

response, err := mc.FindPlaceFromText(context.Background(), &r)
if err != nil {
log.Fatal(err)
}

pdr := maps.PlaceDetailsRequest{
PlaceID: response.Candidates[0].PlaceID,
}
log.Printf("retrieving geo coordinates for %s", location)
pdResponse, err := mc.PlaceDetails(context.Background(), &pdr)
if err != nil {
log.Fatal(err)
}

lat := pdResponse.Geometry.Location.Lat
lng := pdResponse.Geometry.Location.Lng
u := fmt.Sprintf("https://api.weather.gov/points/%.4f,%.4f/forecast/hourly", lat, lng)
log.Printf("retrieving weather data for %s (%.4f,%.4f)", location, lat, lng)

request, err := http.NewRequest("GET", u, nil)
if err != nil {
log.Fatal(err)
}
request.Header.Add("User-Agent", "Hightower Weather 1.0")
request.Header.Add("Accept", "application/geo+json")

weatherResponse, err := http.DefaultClient.Do(request)
if err != nil {
log.Fatal(err)
}

data, err := ioutil.ReadAll(weatherResponse.Body)
if err != nil {
log.Fatal(err)
}

weatherResponse.Body.Close()

var forecast HourlyForecast
if err := json.Unmarshal(data, &forecast); err != nil {
log.Fatal(err)
}

log.Printf("setting temperature for %s to %d", location, forecast.Properties.Periods[0].Temperature)
_, err = db.Exec(query, location, forecast.Properties.Periods[0].Temperature)
if err != nil {
log.Fatal(err)
}
}()
}

wg.Wait()
}

var query = `INSERT INTO weather (location, temperature)
VALUES ($1, $2)
ON CONFLICT (location)
DO UPDATE SET temperature = EXCLUDED.temperature;`

关于google-maps - 您如何从Google Maps Platform提取天气数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51532554/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com