[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);
}
}
}
}
* ๊ฒฐ๊ณผ
์์ ์ฝ๋๋ฅผ ํตํด ๊ตฌํ ์ ์๋์ ๊ฐ์ ์ฌ์ง์ผ๋ก ๊ตฌ๋๋จ์ ํ์ธํ ์ ์๋ค.