xml 파서를 통해서 위도 경도를 읽어오는데 까지는 성공했는데 이 위도, 경도를 통해 마커를 찍을 수 있는지
질문드립니다...
public class MainActivity extends FragmentActivity {
/** Called when the activity is first created. */
String xml; //다운로드된 xml문서
GoogleMap TestMap;
LatLng loc = new LatLng(35.5437482,129.2561333);
double tmp,tmp2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
TestMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
TestMap.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 15));
TestMap.addMarker(new MarkerOptions().position(new LatLng(tmp,tmp2)).title("ㅋㅋ").snippet("ㅇㅇ"));
}
//버튼을 눌렀을때 실행되는 메소드
/* URL 인코딩 방법 예시
* String URL = "www.myhome.com/";
String subURL = "한글값";
String thirdURL = "/index";
sbuURL = URLEncoder.encode(subURL, "UTF-8");
URL = URL + subURL + thirdURL;
*/
public void down(View v){
StringBuffer sBuffer = new StringBuffer();
try{
String firstURL = "http://maps.googleapis.com/maps/api/geocode/xml?address=";
String hangul = "63빌딩";
String secondURL = "&sensor=true";
String sbuURL = URLEncoder.encode(hangul, "UTF-8");
URL url = new URL(firstURL+sbuURL+secondURL);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if(conn != null){
conn.setConnectTimeout(20000);
conn.setUseCaches(false);
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
//서버에서 읽어오기 위한 스트림 객체
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
//줄단위로 읽어오기 위해 BufferReader로 감싼다.
BufferedReader br = new BufferedReader(isr);
//반복문 돌면서읽어오기
while(true){
String line = br.readLine();
if(line==null){
break;
}
sBuffer.append(line);
}
br.close();
conn.disconnect();
}
}
//결과값 출력해보기
//editText.setText(sBuffer.toString());
xml = sBuffer.toString(); //결과값 변수에 담기
}catch (Exception e) {
// TODO: handle exception
Log.e("다운로드 중 에러 발생",e.getMessage());
}
parse();
}
//xml파싱하는 메소드
public void parse(){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
//xml을 InputStream형태로 변환
InputStream is = new ByteArrayInputStream(xml.getBytes());
//document와 element 는 w3c dom에 있는것을 임포트 한다.
Document doc = documentBuilder.parse(is);
Element element = doc.getDocumentElement();
//읽어올 태그명 정하기
NodeList lats = element.getElementsByTagName("lat");
NodeList lngs = element.getElementsByTagName("lng");
//자료를 누적시킬 stringBuffer 객체
StringBuffer sBuffer = new StringBuffer();
Node item = lats.item(0);
Node text = item.getFirstChild();
Node item2 = lngs.item(0);
Node text2 = item2.getFirstChild();
//해당 노드에서 문자열 읽어오기
String itemValue = text.getNodeValue();
String itemValue2 = text2.getNodeValue();
tmp = Double.parseDouble(itemValue);
tmp2 = Double.parseDouble(itemValue2);
}catch (Exception e) {
// TODO: handle exception
Log.e("파싱 중 에러 발생", e.getMessage());
}
}
}
소스가 이런 형태로 되어 있는데
parse 메소드에서 받아온 String 형 위도(itemValue), 경도(ItemValue2)를 double형으로 변환(tmp,tmp2)한 뒤 마커에 찍
어주면 될 거같은데 이렇게 하니까 아무것도 표시가 안돼서...
조언 부탁드리겠습니다..