Tải bản đầy đủ (.docx) (67 trang)

xây dựng phần mềm dự báo thời tiết

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (912.9 KB, 67 trang )

Xây dựng phần mềm dự báo thời tiết
Tui muốn tổng hợp các bài tập về Google Map, về Đa tiến trình, về JSon,
Webservice…. để xây dựng phần mềm dự báo thời tiết đơn giản như hình dưới đây:

– Phần mềm sẽ có 4 chức năng
như mô tả ở trên, để làm được bài này thì các bạn cần phải có các kiến thức sau:
Đồng thời nghiên cứu thêm API Open Weather Map(Hỗ trợ xem thời tiết hầu hết
mọi nơi trên thế giới, được đánh giá là một trong những API cũng cấp webservice về
dự báo thời tiết tốt nhất hiện nay)
Cập nhật ngày 13/12/2015

1


Để sử dụng được API này bạn cần đăng nhập để lấy appid, chi tiết đọc
tại : />Khi đăng ký và đăng nhập thành công, bạn sẽ có API key bên dưới:

/>lat=10.778182&lon=106.665504&cnt=1&appid=be8d3e323de722ff78208a7dbb2dc
d6f
Ví dụ 0: Xem thời tiết ở Thành Phố Hồ Chí Minh:
chí
minh&appid=be8d3e323de722ff78208a7dbb2dcd6f
Ví dụ 1: Xem thời tiết ở Đà Lạt
lạ
t&appid=be8d3e323de722ff78208a7dbb2dcd6f
Ví dụ 2: Xem thời tiết ở Đà Lạt sau 15 ngày nữa thì như thế nào:
lạt&cnt=1
5&appid=be8d3e323de722ff78208a7dbb2dcd6f


Ví dụ 3: Xem thời tiết ở 1 Kinh độ và Vĩ độ bất kỳ (ứng dụng hiển thị trên Google


Map)
/>lat=35&lon=139&cnt=10&mode=json&appid=be8d3e323de722ff78208a7dbb2dcd6
f
Chi tiết hướng dẫn sử dụng: />Các bạn tranh thủ ôn lại các bài trên sau đó Tui sẽ trình bày chi tiết từng bước cách
thực hiện dự án này.
Trước khi bắt tay vào lập trình bài này thì ta xem sơ qua cấu trúc JSON khi kiểm tra
thời tiết của một địa điểm nào đó, ví dụ ở Thành Phố Hồ Chí Minh:
chí
minh&appid=be8d3e323de722ff78208a7dbb2dcd6f
hay dùng kinh độ vĩ độ (để áp dụng cho chức năng đầu tiên là xem thời tiết tại địa
điểm hiện tại, theo địa chỉ bất kỳ, hay xem trên Google Map):
/>lat=10.778182&lon=106.665504&appid=be8d3e323de722ff78208a7dbb2dcd6f
Ta được kết quả tương tự như sau:


Các source code bạn tự nối đuôi appid mà Tui đã chỉ ở trên là có thể chạy tốt.
End update 13/12/2015
—————————————–
Việc phân tích cấu trúc JSON để viết Java class các bạn đã được học kỹ ở các bài
trước rồi, nên bài này Tui không nói nữa mà Tui chỉ show Mô hình Java class đã viết
ra như sau (Bạn tự viết):

– Ta sẽ dùng GSon để chuyển JSON qua Java class, từ đó dễ dàng cho việc sử dụng
lấy thông tin dự báo thời tiết.
– Còn dưới đây là cấu trúc JSON cho trường hợp dự báo các ngày khác (Daily):
Ví dụ:
/>lat=10.778182&lon=106.665504&cnt=10


Hay:

chí minh&cnt=10
Thì ta có kết quả với cấu trúc JSON khá khác cho với trường hợp trên như sau:

–> Bạn tự phân tích cấu trúc ở trên để viết Java Class cho trường hợp Daily (chức
năng số 3).
Trong Project này Tui sẽ thực hiện 3 chức năng (thời tiết địa điểm hiện tại của thiết
bị, xem thời tiết theo địa điểm nhập bất kỳ và xem thời tiết trên Google Map), còn
chức năng dự báo ngày kế tiếp các bạn tiếp tục thực hiện.
Tui có cấu trúc tập tin, class của dự án như sau:


– Tui chụp màn hình sử
dụng phần mềm dự báo thời tiết như sau:
1) Từ màn hình chính, nếu bấm vào “Thời tiết địa điểm hiện tại của thiết bị”:

Chương trình sẽ hiển thị thông
báo chi tiết dự báo thời tiết như sau:


Ta thấy chi tiết của địa điểm hiện
tại của thiết bị (vị trị hiện tại tương đối trong phạm vi thiết bị), ở trên ta thấy nhiệt
độ, bầu trời bằng hình có nắng, có mây, có mưa…, tốc độ gió, áp suất…
2) Từ màn hình chính, nếu bấm vào “Xem thời tiết theo địa điểm nhập bất kỳ”:

Chương trình sẽ hiển thị màn hình
cho phép nhập địa chỉ bất kỳ, hoặc chọn các tỉnh thành có sẵn của Việt Nam (muốn
bất kỳ địa điểm nào khác ở Việt Nam hay trên thế giới thì tự gõ địa chỉ vào), màn
hình chọn địa điểm:



Nếu ta chọn hoặc nhập địa điểm
bất kỳ, ví dụ như Hồ Chí Minh, ta có kết quả sau:


Ở hình trên ta thấy đấy, Hồ Chí
Minh tuy có mưa nhưng nhiệt độ vẫn nóng là 34.2 độ C. Đúng là tại thời điểm Tui
post hình này lên là Sài gòn đang mưa và vẫn nóng le lưỡi.
3) Tại màn hình chính, chọn “Xem thời tiết trên Google Map”, chương trình sẽ tự
động hiển thị thời tiết tại vị trí hiện tại trên bản đồ, và cho phép bạn nhấn chọn bất
kỳ địa điểm nào đó trên bản đồ để xem thời tiết. Đây là chức năng rất thú vị và
coding hơi phức tạp.

Ta có kết quả:


Nhớ là bạn có thể nhấn chọn vị trí
khác để xem thời tiết, ví dụ Tui chọn Nguyễn văn Trỗi:


Nếu làm tốt bài này bạn có thể áp
dụng một cách uyển chuyển vào các dự án liên quan tới Du lịch, xem thời tiết….
Sau đây là Coding chi tiết cho từng phần, tui sẽ giải thích những đặc tính mới trong
Android Studio, còn những cái này tương tự như Eclipse thì thôi (Tui chỉ trỏ link tới
rồi các bạn đọc lại).
Ta cần xây dựng mô hình class cho Weather theo tọa độ và địa chỉ của kết quả Json 2
loại dưới đây:
/> lạt
Như sau:



Tui sẽ không phân tích hay giải thích tại sao phải viết các
lớp như trên nữa, vì Tui đã hướng dẫn chi tiết ở các bài 61, bài 62, bài 63 các bạn bắt
buộc phải đọc lại.
Soure code từng lớp như sau:
Lớp Clouds:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

package com.tranduythanh.model;
/**
* Created by drthanh on 04/04/2015.
*/
public class Clouds {
private int all;
public int getAll() {
return all;

}
public void setAll(int all) {
this.all = all;
}
}

Lớp Coord:
1
2
3
4
5
6
7
8
9
10
11
12
13

package com.tranduythanh.model;
/**
* Created by drthanh on 04/04/2015.
*/
public class Coord {
private double lon;
private double lat;
public double getLon() {
return lon;

}
public void setLon(double lon) {
this.lon = lon;


14
15
16
17
18
19
20
21
22
23
24
25

}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
}

Lớp Main:
1
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

33
34
35
36

package com.tranduythanh.model;
public class Main {
private double temp;
private double temp_min;
private double temp_max;
private double pressure;
private double sea_level;
private double grnd_level;
private double humidity;
public double getTemp() {
return temp;
}
public void setTemp(double temp) {
this.temp = temp;
}
public double getTemp_min() {
return temp_min;
}
public void setTemp_min(double temp_min)
this.temp_min = temp_min;
}
public double getTemp_max() {
return temp_max;
}
public void setTemp_max(double temp_max)

this.temp_max = temp_max;
}
public double getPressure() {
return pressure;
}
public void setPressure(double pressure)
this.pressure = pressure;
}
public double getHumidity() {
return humidity;
}
public void setHumidity(double humidity)
this.humidity = humidity;
}
public double getSea_level() {
return sea_level;
}

{

{

{

{


37
38
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

public void setSea_level(double sea_level) {
this.sea_level = sea_level;
}
public double getGrnd_level() {
return grnd_level;
}
public void setGrnd_level(double grnd_level) {
this.grnd_level = grnd_level;
}
}


Lớp Sys:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

package com.tranduythanh.model;

public class Sys {
private double message;
private String country;
private long sunrise;
private long sunset;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public long getSunrise() {
return sunrise;
}
public void setSunrise(long sunrise) {
this.sunrise = sunrise;
}
public long getSunset() {
return sunset;
}
public void setSunset(long sunset) {
this.sunset = sunset;
}
public double getMessage() {
return message;
}
public void setMessage(double message) {
this.message = message;
}



28
29
30
31
32
33
34

}

Lớp WeatherItem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

package com.tranduythanh.model;
public class WeatherItem {
private long id;
private String main;
private String description;
private String icon;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getMain() {
return main;

}
public void setMain(String main) {
this.main = main;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
}

Lớp Wind:
1
2
3
4

package com.tranduythanh.model;
public class Wind {
private double speed;
private double deg;



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public double getDeg() {
return deg;
}
public void setDeg(double deg) {
this.deg = deg;

}
}

Và cuối cùng là lớp tổng hợp JSON:
Lớp OpenWeatherJSon:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

26
27
28

package com.tranduythanh.model;
import java.util.List;
public class OpenWeatherJSon {
private Coord coord;
private Sys sys;
private List<WeatherItem> weather;
private String base;
private Main main;
private Wind wind;
private Clouds clouds;
private long dt;
private long id;
private String name;
private int cod;
public Coord getCoord() {
return coord;
}
public void setCoord(Coord coord) {
this.coord = coord;
}
public Sys getSys() {
return sys;
}
public void setSys(Sys sys) {
this.sys = sys;
}



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

public List<WeatherItem> getWeather() {
return weather;
}
public void setWeather(List<WeatherItem> weather) {
this.weather = weather;

}
public String getBase() {
return base;
}
public void setBase(String base) {
this.base = base;
}
public Main getMain() {
return main;
}
public void setMain(Main main) {
this.main = main;
}
public Wind getWind() {
return wind;
}
public void setWind(Wind wind) {
this.wind = wind;
}
public Clouds getClouds() {
return clouds;
}
public void setClouds(Clouds clouds) {
this.clouds = clouds;
}
public long getDt() {
return dt;
}
public void setDt(long dt) {
this.dt = dt;

}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

99
100
101
102
103
104
105

public void setName(String name) {
this.name = name;
}
public int getCod() {
return cod;
}
public void setCod(int cod) {
this.cod = cod;
}
}

– Như vậy ta đã có đủ mô hình lớp về kết quả dự báo thời tiết theo định dạng JSON.
– Bạn cần chú ý phải thêm thư viện cho dự án để đưa về Json thông qua GSon (Bạn
xem lại thư viện ở các bài trước, ở bài này Tui không hướng dẫn nữa).
– Tiến hành viết thư viện đọc thời tiết bằng đa tiến trình, thư viên này được sử dụng
và tái sử dụng cho các chức năng kiểm tra thời tiết theo tọa độ hay địa chỉ.

Chi tiết từng lớp như sau:
Enum TypePrediction:
1
2
3

4
5
6
7

package com.tranduythanh.utils;
/**
* Created by drthanh on 11/05/2015.
*/
public enum TypePrediction {
ADDRESS_NAME,//nhập theo địa chỉ cụ thể
LATITUDE_LONGITUDE//nhập theo vĩ độ kinh độ


8
9

}

enum này để chia ra 2 loại là xem thời tiết theo địa chỉ và theo tọa độ.
Lớp OpenWeatherMapAPI, lớp này dùng để đọc thông tin thời tiết (đưa Json về
model java class thông qua GSon):
1 package com.tranduythanh.utils;
2
3 import com.google.gson.Gson;
import com.tranduythanh.model.OpenWeatherJSon;
4
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.net.MalformedURLException;

8 import java.net.URL;
import java.net.URLEncoder;
9
10 /**
11 * Created by drthanh on 03/04/2015.
12 */
13 public class OpenWeatherMapAPI {
14 public static OpenWeatherJSon prediction(String q)
{
15 try {
16 String location= URLEncoder.encode(q, "UTF-8");
17
18 URL url = new URL(" />19 q="+location);
reader = new InputStreamReader(url.openStream(),"UTF20 InputStreamReader
8");
21 OpenWeatherJSon results = new Gson().fromJson(reader,
22 OpenWeatherJSon.class);
23
24 String idIcon = results.getWeather().get(0).getIcon().toString();
25 String urlIcon = " />URL urlImage = new URL(urlIcon);
26
27 return results;
28
29 } catch (MalformedURLException e) {
30 // TODO Auto-generated catch block
31 //e.printStackTrace();
32 } catch (IOException e) {
// TODO Auto-generated catch block
33 //e.printStackTrace();
34 }

35 return null;
36 }
37
38 /**
*
39 * />40 lat=10.778182&lon=106.665504
41 * @param lat
42 * @param lon
* @return


43 */
44 public static OpenWeatherJSon prediction(double lat,double lon)
{
45 try {
46
47 URL url = new URL(" />48 lat="+lat+"&lon="+lon);
49 InputStreamReader reader = new InputStreamReader(url.openStream(),"UTF8");
50 OpenWeatherJSon results = new Gson().fromJson(reader,
51 OpenWeatherJSon.class);
52
53 String idIcon = results.getWeather().get(0).getIcon().toString();
54 String urlIcon = " />55 URL urlImage = new URL(urlIcon);
56
return results;
57
58 } catch (MalformedURLException e) {
59 // TODO Auto-generated catch block
60 //e.printStackTrace();
61 } catch (IOException e) {

TODO Auto-generated catch block
62 //
//e.printStackTrace();
63 }
64 return null;
65 }
66
67 /**
* Sửa lại WeatherJSON vì chưa phù hợp trong trường hợp Daily
68 * />69 lat=10.778182&lon=106.66550&cnt=10
70 * @param lat
71 * @param lon
72 * @param cnt
* @return
73 */
74 public static OpenWeatherJSon predictionDaily(double lat,double lon,int cnt)
75 {
76 try {
77
url = new URL(" />78 URL
lat="+lat+"&lon="+lon+"&cnt="+cnt);
79 InputStreamReader reader = new InputStreamReader(url.openStream(),"UTF80 8");
81 OpenWeatherJSon results = new Gson().fromJson(reader,
82 OpenWeatherJSon.class);
83
String idIcon = results.getWeather().get(0).getIcon().toString();
84 String urlIcon = " />85 URL urlImage = new URL(urlIcon);
86
87 return results;
88

89 } catch (MalformedURLException e) {
90 // TODO Auto-generated catch block
//e.printStackTrace();
91 } catch (IOException e) {
92 // TODO Auto-generated catch block
93 //e.printStackTrace();
94 }
return null;


95 }
96
97 /**
* Sửa lại WeatherJSON vì chưa phù hợp trong trường hợp Daily
98 * />99 lạt&cnt=10
10 * @param q
0 * @param cnt
* @return
10 */
1 public static OpenWeatherJSon predictionDaily(String q,int cnt)
10 {
2 try {
10 String location= URLEncoder.encode(q, "UTF-8");
URL url = new URL(" />3 q="+location+"&cnt="+cnt);
10 InputStreamReader reader = new InputStreamReader(url.openStream(),"UTF4 8");
10 OpenWeatherJSon results = new Gson().fromJson(reader,
5 OpenWeatherJSon.class);
10 String idIcon = results.getWeather().get(0).getIcon().toString();
6 String urlIcon = " />10 URL urlImage = new URL(urlIcon);
7

10 return results;
8
10 } catch (MalformedURLException e) {
// TODO Auto-generated catch block
9 //e.printStackTrace();
11 } catch (IOException e) {
0 // TODO Auto-generated catch block
11 //e.printStackTrace();
1 }
return null;
11 }
2 }
11
3
11
4
11
5
11
6
11
7
11
8
11
9
12
0
12
1

12
2
12


3
12
4
12
5
12
6
12
7
12
8
12
9
13
0
13
1
13
2
13
3
13
4

– Lớp WeatherAsyncTask, để thực hiện đa tiến trình truy vấn thời tiết, hiển thị lên

giao diện. Lớp này được sử dụng chung cho cả theo Địa chỉ hay theo tọa độ.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

package com.tranduythanh.utils;
import
import
import

import
import
import
import
import
import
import

android.app.Activity;
android.app.ProgressDialog;
android.graphics.Bitmap;
android.graphics.BitmapFactory;
android.location.Address;
android.location.Geocoder;
android.os.AsyncTask;
android.view.View;
android.widget.ImageView;
android.widget.TextView;

import
import
import
import
import
import
import

com.google.android.gms.maps.GoogleMap;
com.google.android.gms.maps.model.Marker;
com.gtranslate.Language;

com.gtranslate.Translator;
com.tranduythanh.model.OpenWeatherJSon;
com.tranduythanh.weatherprediction.MyInfoWindowAdapter;
com.tranduythanh.weatherprediction.R;

import
import
import
import
import
import
import
import

java.io.IOException;
java.io.InputStream;
java.net.HttpURLConnection;
java.net.URL;
java.text.DecimalFormat;
java.text.NumberFormat;
java.util.Date;
java.util.List;


25
26
27
28
29
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

import java.util.Locale;
/**
* Created by drthanh on 11/05/2015.
*/
public class WeatherAsyncTask extends
AsyncTask<Void,Void,OpenWeatherJSon>{
ProgressDialog dialog;
Activity activity;
TypePrediction typePrediction;
String q;
double latitude;
double longitude;
NumberFormat format = new DecimalFormat("#0.0");

Bitmap myBitmap=null;
Marker marker=null;
GoogleMap map=null;
/**
* Constructor dùng để lấy thời tiết theo địa chỉ bất kỳ
* @param activity
* @param q
*/
public WeatherAsyncTask(Activity activity,String q)
{
this.activity=activity;
this.typePrediction=TypePrediction.ADDRESS_NAME;
this.q=q;
this.dialog=new ProgressDialog(activity);
this.dialog.setTitle("Đang tải thông tin ...");
this.dialog.setMessage("Vui lòng chờ...");
this.dialog.setCancelable(true);
}
/**
* constructor cho phép lấy thông tin thời tiết theo tọa độ bất kỳ
* @param activity
* @param latitude
* @param longitude
*/
public WeatherAsyncTask(Activity activity,double latitude,double longitude)
{
this.activity=activity;
this.typePrediction=TypePrediction.LATITUDE_LONGITUDE;
this.latitude=latitude;
this.longitude=longitude;

this.dialog=new ProgressDialog(activity);
this.dialog.setTitle("Đang tải thông tin ...");
this.dialog.setMessage("Vui lòng chờ...");
this.dialog.setCancelable(true);
}
/**
* constructor cho lấy thông tin thời tiết theo tọa độ bất kỳ trên bản đồ
* @param marker
* @param map
* @param activity
* @param latitude
* @param longitude
*/
public WeatherAsyncTask(Marker marker,GoogleMap map,Activity
activity,double latitude,double longitude)


77 {
78 this(activity,latitude,longitude);
this.marker=marker;
79 this.map=map;
80 }
81 @Override
82 protected void onPreExecute() {
super.onPreExecute();
83 this.dialog.show();
84 }
85
86 @Override
87 protected OpenWeatherJSon doInBackground(Void... params) {

88 OpenWeatherJSon openWeatherJSon=null;
if(typePrediction== TypePrediction.LATITUDE_LONGITUDE)
89 openWeatherJSon= OpenWeatherMapAPI.prediction(latitude,longitude);
90 else
91 openWeatherJSon= OpenWeatherMapAPI.prediction(q);
92 try {
idIcon = openWeatherJSon.getWeather().get(0).getIcon().toString();
93 String
String urlIcon = " />94 //Tiến hành tạo đối tượng URL
95 URL urlConnection = new URL(urlIcon);
96 //Mở kết nối
97 HttpURLConnection connection = (HttpURLConnection) urlConnection
.openConnection();
98 connection.setDoInput(true);
99 connection.connect();
10 //Đọc dữ liệu
0 InputStream input = connection.getInputStream();
10 //Tiến hành convert qua hình ảnh
myBitmap = BitmapFactory.decodeStream(input);
1 } catch (Exception e) {
10 e.printStackTrace();
2 }
10 return openWeatherJSon;
3 }
10 @Override
4 protected void onProgressUpdate(Void... values) {
10 super.onProgressUpdate(values);
5 }
10
6 @Override

protected void onPostExecute(OpenWeatherJSon openWeatherJSon) {
10 super.onPostExecute(openWeatherJSon);
7 if(map!=null) {
10 map.setInfoWindowAdapter(new
8 MyInfoWindowAdapter(openWeatherJSon,myBitmap,marker,
10 this.activity,latitude,longitude));
marker.showInfoWindow();
9 this.dialog.dismiss();
110 return;
111 }
112 TextView txtTemperature=(TextView)
113 activity.findViewById(R.id.txtTemperature);
TextView txtCurrentAddressName=(TextView)
114 activity.findViewById(R.id.txtCurrentAddressName);
115 ImageView imageView=(ImageView) activity.findViewById(R.id.imgBauTroi);
116 TextView txtMaxtemp=(TextView) activity.findViewById(R.id.txtMaxTemp);
117 TextView txtMinTemp=(TextView) activity.findViewById(R.id.txtMinTemp);
TextView txtWind=(TextView) activity.findViewById(R.id.txtWind);
118 TextView txtCloudliness= (TextView)


119 activity.findViewById(R.id.txtCloudliness);
12 TextView txtPressure= (TextView) activity.findViewById(R.id.txtPressure);
TextView txtHumidty= (TextView) activity.findViewById(R.id.txtHumidty);
0 TextView txtSunrise= (TextView) activity.findViewById(R.id.txtSunrise);
12 TextView txtSunset= (TextView) activity.findViewById(R.id.txtSunset);
1 double temperature=openWeatherJSon.getMain().getTemp()-273.15;
12 String maxtemp= format.format(openWeatherJSon.getMain().getTemp_max()273.15)+"°C";
2 String mintemp= format.format(openWeatherJSon.getMain().getTemp_min()12 273.15)+"°C";
3 String wind= openWeatherJSon.getWind().getSpeed()+" m/s";

12 String mesg = openWeatherJSon.getWeather().get(0).getMain();
4 // Translator translate = Translator.getInstance();
// String cloudiness=mesg+" ("+translate.translate(mesg,
12 Language.ENGLISH, Language.VIETNAMESE)+")";
5 String cloudiness=mesg;
12 String pressure= openWeatherJSon.getMain().getPressure()+" hpa";
6 String humidity=openWeatherJSon.getMain().getHumidity()+" %";
12
timeSunrise = new Date(openWeatherJSon.getSys().getSunrise()*1000);
7 Date
String Sunrise= timeSunrise.getHours()+":"+timeSunrise.getMinutes()+"
12 AM";
8 Date timeSunSet = new Date(openWeatherJSon.getSys().getSunset()*1000);
12 String sunset= timeSunSet.getHours()+":"+timeSunSet.getMinutes();
9 txtTemperature.setText(format.format(temperature)+"°C");
imageView.setImageBitmap(myBitmap);
13 txtMaxtemp.setText(maxtemp);
0 txtMinTemp.setText(mintemp);
13 txtWind.setText(wind);
1 txtCloudliness.setText(cloudiness);
13 txtPressure.setText(pressure);
txtHumidty.setText(humidity);
2 txtSunrise.setText(Sunrise);
13 txtSunset.setText(sunset);
3
13 try {
4 Geocoder geocoder;
addresses;
13 List<Address>
geocoder = new Geocoder(this.activity, Locale.getDefault());

5 if(typePrediction==TypePrediction.LATITUDE_LONGITUDE)
13 addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1
6 represent max location result to returned, by documents it recommended 1
13 to 5
else
7 {
13 addresses = geocoder.getFromLocationName(q, 1);
8 }
13 Address address=null;
9 if(addresses.size()>0)
address=addresses.get(0);
14 if(address!=null)
0 {
14 if(typePrediction==TypePrediction.LATITUDE_LONGITUDE)
1 txtCurrentAddressName.setText(address.getAddressLine(0));
14 else
txtCurrentAddressName.setText(q);
2 /*String city = address.getLocality();
14 String state = address.getAdminArea();
3 String country = address.getCountryName();
14 String postalCode = address.getPostalCode();
String knownName = address.getFeatureName();*/
4 }
14


×