質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

1回答

1039閲覧

今がどういう状況で次何をやればいいのか把握できないです。

abcdefghijk

総合スコア0

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2021/07/13 10:53

前提・実現したいこと

天気予報アプリを作りたい。

APIから次の情報を取得し、ボタンを押すと情報が画面に現れるようにしたいです。
場所(地名)
経度・緯度
天気
気温
気圧

発生している問題

今がどういう状況でつぎに何をやるべきなのかわからなくなりました。
geoloctorの情報はAPIに送れていますか?

Dart

// マテリアルコンポーネントをimportしてファイル内で利用可能に import 'package:flutter/material.dart'; import 'package:geolocator/geolocator.dart'; import 'dart:convert' ; import 'package:http/http.dart' as http; // import 'package:async/async.dart'; //main関数 プログラムが実行されたときに一番最初に実行される。 void main() { print("start"); // runApp(MyApp()); getWeatherFromAPI(); print("done!"); } var serverURL = "https://api.openweathermap.org/data/2.5/find?lat="; double lat =34 , lon = 116; Future<double> getWeatherFromAPI() async { Uri _url = Uri.parse(serverURL + lat.toString() + "&lon=" + lon.toString() + "&appid=d598129edcf409398b88c41347a317d6&cnt=1&lang=ja"); print(_url); final response = await http.get(_url); print(response.statusCode); if (response.statusCode == 200) { print(response.body); Map<String, dynamic> info = json.decode(utf8.decode(response.bodyBytes)); ApiData data = ApiData.fromJson(info); print(data.data.name); print(data.data.coord.lat); print(data.data.coord.lon); print(data.data.weather.main); print(data.data.main.temp); print(data.data.main.pressure); print("Weather done!"); return 0; } else { print("error:" + response.body); return -1; } } //StatelessWidgetを継承したMyAppクラス  class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } } //StatefulWidgetを継承したMyHomePageクラス class MyHomePage extends StatefulWidget { //コンストラクタ MyHomePage({Key? key, required this.title}) : super(key: key); //受け取った文字列を格納する変数 final String title; @override _MyHomePageState createState() => _MyHomePageState(); } //Stateを継承した_MyHomePageStateクラス class _MyHomePageState extends State<MyHomePage> { //カウンターの値を格納する変数 int _counter = 0; //カウンターの値を+1する関数 Future<Position> _determinePosition() async { bool serviceEnabled; LocationPermission permission; // Test if location services are enabled. serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { // Location services are not enabled don't continue // accessing the position and request users of the // App to enable the location services. return Future.error('Location services are disabled.'); } permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission == LocationPermission.denied) { // Permissions are denied, next time you could try // requesting permissions again (this is also where // Android's shouldShowRequestPermissionRationale // returned true. According to Android guidelines // your App should show an explanatory UI now. return Future.error('Location permissions are denied'); } } if (permission == LocationPermission.deniedForever) { // Permissions are denied forever, handle appropriately. return Future.error( 'Location permissions are permanently denied, we cannot request permissions.'); } // When we reach here, permissions are granted and we can // continue accessing the position of the device. Position pos = await Geolocator.getCurrentPosition(); print(pos.latitude); print(pos.accuracy) ; return pos; } //_MyHomePageStateのbuildメソッド @override Widget build(BuildContext context) { //ページはScaffoldで組む return Scaffold( //AppBar(画面上部)のレイアウト appBar: AppBar( //引数として受け取ったtitleをTextの要素として表示 title: Text(widget.title), ), //ページのbodyのレイアウト body: Center( //レイアウトWidgetのColumn、内部の要素を垂直に並べる。 child: Column( //内部の要素を真ん中に垂直に並べることを明示。 mainAxisAlignment: MainAxisAlignment.center, //Columnで並べる要素(Widget) children: <Widget>[ //テキスト Text( 'You have pushed the button this many times:', ), //テキスト Text( //変数_counterの値を表示 '$_counter', style: Theme.of(context).textTheme.headline4, ), ], ), ), //フローティングアクションボタン //右下のプラスボタンのWidget floatingActionButton: FloatingActionButton( //ボタンが押されたときに_incrementCounter関数を実行 onPressed: _determinePosition, tooltip: 'Increment', //アイコンをプラスボタンに設定 child: Icon(Icons.add), ), ); } } class ApiData { final String message; final int cod; final WeatherData data; ApiData(this.message, this.cod, this.data); ApiData.fromJson(Map<String, dynamic> json) : message = json['message'].toString(), cod = int.parse(json['cod']), data = WeatherData.fromJson(json['list'][0]); } class WeatherData{ final String id; final String name; final Coord coord; final MainWeatherData main; final int dt; final Weather weather; WeatherData(this.id, this.name, this.coord, this.main, this.dt, this.weather); WeatherData.fromJson(Map<String, dynamic> json) : id = json['id'].toString(), name = json['name'].toString(), coord = Coord.fromJson(json), main = MainWeatherData.fromJson(json), dt = json['dt'], weather = Weather.fromJson(json); } class MainWeatherData{ MainWeatherData(this.temp, this.feelsLike, this.tempMax, this.tempMin, this.pressure, this.humidity, this.seaLevel, this.grndLevel); MainWeatherData.fromJson(Map<String, dynamic> json) : temp = json['main']["temp"] - 273 ?? -9999, tempMin = json['main']["temp_min"] - 273 ?? -9999, tempMax = json['main']["temp_max"]- 273 ?? -9999, feelsLike = json['main']["feels_like"] - 273 ?? -9999, pressure = json['main']["pressure"] ?? -9999, humidity = json['main']["humidity"] ?? -9999, seaLevel = json['main']["sea_level"] ?? -9999, grndLevel = json['main']["grnd_level"] ?? -9999; final double temp; final double feelsLike; final double tempMin; final double tempMax; final int pressure; final int humidity; final int seaLevel; final int grndLevel; } class Weather { final int id; final String main; final String description; final String icon; Weather(this.main, this.description, this.icon, this.id); Weather.fromJson(Map<String, dynamic> json) : id = json['weather'][0]['id'], main = json['weather'][0]['main'], description = json['weather'][0]['description'], icon = json['weather'][0]['icon']; } class Coord{ final double lat; final double lon; Coord(this.lat, this.lon); Coord.fromJson(Map<String, dynamic> json) : lat = json['coord']['lat'], lon = json['coord']['lon']; }

試したこと

ソースコードを確認した

補足情報(FW/ツールのバージョンなど)

このコードの状況を整理していただきこのあとどうするべなのかを明確に教えていただきたいです。
よろしくお願いします。
次のような天気予報アプリを作りたいです。

APIから次の情報を取得し、ボタンを押すと情報が画面に現れる。
場所(地名)
経度・緯度
天気
気温
気圧

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

K_3578

2021/07/14 00:27

仕様を決めているのは質問者さんなのだから何をすべきか分からないと言うことがありえないと 思うのですが。 天気予報アプリを作りたいので仕様を決めてくれと言う話だと作業依頼になります。
guest

回答1

0

多分コピペしてそのままだと思いますが
determinePosition()はパーミッション(権限)が大丈夫か確認して大丈夫なら経度緯度を取ってきているところ、
double lat =34 , lon = 116;のところに double lat = pos.latitude, lon = pos.longitude;という感じで自分の位置情報入れれば取得できると思います。

もし今の段階でエラーがあるならそこを修正し、ないのであれば、
権限のところで、スマホが位置情報ついているとか、このアプリに現在地取得する権限を与えているのかとかいう文がdeterminePosition()にあると思うので、そこをpirnt('error');ではなくてスマホの位置情報設定画面だったり、権限を取得するために必要な画面を促すようにしてください。

投稿2021/07/14 01:56

endiv

総合スコア161

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問