λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

Etc/Android

[Android] 곡곡데이터 νŒŒμ‹±ν›„ T-Map지도에 마컀 κ΅¬ν˜„ν•˜κΈ°

728x90
λ°˜μ‘ν˜•
 

[Android]곡곡데이터 'λ―Όλ°©μœ„ λŒ€ν”Όμ†Œμ •λ³΄ μ„œλΉ„μŠ€' API νŒŒμ‹±ν•˜κΈ°

더보기 [πŸ‘¨‍πŸŽ“ν•™κ΅ μˆ˜μ—…πŸ‘¨‍πŸŽ“/[3-2]Android] - [Android] T-Map API을 μ‚¬μš©ν•˜μ—¬ 지도 λ„μ›Œλ³΄κΈ° [Android] T-Map API을 μ‚¬μš©ν•˜μ—¬ 지도 λ„μ›Œλ³΄κΈ° [πŸ‘¨‍πŸŽ“ν•™κ΅ μˆ˜μ—…πŸ‘¨‍πŸŽ“/[3-2]Android] - [Android]..

sungmin.dev

 

[Android] T-Map API을 μ‚¬μš©ν•˜μ—¬ 지도 λ„μ›Œλ³΄κΈ°

[πŸ‘¨‍πŸŽ“ν•™κ΅ μˆ˜μ—…πŸ‘¨‍πŸŽ“/[3-2]Android] - [Android] μžλ°”λ‘œ μ•ˆλ“œλ‘œμ΄λ“œ μ•± λ‹€μŒ 지도 APIμ‚¬μš©ν•˜μ—¬ 지도 λ„μ›Œλ³΄κΈ° [Android] μžλ°”λ‘œ μ•ˆλ“œλ‘œμ΄λ“œ μ•± λ‹€μŒ 지도 APIμ‚¬μš©ν•˜μ—¬ 지도 λ„μ›Œλ³΄κΈ° λ‹€μŒμ§€λ„ API ν‚€ 발..

sungmin.dev

λ³Έ 글은 NYλ‹˜μ˜ κΈ€(http://blog.naver.com/PostView.nhn?blogId=yandul83&logNo=220436066801)을 μ°Έκ³ ν•˜μ—¬ μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

이전 글을 보면 곡곡데이터인 'λ―Όλ°©μœ„ λŒ€ν”Όμ†Œ 정보 μ„œλΉ„μŠ€'λ₯Ό νŒŒμ‹± ν•˜λŠ” 것을 확인할 수 μžˆλ‹€.

ν˜„μž¬ λ‚˜λŠ” 좩남도청 곡곡데이터 μ„œν¬ν„°μ¦ˆ 1기둜 ν™œλ™ 쀑이며, μœ„μ˜ 곡곡데이터λ₯Ό ν™œμš©ν•˜μ—¬ λͺ¨λ°”일 SW ν”„λ‘œμ νŠΈ μˆ˜μ—…μ˜
ν”„λ‘œμ νŠΈλ₯Ό μ§„ν–‰ν•˜κ³  μžˆλ‹€.

곡곡데이터λ₯Ό νŒŒμ‹±ν•œν›„ 1주일 λ™μ•ˆ 지도에 마컀λ₯Ό λ„μš°κΈ° μœ„ν•΄ λ§Žμ€ μ‚½μ§ˆμ„ ν•˜μ˜€κ³  T-Map의 μžλ£Œκ°€ 많이 μ—†μ–΄
직접 μž‘μ„±ν•˜κΈ°λ‘œ ν•˜μ˜€λ‹€.

μš°μ„  기본적인 κ°œλ°œν™˜κ²½μ€ λ‹€μŒκ³Ό κ°™λ‹€.

개발 μ–Έμ–΄ : Java 12.0.2

개발 툴 : Android Studio 3.5.2

지도 : T-Map (http://tmapapi.sktelecom.com/index.html)

곡곡 데이터 : λ―Όλ°©μœ„ λŒ€ν”Όμ†Œ 정보 μ„œλΉ„μŠ€ (https://www.data.go.kr/dataset/3038747/openapi.do?mypageFlag=Y)

이 κΈ€μ—μ„œλŠ” 곡곡데이터 νŒŒμ‹± ν•˜λŠ” 뢀뢄은 μ œμ™Έν•˜κ³  글을 μž‘μ„±ν•˜λ €κ³  ν•œλ‹€.
νŒŒμ‹± 뢀뢄은 이전 글을 μ°Έκ³ ν•˜κΈ° λ°”λž€λ‹€.

1. 곡곡데이터 νŒŒμ‹±μ„ μœ„ν•œ Class λ§Œλ“€κΈ°


κ°€μž₯ λ¨Όμ € ν•΄λ‹Ή 데이터λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” 곡곡데이터 ν¬ν„Έμ—μ„œ μ œκ³΅ν•˜λŠ” κΈ°μˆ λ¬Έμ„œλ₯Ό 확인을 ν•΄μ•Ό ν•œλ‹€.
κ·Έ μ΄μœ λŠ” λ°μ΄ν„°λ§ˆλ‹€ tag값이 λ‹€λ₯΄κ³  인터넷에 제곡된 μ½”λ“œλ₯Ό 따라 ν•˜λ‹€ 보면 tag값이 λ‹¬λΌμ Έμ„œ νŒŒμ‹±μ΄ μ•ˆ λ˜λŠ” κ²½μš°κ°€ 많이 있기 λ•Œλ¬Έμ΄λ‹€.

μš°μ„  λ‚΄κ°€ ν•„μš”ν•œ λŒ€ν”Όμ†Œ 이름, μœ„λ„, 경도λ₯Ό 뢈러였기 μœ„ν•œ 클래슀λ₯Ό λ§Œλ“ λ‹€.

MapPoint.java

public class MapPoint {
    private String Name; //λŒ€ν”Όμ†Œ 이름
    private double  latitude; //μœ„λ„
    private  double longitude; //경도

    public MapPoint() {
        super();
    }

    public  MapPoint(String Name, double latitude, double longitude) {
        this.Name = Name;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public String getName() { //λŒ€ν”Όμ†Œ 이름
        return Name;
    }

    public void setName(String Name){
        this.Name = Name;
    }

    public double getLatitude() { //μœ„λ„
        return latitude;
    }

    public  void setLatitude(double latitude){
        this.latitude = latitude;
    }

    public double getLongitude() { //경도
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
}

 

2. API νŒŒμ‹± Class 클래슀 λ§Œλ“€κΈ°


곡곡데이터λ₯Ό 마컀둜 찍기 μœ„ν•΄μ„  ArrayListλ₯Ό 톡해 배열에 각각의 데이터듀을 νŒŒμ‹± ν•΄ μΆ”κ°€ν•΄μ£ΌλŠ” κ²ƒμœΌλ‘œ κ΅¬ν˜„μ„ ν•˜μ˜€λ‹€.
λ˜ν•œ νŒŒμ‹± 된 κ²°κ³Όκ°€ 좔가될 경우 trueκ°’μœΌλ‘œ λ³€κ²½ ν•΄μ£Όλ©° 값이 듀어간것을 ν™•μΈν•œν›„ falseκ°’μœΌλ‘œ λ°”κΏ”μ£Όμ–΄ νŒŒμ‹±λœ κ²°κ³Όλ₯Ό ν™•μΈν•˜μ˜€λ‹€.

ν•΄λ‹Ή λ°μ΄ν„°λŠ” jsonκ³Ό xml을 λͺ¨λ‘ μ§€μ›ν•˜λŠ”λ° λ‚˜λŠ” xml으둜 νŒŒμ‹± ν•˜μ—¬ 마컀λ₯Ό 찍어 μ£Όμ—ˆλ‹€.

ShelterApi.java

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.BufferedInputStream;
import java.net.URL;
import java.util.ArrayList;

public class ShelterApi {
    private static String ServiceKey = "APIν‚€"; //곡곡데이터 μ‚¬μ΄νŠΈλ₯Ό 톡해 λ°œκΈ‰ 받은 APIν‚€
    public ShelterApi() {
        try {
            apiParserSearch();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public ArrayList<MapPoint> apiParserSearch() throws Exception {
        URL url = new URL(getURLParam(null));

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        BufferedInputStream bis = new BufferedInputStream(url.openStream());
        xpp.setInput(bis, "utf-8");

        String tag = null;
        int event_type = xpp.getEventType();

        ArrayList<MapPoint> mapPoint = new ArrayList<MapPoint>();

        String facility_name = null, longitude= null,latitude=null;
        boolean bfacility_name = false, blatitude = false, blongitude = false;

        while (event_type != XmlPullParser.END_DOCUMENT) {
            if (event_type == XmlPullParser.START_TAG) {
                tag = xpp.getName();
                if(tag.equals("facility_name")) {
                    bfacility_name = true;
                }
                if(tag.equals("latitude")) {
                    blatitude = true;
                }
                if(tag.equals("longitude")){
                    blongitude = true;
                }
            } else if (event_type == XmlPullParser.TEXT) {
                if(bfacility_name == true){
                    facility_name = xpp.getText();
                    bfacility_name = false;
                } else if(blatitude == true){
                    latitude = xpp.getText();
                    blatitude = false;
                }else if(blongitude ==true){
                    longitude = xpp.getText();
                    blongitude = false;
                }
            } else if (event_type == XmlPullParser.END_TAG) {
                tag = xpp.getName();
                if (tag.equals("row")) {
                    MapPoint entity = new MapPoint();
                    entity.setName(facility_name);
                    entity.setLatitude(Double.valueOf(latitude));
                    entity.setLongitude(Double.valueOf(longitude));
                    mapPoint.add(entity);
                    System.out.println(mapPoint.size());
                }
            }
            event_type = xpp.next();
        }
        System.out.println(mapPoint.size());

        return mapPoint;
    }

    private String getURLParam(String search){
        String url = "http://apis.data.go.kr/1741000/CivilDefenseShelter2/getCivilDefenseShelterList?ServiceKey=" + ServiceKey + "&type=xml&pageNo=1&numOfRows=300&flag=Y";
        return url;
    }

    public static void main(String[] args) {
        new ShelterApi();
    }
}

 

λ˜ν•œ 개발 λ¬Έμ„œλ₯Ό 보면 μš”μ²­ λ©”μ„Έμ§€μ˜ defailt 값듀은 type = xml, pageNo = 1, numOfRows = 10인 것을 확인할 수 μžˆλ‹€. 이 말은 url λΆ€λΆ„μ˜ target 값을 λ³€κ²½ μ•ˆ ν•΄μ£Όλ©΄ xml ν˜•μ‹μœΌλ‘œ 1νŽ˜μ΄μ§€μ— 10개 μ •λ„μ˜ λ°μ΄ν„°λ§Œ 좜λ ₯되기 λ•Œλ¬Έμ—
target 값을 λ³€κ²½ν•΄ μ£Όμ–΄μ•Ό ν•œλ‹€. (μœ„μ˜ μ½”λ“œλŠ” xmlν˜•μ‹μœΌλ‘œ νŒŒμ‹± ν•˜μ—¬ 300개의 데이터λ₯Ό 1νŽ˜μ΄μ§€μ— 좜λ ₯ν•˜κ³  μžˆλ‹€.)

κ°œλ°œλ¬Έμ„œμ— 적힌 λ‚΄μš©

 

3. 지도에 νŒŒμ‹± ν•œ κ²°κ³Ό 마컀둜 ν‘œμ‹œν•˜κΈ°


그럼 이제 νŒŒμ‹±ν•œ κ²°κ³Όλ₯Ό 마컀둜 ν‘œμ‹œν•΄μ£Όλ©΄ λœλ‹€.

μš°μ„  닀쀑 λ§ˆμ»€μ‹μœΌλ‘œ κ΅¬ν˜„μ„ ν•΄μ•Ό ν•˜κΈ° λ•Œλ¬Έμ— νŒŒμ‹±ν•œ κ²°κ³Όλ₯Ό for문을 톡해 κ΅¬ν˜„μ„ ν•˜μ˜€λ‹€.

MainActivity.java

package dev.sungmin.Shelter;

        import androidx.appcompat.app.AppCompatActivity;
        import android.os.Bundle;
        import android.os.StrictMode;
        import android.widget.LinearLayout;
        import android.location.Location;

        import com.skt.Tmap.TMapGpsManager;
        import com.skt.Tmap.TMapView;
        import com.skt.Tmap.TMapMarkerItem;
        import com.skt.Tmap.TMapPoint;

        import java.util.ArrayList;


public class MainActivity extends AppCompatActivity implements TMapGpsManager.onLocationChangedCallback {

    private boolean TrackingMode = true;
    private TMapView tMapView = null;
    private TMapGpsManager tmapgps = null;
    private static String TMapAPIKey = "μ•±";

    @Override
    public void onLocationChange(Location location) {
        if (TrackingMode) {
            tMapView.setLocationPoint(location.getLongitude(), location.getLatitude());
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);


        LinearLayout linearLayoutTmap = (LinearLayout) findViewById(R.id.linearLayoutTmap);
        tMapView = new TMapView(this);
        tMapView.setHttpsMode(true);
        //λ°œκΈ‰ 받은 APIν‚€  λ³΄μ•ˆμƒ 문제둜 μΈν•˜μ—¬ κΉƒν—ˆλΈŒ μ»€λ°‹μ‹œ μ§€μ›Œμ„œ 컀밋할것!
        tMapView.setSKTMapApiKey(TMapAPIKey);
        linearLayoutTmap.addView(tMapView);

        setUpMap();

        /*ν˜„μž¬ λ³΄λŠ” λ°©ν–₯으둜 μ„€μ •*/
        tMapView.setCompassMode(true);

        /*ν˜„ μœ„μΉ˜ μ•„μ΄μ½˜ μ„€μ •*/
        tMapView.setIconVisibility(true);

        tMapView.setZoomLevel(15);
        tMapView.setMapType(TMapView.MAPTYPE_STANDARD);
        tMapView.setLanguage(TMapView.LANGUAGE_KOREAN);

        tmapgps = new TMapGpsManager(MainActivity.this);
        tmapgps.setMinTime(1000);
        tmapgps.setMinDistance(5);
        tmapgps.setProvider(tmapgps.NETWORK_PROVIDER); //λ„€νŠΈμ›Œν¬μ—μ„œ κ°€μ Έμ˜΄
        tmapgps.OpenGps(); //κ°€μƒμœΌλ‘œ ν…ŒμŠ€νŠΈμ‹œ 이뢀뢄은 μ§€μ›Œμ£Όμ–΄μ•Ό 였λ₯˜κ°€ μ—†μŒ

        tMapView.setTrackingMode(true);
        tMapView.setSightVisible(true);
    }


    private void setUpMap() {
        ShelterApi parser = new ShelterApi();
        ArrayList<MapPoint> mapPoint = new ArrayList<MapPoint>();
        try {
            mapPoint = parser.apiParserSearch();
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (int i = 0; i < mapPoint.size(); i++) {
            for (MapPoint entity : mapPoint) {
                TMapPoint point = new TMapPoint(mapPoint.get(i).getLatitude(), mapPoint.get(i).getLongitude());
                TMapMarkerItem markerItem1 = new TMapMarkerItem();

                markerItem1.setPosition(0.5f, 1.0f);
                markerItem1.setTMapPoint(point);
                markerItem1.setName(entity.getName());
                tMapView.setCenterPoint(mapPoint.get(i).getLongitude(), mapPoint.get(i).getLatitude());
                tMapView.addMarkerItem("markerItem1" + i, markerItem1);
            }
        }
    }
}

 

* κ²°κ³Ό


μœ„μ˜ μ½”λ“œλ₯Ό 톡해 κ΅¬ν˜„ μ‹œ μ•„λž˜μ™€ 같은 μ‚¬μ§„μœΌλ‘œ ꡬ동됨을 확인할 수 μžˆλ‹€.

κ΅¬ν˜„ κ²°κ³Ό 1
κ΅¬ν˜„ κ²°κ³Ό 2

728x90
λ°˜μ‘ν˜•