はじめに
開発環境 AndroidStudio
言語 Java
対象読者 初心者
概要
自分の中でオープンデータが流行している今日このごろ、NASAでオープンデータが公開されていることを知りました
OpenNASA
https://open.nasa.gov/blog/welcome-to-nasas-data-portal/(外部リンク)
今回のアプリで、実際に送られてくるJSONデータはこちら
https://data.nasa.gov/resource/gh4g-9sfh.json(外部リンク)
簡単に解説すると
のような感じです。書いてないところはわからないのでわかる方いらっしゃればご一報ください。
隕石の種類はWikipediaにページが有りました。
https://en.wikipedia.org/wiki/Meteorite#Falls(外部リンク)
今回のアプリで、実際に送られてくるJSONデータはこちら
https://data.nasa.gov/resource/gh4g-9sfh.json(外部リンク)
簡単に解説すると
"mass" : "21", 重量
"id" : "1", ID
"nametype" : "Valid",
"geolocation" : { 座標
"needs_recoding" : false,
"longitude" : "6.08333",
"latitude" : "50.775"
},
"recclass" : "L5", 隕石の種類
"fall" : "Fell",
"name" : "Aachen", 隕石の名前
"year" : "1880-01-01T00:00:00", 落下した年
"reclong" : "6.083330", 経度
"reclat" : "50.775000" 緯度
のような感じです。書いてないところはわからないのでわかる方いらっしゃればご一報ください。
隕石の種類はWikipediaにページが有りました。
https://en.wikipedia.org/wiki/Meteorite#Falls(外部リンク)
GoogleMapの設定の仕方は過去のブログを御覧ください
Maps API keyの入手をMacBookで | Android
ソースコード
ソースコードはGitに公開されていたこのソースコードを少しいじって実現したいと思います
packageの表記を省略しています
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author saxman
*/
public class MapsActivity extends FragmentActivity {
private static final String LOG_TAG = "MapApp";
private static final String SERVICE_URL = "https://data.nasa.gov/resource/gh4g-9sfh.json";
protected GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (map == null) {
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
if (map != null) {
setUpMap();
}
}
}
private void setUpMap() {
// Retrieve the city data from the web service
// In a worker thread since it's a network operation.
new Thread(new Runnable() {
public void run() {
try {
retrieveAndAddCities();
} catch (IOException e) {
Log.e(LOG_TAG, "Cannot retrive cities", e);
return;
}
}
}).start();
}
protected void retrieveAndAddCities() throws IOException {
HttpURLConnection conn = null;
final StringBuilder json = new StringBuilder();
try {
// Connect to the web service
URL url = new URL(SERVICE_URL);
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Read the JSON data into the StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
json.append(buff, 0, read);
}
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to service", e);
throw new IOException("Error connecting to service", e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
// Create markers for the city data.
// Must run this on the UI thread since it's a UI operation.
runOnUiThread(new Runnable() {
public void run() {
try {
createMarkersFromJson(json.toString());
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON", e);
}
}
});
}
void createMarkersFromJson(String json) throws JSONException {
// De-serialize the JSON string into an array of city objects
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
// Create a marker for each city in the JSON data.
JSONObject jsonObj = jsonArray.getJSONObject(i);
if (jsonObj.length() == 10) {
map.addMarker(new MarkerOptions()
.title(jsonObj.getString("name"))
.position(new LatLng(
Double.parseDouble(jsonObj.getString("reclat")),
Double.parseDouble(jsonObj.getString("reclong"))
))
);
} else {
Log.v("json", "error data");
}
}
}
}
この、NASAのデータはNULLが入って来ず、NULL値の場合にはその項目自体を送ってくれないので、項目が10に満たない場合はエラーデータとして処理していますスクリーンショット(16'1/13追記)

まとめ
面白いデータを見つけてしまった
NASAが公開したデータだから、ロシアの隕石がないのかな?
もう少し遊んでみたいと思う
コメント
コメントを投稿