구글 API 이용하여 원하는 주소값의 위도,경도 받아오기
google 프로젝트 등록 필요.
https://developers.google.com/maps/documentation/geocoding/get-api-key?hl=ko
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class Geocode { String response = ""; String run(String address) throws IOException { HttpPost httpPost = new HttpPost("https://maps.googleapis.com/maps/api/geocode/json?address="+address+"&components=country:KR"); HttpClient httpclient = new DefaultHttpClient(); HttpResponse httpResponse = httpclient.execute(httpPost); response = EntityUtils.toString(httpResponse.getEntity()); return response; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import java.io.IOException; import org.apache.http.ParseException; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.os.Bundle; import android.os.Handler; import android.os.Message; public class GeoThread extends Thread{ Handler handler; boolean isRun = true; String address = ""; public GeoThread(Handler handler){ this.handler = handler; } public void run(){ Geocode geo = new Geocode(); Bundle bun = new Bundle(); String response; try { response = geo.run(address); JSONObject json = new JSONObject(response); JSONArray results = (JSONArray) json.get("results"); JSONObject resultsArray = (JSONObject) results.get(0); JSONObject jsonObject = (JSONObject) resultsArray.get("geometry"); JSONObject jsonObject2 = (JSONObject) jsonObject.get("location"); double address_lat = (double) jsonObject2.get("lat"); double address_lng = (double) jsonObject2.get("lng"); bun.putDouble("address_lat", address_lat); bun.putDouble("address_lng", address_lng); Message msg = handler.obtainMessage(); msg.setData(bun); handler.sendMessage(msg); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 원하는 Activity에서 GeoThread geo = new GeoThread(addressHandler); String address = address_edt.getText().toString(); address = address.replaceAll(" ", ""); //주소에 geo.address = address; geo.start(); Handler addressHandler = new Handler() { public void handleMessage(Message msg) { Bundle bun = msg.getData(); double lat = bun.getDouble("address_lat"); double lng = bun.getDouble("address_lng"); } }; | cs |
'이것저것 > 자바*안드로이드' 카테고리의 다른 글
Android Studio 3.0.1 업데이트 AAPT2 에러 (0) | 2018.02.20 |
---|---|
안드로이드 dialog setTextSize (0) | 2017.11.06 |
FCM (0) | 2017.02.03 |
안드로이드 EditText 다음 버튼 원하는 포커스 이동 (0) | 2017.01.23 |
안드로이드 scrollview 스크롤바 보이기 (0) | 2017.01.23 |