¾È±Ô °øºÎ¹æ

Android > À§Ä¡Á¤º¸ - À§Ä¡ Á¤º¸¸¦ ±¸ÇÏ´Â ¹æ¹ý

µî·ÏÀÏ : 2017-07-06 17:55 Á¶È¸¼ö : 52,961

À̹ø Æ÷½ºÆ®¿¡¼­´Â ¾Èµå·ÎÀÌµå ¾ÛÀÌ À§Ä¡ Á¤º¸¸¦ ¾î¶»°Ô ±¸ÇÒ ¼ö ÀÖ´ÂÁö ¾Ë¾Æº¾´Ï´Ù

1. ¸ÕÀú À§Ä¡ Á¤º¸¸¦ ±¸Çϱâ À§ÇÑ ±ÇÇÑÀ» ¼³Á¤ÇÕ´Ï´Ù.
		<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >    </uses-permission>
		<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" >    </uses-permission>


2. LocationListener ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇϴ Ŭ·¡½º¸¦ Á¤ÀÇÇϼ¼¿ä.

ÀÌ Å¬·¡½º´Â À§Ä¡ Á¤º¸¸¦ À§Ä¡ °ø±ÞÀڷκÎÅÍ Áö¼ÓÀûÀ¸·Î ¹Þ¾Æ¿À´Â ¿ªÇÒÀ» ÇÕ´Ï´Ù. ¿À¹ö¶óÀ̵å ÇØ¾ß ÇÏ´Â ¸Þ¼ÒµåµéÀº ´ÙÀ½°ú °°½À´Ï´Ù.


void onLocationChanged(Location location)
. À§Ä¡ Á¤º¸¸¦ °¡Á®¿Ã ¼ö ÀÖ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù.
. À§Ä¡ À̵¿À̳ª ½Ã°£ °æ°ú µîÀ¸·Î ÀÎÇØ È£ÃâµË´Ï´Ù.
. ÃֽŠÀ§Ä¡´Â location ÆĶó¸ÞÅÍ°¡ °¡Áö°í ÀÖ½À´Ï´Ù.
. ÃֽŠÀ§Ä¡¸¦ °¡Á®¿À·Á¸é, location ÆĶó¸ÞÅ͸¦ ÀÌ¿ëÇÏ½Ã¸é µË´Ï´Ù.

void onProviderDisabled(String provider)
. À§Ä¡ °ø±ÞÀÚ°¡ »ç¿ë ºÒ°¡´ÉÇØÁú(disabled) ¶§ È£Ã⠵˴ϴÙ.
. ´Ü¼øÈ÷ À§Ä¡ Á¤º¸¸¦ ±¸ÇÑ´Ù¸é, Äڵ带 ÀÛ¼ºÇÏ½Ç ÇÊ¿ä´Â ¾ø½À´Ï´Ù.

void onProviderEnabled(String provider)
. À§Ä¡ °ø±ÞÀÚ°¡ »ç¿ë °¡´ÉÇØÁú(enabled) ¶§ È£Ã⠵˴ϴÙ.
. ´Ü¼øÈ÷ À§Ä¡ Á¤º¸¸¦ ±¸ÇÑ´Ù¸é, Äڵ带 ÀÛ¼ºÇÏ½Ç ÇÊ¿ä´Â ¾ø½À´Ï´Ù.

void onStatusChanged(String provider, int status, Bundle extras)
. À§Ä¡ °ø±ÞÀÚÀÇ »óÅ°¡ ¹Ù²ð ¶§ È£Ã⠵˴ϴÙ.
. ´Ü¼øÈ÷ À§Ä¡ Á¤º¸¸¦ ±¸ÇÑ´Ù¸é, Äڵ带 ÀÛ¼ºÇÏ½Ç ÇÊ¿ä´Â ¾ø½À´Ï´Ù.
 
			LocationListener locationListener = new LocationListener() {
				
				@Override
				public void onLocationChanged(Location location) {
				}

				@Override
				public void onProviderDisabled(String provider) {
				}

				@Override
				public void onProviderEnabled(String provider) {
				}

				@Override
				public void onStatusChanged(String provider, int status, Bundle extras) {
				}

			}; 



3. onLocationChanged ¸Þ¼Òµå¿¡¼­ À§Ä¡ Á¤º¸¸¦ ÀÐ¾î ¿À¼¼¿ä. location ÆĶó¸ÞÅͷκÎÅÍ ÀÐÀ¸½Ã¸é µË´Ï´Ù.

Location °´Ã¼·ÎºÎÅÍ À§µµ(latitude)¿Í °æµµ(longitude)¸¦ ºñ·ÔÇÑ À§Ä¡ Á¤º¸¸¦ ±¸ÇÒ ¼ö ÀÖ½À´Ï´Ù.
		double latitude = location.getLatitude();
		double longitude = location.getLongitude(); 



»ç¿ë¿¹)
 
		private Location lastKnownLocation = null ;
		
		private LocationListener locationListener = new LocationListener() {

			@Override
			public void onLocationChanged(Location location) {
				LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
				// Get the last location.
				lastKnownLocation = location;
			}

			@Override
			public void onProviderDisabled(String provider) {}

			@Override
			public void onProviderEnabled(String provider) {}

			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {}
		}; 
		


4. ÄÁÅؽºÆ®·ÎºÎÅÍ À§Ä¡ °ü¸®ÀÚ¸¦ ±¸Çϼ¼¿ä.

¾×ƼºñƼ Ŭ·¡½º ³»¿¡¼­ À§Ä¡ °ü¸®ÀÚ¸¦ ±¸ÇÑ´Ù°í °¡Á¤ÇÕ´Ï´Ù.

±×·¯¸é ¾×ƼºñƼÀÇ »óÀ§ Ŭ·¡½ºÀÎ Context Ŭ·¡½ºÀÇ getSystemService ¸Þ¼Òµå¸¦ È£ÃâÇÏ¸é µÇ´Â °ÍÀÌÁÒ.
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 


5. À§Ä¡ °ü¸®ÀÚ¿¡°Ô À§Ä¡ÀÇ °»½ÅÀ» ¿äûÇϼ¼¿ä.
ÇÊ¿äÇÑ ¸Þ¼Òµå´Â LocationManagerÀÇ requestLocationUpdatesÀÔ´Ï´Ù. ÆĶó¸ÞÅÍ´Â ´ÙÀ½°ú °°½À´Ï´Ù.

String provider
. À§Ä¡ °ø±ÞÀÚ¸¦ ´ëÀÔÇϼ¼¿ä.
. Âü°í·Î ÁöÁ¤ °¡´ÉÇÑ °ªµéÀº ´ÙÀ½°ú °°½À´Ï´Ù.
LocationManager.GPS_PROVIDER
LocationManager.NETWORK_PROVIDER
LocationManager.PASSIVE_PROVIDER

long minTime
. À§Ä¡ °»½ÅÇϴµ¥ ÇÊ¿äÇÑ ÃÖ¼Ò ½Ã°£ °£°ÝÀÔ´Ï´Ù. ´ÜÀ§´Â millisecondÀÔ´Ï´Ù.

float minDistance
. À§Ä¡¸¦ °»½ÅÇϴµ¥ ÇÊ¿äÇÑ ÃÖ¼Ò °Å¸®ÀÔ´Ï´Ù. ´ÜÀ§´Â ¹ÌÅÍÀÔ´Ï´Ù.

LocationListener listener
. À§Ä¡ °»½ÅÀ» ó¸®ÇÒ ¸®½º³ÊÀÔ´Ï´Ù. 2¿¡¼­ ±¸ÇöÇÑ ¸®½º³Ê °´Ã¼ÀÇ À̸§À» ´ëÀÔÇØ ÁÖ½Ã¸é µÇ°Ú½À´Ï´Ù.


À§Ä¡ °ø±ÞÀÚ°¡ GPSÀÌ°í, °»½Å¿¡ ÇÊ¿äÇÑ ÃÖ¼Ò ½Ã°£ °£°ÝÀÌ 1ÃÊ(1000 milliseconds)À̸ç,

ÃÖ¼Ò °Å¸®°¡ 10¹ÌÅÍÀÎ À§Ä¡ °»½ÅÀ» ¿ä±¸ÇÑ´Ù°í °¡Á¤ÇØ º¾½Ã´Ù. ÄÚµå´Â ´ÙÀ½°ú °°½À´Ï´Ù.

¸®½º³Ê´Â 2¿¡¼­ ±¸ÇöÇÑ °ÍÀ» listener ÆĶó¸ÞÅÍ¿¡ ´ëÀÔÇÏ¿´½À´Ï´Ù.
 
		lm.requestLocationUpdates(
			LocationManager.GPS_PROVIDER,
			1000,
			10,
			locationListener
		); 
		



6. À§Ä¡ °»½ÅÀÌ ´õ ÀÌ»ó ÇÊ¿äÇÏÁö ¾ÊÀ¸¸é, LocationManagerÀÇ removeUpdates ¸Þ¼Òµå¸¦ È£ÃâÇϼ¼¿ä. ÆĶó¸ÞÅÍ´Â ´ÙÀ½°ú °°½À´Ï´Ù.

LocationListener listener
. À§Ä¡ °»½ÅÀ» ó¸®ÇÏ´ø ¸®½º³ÊÀÔ´Ï´Ù. ÀÌ°ÍÀ» ´ëÀÔÇØ¾ß À§Ä¡ °»½ÅÀÌ Áߴܵ˴ϴÙ.


À§Ä¡ Á¤º¸¸¦ °¡Á®¿Â ÈÄ ¹Ù·Î °»½Å Áß´ÜÀ» ¿øÇÑ´Ù¸é, ´ÙÀ½°ú °°ÀÌ Äڵ带 ÀÛ¼ºÇÏ½Ã¸é µË´Ï´Ù.
 
		LocationListener locationListener = new LocationListener() {

			@Override
			public void onLocationChanged(Location location) {
				LocationManager lm = (LocationManager)getSystemService(Context. LOCATION_SERVICE);

				// Get the last location.
				lastKnownLocation = location;

				// Stop the update as soon as get the location.
				lm.removeUpdates(locationListener);
			}

			@Override
			public void onProviderDisabled(String provider) { }


			@Override
			public void onProviderEnabled(String provider) {}

			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) { }
		}; 




¾Æ·¡ ¿¹Á¦´Â ¹öÆ°À» ´­·¯ GPS ·Î ºÎÅÍ À§µµ/°æµµ °ªÀ» »Ì¾Æ Toast ·Î º¸¿©ÁÖ´Â ¿¹Á¦ ÀÔ´Ï´Ù.

 
	import android.app.Activity;
	import android.content.Context;
	import android.location.Location;
	import android.location.LocationListener;
	import android.location.LocationManager;
	import android.os.Bundle;
	import android.util.Log;
	import android.view.View;
	import android.view.View.OnClickListener;
	import android.widget.Button;
	import android.widget.Toast;

	public class GpsLocation extends Activity {
		
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.gpslocation);
			
			Button button01 = (Button) findViewById(R.id.button01);
			button01.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					
					// startLocationService
					startLocationService();
					
				}
			});
				   
		}
		
		
		private void startLocationService() {
			
			// get manager instance
			LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
			
			// set listener
			GPSListener gpsListener = new GPSListener();
			long minTime = 10000; 
			float minDistance = 0; 
			
			manager.requestLocationUpdates(
						LocationManager.GPS_PROVIDER,
						minTime, 
						minDistance, 
						gpsListener);
			
			Toast.makeText(getApplicationContext(), "Location Service started.\nyou can test using DDMS.", 2000).show();
		}
		
		
		private class GPSListener implements LocationListener {

			public void onLocationChanged(Location location) {
				//capture location data sent by current provider
				Double latitude = location.getLatitude();
				Double longitude = location.getLongitude();
				
				String msg = "Latitude : "+ latitude + "\nLongitude:"+ longitude;
				Log.i("GPSLocationService", msg);
				Toast.makeText(getApplicationContext(), msg, 2000).show();
				
			}

			public void onProviderDisabled(String provider) {
			}

			public void onProviderEnabled(String provider) {
			}

			public void onStatusChanged(String provider, int status, Bundle extras) {
			}

		}    
		
	}

¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.