728x90

# 실행 화면

# AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kr.ac.skuniv.air_marker">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

# activity_maps.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity" />

# MapsActivity.java

package kr.ac.skuniv.air_marker;

import androidx.fragment.app.FragmentActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private GoogleMap mMap;

    private static final LatLng SEOUL = new LatLng(37.566535, 126.977969);
    private static final LatLng BUSAN = new LatLng(35.179554, 129.075642);

    String pm10Seoul;
    String pm10Busan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        String serviceUrl = 
           "http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getCtprvnMesureLIst";
        String serviceKey = 
"BmggWicZAv3%2BOQxar9xrI6hNlsn9%2B%2B13cJPg0FRtn%2BBQ2HThaGn9qrYfb%2FTCSk%2FQGikZVrvxlWEvaamMaR%2FF9w%3D%3D";
        String itemCode = "PM10";
        String dataGubun = "HOUR";
        String searchCondition = "MONTH";
        String pageNo = "1";
        String numOfRows = "1";
        String strUrl =
           serviceUrl+"?itemCode="+itemCode+"&dataGubun="+dataGubun+"&searchCondition="+searchCondition+
                "&pageNo="+pageNo+"&numOfRows="+numOfRows+"&ServiceKey="+serviceKey;
        new DownloadWebpageTask().execute(strUrl);
    }

    private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            try {
                return (String)downloadUrl((String)urls[0]);
            } catch (IOException e) {
                return "다운로드 실패";
            }
        }

        protected void onPostExecute(String result) {
            try {
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(true);
                XmlPullParser xpp = factory.newPullParser();

                xpp.setInput(new StringReader(result));
                int eventType = xpp.getEventType();

                String resultCode = "";

                boolean bSet_resultCode = false;
                boolean bSet_itemCode = false;
                boolean bSet_seoul = false;
                boolean bSet_busan = false;

                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if(eventType == XmlPullParser.START_DOCUMENT) {
                        ;
                    } else if(eventType == XmlPullParser.START_TAG) {
                        String tag_name = xpp.getName();
                        if (tag_name.equals("resultCode"))
                            bSet_resultCode = true;
                        if (tag_name.equals("itemCode"))
                            bSet_itemCode = true;
                        if (tag_name.equals("seoul"))
                            bSet_seoul = true;
                        if (tag_name.equals("busan"))
                            bSet_busan = true;
                    } else if(eventType == XmlPullParser.TEXT) {
                        if (bSet_resultCode) {
                            resultCode = xpp.getText();
                            bSet_resultCode = false;
                        }

                        if (resultCode.equals("00")) {
                            if (bSet_seoul) {
                                pm10Seoul = xpp.getText();
                                bSet_seoul = false;
                            }
                            if (bSet_busan) {
                                pm10Busan = xpp.getText();
                                bSet_busan = false;
                                displayPm10(pm10Seoul, pm10Busan);
                            }
                        }

                    } else if(eventType == XmlPullParser.END_TAG) {
                        ;
                    }
                    eventType = xpp.next();
                }
            } catch (Exception e) {
            }
        }

        private String downloadUrl(String myurl) throws IOException {

            HttpURLConnection conn = null;
            try {
                URL url = new URL(myurl);
                conn = (HttpURLConnection) url.openConnection();
                BufferedInputStream buf = new BufferedInputStream(conn.getInputStream());
                BufferedReader bufreader = new BufferedReader(new InputStreamReader(buf, "utf-8"));
                String line = null;
                String page = "";
                while((line = bufreader.readLine()) != null) {
                    page += line;
                }

                return page;
            } finally {
                conn.disconnect();
            }
        }
        private void displayPm10(String seoul, String busan){
            Marker mSeoul = mMap.addMarker(new MarkerOptions().position(SEOUL).title(seoul));
                Marker mBusan = mMap.addMarker(new MarkerOptions().position(BUSAN).title(busan));
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SEOUL,6));
        }
    }
}
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기