본문 바로가기

프로그래밍/Android

ip주소로 위치정보 가져오기

GPS를 사용하지 않고, 

 

IP주소를 이용해 위치(좌표)정보를 얻을 수 있다.

 

 

간단하게 다음과 같이 주소를 요청 하면 

 

http://ip-api.com/json

 

아래와 같은 결과 값을 얻을 수 있다.

{"as":"AS4766 Korea Telecom","city":"Seoul","country":"Republic of Korea","countryCode":"KR","isp":"Korea Telecom","lat":37.5111,"lon":126.9743,"org":"Korea Telecom","query":"220.76.254.166","region":"11","regionName":"Seoul","status":"success","timezone":"Asia/Seoul","zip":""}

위치 좌표가 상세하게 나오지 않는다. 

 

IP주소를 이용해 조금더 상세한 위치정보 좌표를 얻기 위해 구글 API를 이용했다.

 

 

구글 API를 이용하는 방법

 

https://developers.google.com/maps/documentation/geolocation/intro?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
            RequestQueue queue = Volley.newRequestQueue(act);
            String url = "https://www.googleapis.com/geolocation/v1/geolocate?key=API키값";
            StringRequest req = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                   
                }
            }
 
            ) {
                @Override
                public Map<StringString> getHeaders() throws AuthFailureError {
                    HashMap<StringString> headers = new HashMap<StringString>();
                    headers.put("Content-type""application/json");
                    return headers;
                }
            };
            queue.add(req);
cs

 

{

    "location": {

        "lat": 37.566535,

        "lng": 126.97796919999999

    },

    "accuracy": 11500

}

 

'프로그래밍 > Android' 카테고리의 다른 글

이미지 로딩 관련  (0) 2013.07.18
Volley  (0) 2013.07.16
라이브러리  (0) 2013.07.12