๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๊ฐœ์ธ ๊ณต๋ถ€/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
๋ฐ˜์‘ํ˜•