관리 메뉴

moozi

Bus03 본문

TIS_2019/응용sw2019_1기

Bus03

moozi 2019. 6. 14. 11:20

package com.cookandroid.bus03;

import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;

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.BitmapDescriptorFactory;
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.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

private class DownloadWebpageTask extends AsyncTask<String,Void,String> {

//주요 내용 실행
@Override
protected String doInBackground(String... urls) {
String busRouteId="";
try {
Document doc = Jsoup.connect(urls[0]).get();
Elements headerCd_elements = doc.select("busRouteId");
for (Element item : headerCd_elements) {
busRouteId =item.text().trim();
}

} catch (IOException e) {
e.printStackTrace();
}

return busRouteId;
}


@Override
protected void onPostExecute(String busRouteId) {
String url="http://ws.bus.go.kr/api/rest/buspos/getBusPosByRtid?serviceKey=OckdUErbAGthsbx6VYITeQ9xzEpUow%2F25CZWigZoPo3JVEW%2FU%2Bpd%2B2ZiN06BuDRn3Zjfh21AHXvgg8cWnuP9Hw%3D%3D&busRouteId="+busRouteId;
Log.d("==url==",url);
new DownloadWebpageTask2().execute(url);
}
}

private class DownloadWebpageTask2 extends AsyncTask<String,Void,String> {

Document doc;
Elements gpsX_elements;
Elements gpsY_elements;
Elements plainNo_elements;

private void displayBus(String gpsX, String gpsY, String plainNo) {

double latitude;
double longitude;
LatLng LOC;

latitude = Double.parseDouble(gpsY);
longitude = Double.parseDouble(gpsX);
LOC = new LatLng(latitude, longitude);
Marker mk1 = mMap.addMarker(new MarkerOptions()
.position(LOC)
.title(plainNo)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.bus)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(LOC));
mMap.moveCamera(CameraUpdateFactory.zoomTo(11));

}

//주요 내용 실행
@Override
protected String doInBackground(String... urls) {
String busRouteId="";
try {
doc = Jsoup.connect(urls[0]).get();
gpsX_elements = doc.select("gpsX");
gpsY_elements = doc.select("gpsY");
plainNo_elements = doc.select("plainNo");
} catch (IOException e) {
e.printStackTrace();
}

return null;
}


protected void onPostExecute(String result) {

for (int i=0;i<gpsX_elements.size();i++) {
String gpsX = gpsX_elements.get(i).text().trim(); // gpsX
String gpsY = gpsY_elements.get(i).text().trim(); //gpsY
String plainNo = plainNo_elements.get(i).text().trim(); //plainNo

displayBus(gpsX, gpsY, plainNo);
}
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}


/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

String url="http://ws.bus.go.kr/api/rest/busRouteInfo/getBusRouteList?serviceKey=OckdUErbAGthsbx6VYITeQ9xzEpUow%2F25CZWigZoPo3JVEW%2FU%2Bpd%2B2ZiN06BuDRn3Zjfh21AHXvgg8cWnuP9Hw%3D%3D&strSrch=402";
DownloadWebpageTask task1 = new DownloadWebpageTask();
task1.execute(url);

}
}

'TIS_2019 > 응용sw2019_1기' 카테고리의 다른 글

폰갭연습문제01  (0) 2019.06.17
안드로이드연습문제10  (0) 2019.06.14
안드로이드연습문제09  (0) 2019.06.13
bus01  (0) 2019.06.13
jsp01 MainActiviy.java  (0) 2019.06.12
Comments