티스토리 뷰

Programming/Android

Android GPS 사용

군자동꽃미남 2015. 12. 5. 21:44

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
public class cGPSInfo extends Service implements LocationListener
{
    private final Context   mContext;
 
    // 현재 GPS 사용유무
    boolean                 isGPSEnabled  = false;
 
    // 네트워크 사용유무
    boolean                 isNetworkEnabled = false;
 
    // GPS 상태값
    boolean                 isGetLocation = false;
 
    Location                location;
    double                  lat;                // 위도
    double                  lon;                // 경도
 
    // 최소 GPS 정보 업데이트 거리 10미터
    private static final long                   MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
 
    // 최소 GPS 정보 업데이트 시간 밀리세컨이므로 1분
    private static final long                   MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
 
    protected LocationManager                   locationManager;
 
    public cGPSInfo(Context _context)
    {
        this.mContext       = _context;
        getLocation();
    }
 
    public Location getLocation()
    {
        try
        {
            locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
 
            // GPS 정보 가져오기
            isGPSEnabled    = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
 
            // 현재 네트워크 상태 값 알아오기
            isNetworkEnabled= locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
 
            if (!isGPSEnabled && !isNetworkEnabled)
            {
                // GPS와 네트워크 사용이 가능하지 않을때 소스 구현
            }
            else
            {
                this.isGetLocation              = true;
                // 네트워크 정보로 부터 위치값 가져오기
                if (isNetworkEnabled)
                {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, this);
                    if (locationManager != null)
                    {
                        location                = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null)
                        {
                            // 위도 경도 저장
                            lat                 = location.getLatitude();
                            lon                 = location.getLongitude();
                        }
                    }
                }
 
                if (isGPSEnabled)
                {
                    if (location == null)
                    {
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        if (locationManager != null)
                        {
                            location            = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null)
                            {
                                lat             = location.getLatitude();
                                lon             = location.getLongitude();
                            }
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
 
        return location;
    }
 
 
    // GPS 종료
    public void stopUsingGPS()
    {
        if (locationManager != null)
        {
            locationManager.removeUpdates(this);
        }
    }
 
    // 위도 리턴
    public double getLatitude()
    {
        if (location != null)
            lat             = location.getLatitude();
 
        return lat;
    }
 
    // 경도 리턴
    public double getLongitude()
    {
        if (location != null)
            lon             = location.getLongitude();
 
        return lon;
    }
 
    // GPS 나 wifi 정보가 켜저있는지 확인.
    public boolean isGetLocation()
    {
        return isGetLocation;
    }
 
    // GPS 정보를 가져오지 못했을때 설정ㄱ밧으로 갈지 물어보는 alert창
    public void showSettingsAlert()
    {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
 
        alertDialog.setTitle("GPS 사용유무 셋팅");
        alertDialog.setMessage("GPS 셋팅이 되지 않았을수도 있습니다. \n설정창으로 가시겠습니까?");
 
        // OK를 누르게 되면 설정창으로 이동.
        alertDialog.setPositiveButton("Settings"new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Intent      intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });
 
        // Cancel 하면 종료
        alertDialog.setNegativeButton("Cancle"new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.cancel();
            }
        });
 
        alertDialog.show();
    }
 
    @Override
    public void onLocationChanged(Location location) {
 
    }
 
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
 
    }
 
    @Override
    public void onProviderEnabled(String provider) {
 
    }
 
    @Override
    public void onProviderDisabled(String provider) {
 
    }
 
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

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
public class MainActivity extends AppCompatActivity{
    cGPSInfo                gps;
   
    TextView                txtLat;
    TextView                txtLon;
 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        gps                 = new cGPSInfo(this);
        // GPS 사용유무 가져오기
        if (gps.isGetLocation())
        {
            double          latitude = gps.getLatitude();
            double          longitude = gps.getLongitude();
 
            txtLat          = (TextView)findViewById(R.id.lat);
            txtLon          = (TextView)findViewById(R.id.lon);
 
            txtLat.setText(String.valueOf(latitude));
            txtLon.setText(String.valueOf(longitude));
 
            Toast.makeText(this"당신의 위치 - \n위도: "+latitude+"\n경도: "+longitude, Toast.LENGTH_LONG).show();
        }
        else
        {
            // GPS를 사용할 수 없으므로
            gps.showSettingsAlert();
        }
    }
}
cs


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
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
글 보관함