-
Notifications
You must be signed in to change notification settings - Fork 982
/
WeatherGroundOverlaySample.java
124 lines (108 loc) · 4.18 KB
/
WeatherGroundOverlaySample.java
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
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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package org.osmdroid.samplefragments.data;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;
import org.osmdroid.samplefragments.BaseSampleFragment;
import org.osmdroid.util.BoundingBox;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.GroundOverlay;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
/**
* Sample on how to use the ground mOverlay, which places a geospatially referenced image on the map,
* scaling with zoom.
* <p>
* Related issues
* <a href="https://github.com/osmdroid/osmdroid/issues/883">https://github.com/osmdroid/osmdroid/issues/883</a>
* <a href="https://github.com/osmdroid/osmdroid/issues/684">https://github.com/osmdroid/osmdroid/issues/684</a>
* created on 1/21/2018.
*
* @author Alex O'Ree
*/
public class WeatherGroundOverlaySample extends BaseSampleFragment implements Runnable {
public static final String URL = "https://radar.weather.gov/Conus/RadarImg/latest_Small.gif";
private final GeoPoint mNorthEast = new GeoPoint(50.0, -127.5);
private final GeoPoint mSouthWest = new GeoPoint(21.0, -66.5);
private ConnectivityManager cm;
private GroundOverlay mOverlay;
@Override
public String getSampleTitle() {
return "Live weather for USA using Ground Overlay";
}
@Override
public void addOverlays() {
super.addOverlays();
mOverlay = new GroundOverlay();
mOverlay.setTransparency(0.5f);
mOverlay.setPosition(mNorthEast, mSouthWest);
mMapView.getOverlayManager().add(mOverlay);
cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
mMapView.post(new Runnable() {
@Override
public void run() {
final List<GeoPoint> geoPoints = new ArrayList<>();
geoPoints.add(mNorthEast);
geoPoints.add(mSouthWest);
mMapView.zoomToBoundingBox(BoundingBox.fromGeoPoints(geoPoints), false, 50);
}
});
Toast.makeText(getActivity(), "Downloading the weather image...", Toast.LENGTH_SHORT).show();
new Thread(this).start();
}
@Override
public void run() {
final NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
final boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (!isConnected) {
Activity act = getActivity();
if (act != null) {
act.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), "Cannot connect!", Toast.LENGTH_SHORT).show();
}
});
}
return;
}
URLConnection con;
InputStream is = null;
try {
final URL url = new URL(URL);
con = url.openConnection();
is = con.getInputStream();
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
final Bitmap bitmap = BitmapFactory.decodeStream(is);
mOverlay.setImage(bitmap);
final Activity act = getActivity();
if (act != null) {
act.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), "Weather image downloaded!", Toast.LENGTH_SHORT).show();
mMapView.invalidate();
}
});
}
} catch (Throwable e) {
Toast.makeText(getActivity(), "Cannot download the weather image!", Toast.LENGTH_SHORT).show();
Log.e(TAG, "error fetching image", e);
} finally {
if (is != null) try {
is.close();
} catch (IOException e) {
//
}
}
}
}