ํƒœ๊ทธ ๋ณด๊ด€๋ฌผ: geolocation

geolocation

Android : LocationManager ๋ฐ Google Play ์„œ๋น„์Šค LocationManager์ˆ˜์—… ์„

์‚ฌ์šฉ์ž์˜ ํ˜„์žฌ ์œ„์น˜๋ฅผ ์–ป๋Š” ๋ฐ ์ค‘์ ์„ ๋‘” ์•ฑ์„ ๋งŒ๋“ค๊ณ  Google Places API ๋ฅผ ํ†ตํ•ด ์ž์‹ ๊ณผ ๊ฐ€๊นŒ์šด ๊ด€์‹ฌ ์ง€์  (์˜ˆ : ๋ฐ”, ๋ ˆ์Šคํ† ๋ž‘ ๋“ฑ)์„ ์ฐพ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค .

์›น์„ ์‹œ์ž‘ํ•  ์žฅ์†Œ๋ฅผ ๊ฒ€์ƒ‰ ํ•  ๋•Œ LocationManager์ˆ˜์—… ์„ ์‚ฌ์šฉํ•˜๋Š” ์ผ๋ถ€ ์ž์Šต์„œ ์™€ ์‚ฌ์šฉ์ž ์œ„์น˜๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด Google Play ์„œ๋น„์Šค ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ผ๋ถ€ ์ž์Šต์„œ๋ฅผ ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ฒซ๋ˆˆ์— ๋‘ ์‚ฌ๋žŒ ๋ชจ๋‘ ๊ฐ™์€ ์ผ์„ํ•˜์ง€๋งŒ, ์ด๊ฒƒ์— ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ์•ฝ๊ฐ„ ํ˜ผ๋ž€ ์Šค๋Ÿฌ์› ์œผ๋ฉฐ ์–ด๋–ค ๋ฐฉ๋ฒ•์ด ๋‚ด ์š”๊ตฌ์— ๊ฐ€์žฅ ์ ํ•ฉํ•œ ์ง€ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‚˜๋Š” ๋‹น์‹ ์—๊ฒŒ ๋ฌป๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค :

์œ„์น˜๋ฅผ ์ฐพ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?



๋‹ต๋ณ€

Android์˜ ์‚ฌ์šฉ์ž ์œ„์น˜

์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ์‚ฌ์šฉ์ž์˜ ์œ„์น˜๋ฅผ โ€‹โ€‹์–ป๋Š” ๊ฒƒ์€ iOS๋ณด๋‹ค ๋œ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ํ˜ผ๋ž€์„ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ์™„์ „ํžˆ ๋‹ค๋ฅธ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š”์˜ Android API๋ฅผ ์‚ฌ์šฉ android.location.LocationListenerํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ๋Š” Google Play Services API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ com.google.android.gms.location.LocationListener์ž…๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€๋ฅผ ๋ชจ๋‘ ์‚ดํŽด ๋ณด์ž.

  1. ์•ˆ๋“œ๋กœ์ด๋“œ์˜ ์œ„์น˜ API

    Android์˜ ์œ„์น˜ API๋Š” ์„ธ ๊ฐ€์ง€ ๋‹ค๋ฅธ ์ œ๊ณต ์—…์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์น˜๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.

    • LocationManager.GPS_PROVIDERโ€”์ด ๊ณต๊ธ‰์ž๋Š” ์œ„์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ์กฐ๊ฑด์— ๋”ฐ๋ผ์ด ๊ณต๊ธ‰์ž๋Š” ์œ„์น˜ ์ˆ˜์ •์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • LocationManager.NETWORK_PROVIDERโ€”์ด ๊ณต๊ธ‰์ž๋Š” ์…€ ํƒ€์›Œ ๋ฐ WiFi ์•ก์„ธ์Šค ํฌ์ธํŠธ์˜ ๊ฐ€์šฉ์„ฑ์— ๋”ฐ๋ผ ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋Š” ๋„คํŠธ์›Œํฌ ์กฐํšŒ๋ฅผ ํ†ตํ•ด ๊ฒ€์ƒ‰๋ฉ๋‹ˆ๋‹ค.
    • LocationManager.PASSIVE_PROVIDERโ€”์ด ์ œ๊ณต์ž๋Š” ๋‹ค๋ฅธ ์ œ๊ณต์ž๊ฐ€ ์ƒ์„ฑ ํ•œ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด๋‚˜ ์„œ๋น„์Šค๊ฐ€ ์‹ค์ œ๋กœ ์œ„์น˜๋ฅผ ์ง์ ‘ ์š”์ฒญํ•˜์ง€ ์•Š๊ณ  ์š”์ฒญํ•˜๋ฉด ์œ„์น˜ ์—…๋ฐ์ดํŠธ๋ฅผ ์ˆ˜๋™์œผ๋กœ๋ฐ›์Šต๋‹ˆ๋‹ค.

์š”์ ์€ LocationManager์‹œ์Šคํ…œ์—์„œ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ ์™€์„œ ๊ตฌํ˜„ LocationListenerํ•˜๊ณ ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๋Š” requestLocationUpdates๊ฒƒ LocationManager์ž…๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์ฝ”๋“œ ์Šค ๋‹ˆํŽซ์ž…๋‹ˆ๋‹ค.

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
      makeUseOfNewLocation(location);
    }

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

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

์œ„์น˜ ์ „๋žต์— ๋Œ€ํ•œ Google API ๊ฐ€์ด๋“œ์ฝ”๋“œ๋ฅผ ๊ฝค ์ž˜ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ Google ์œ„์น˜ ์„œ๋น„์Šค API๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐํ„ฐ๋ฆฌ ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋˜๊ณ  ์ •ํ™•์„ฑ์ด ํ–ฅ์ƒ ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ํ˜ผ๋ž€์ด ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค!

  1. Google์˜ ์œ„์น˜ ์„œ๋น„์Šค API

Google์˜ ์œ„์น˜ ์„œ๋น„์Šค API๋Š” Google Play ์„œ๋น„์Šค APK์˜ ์ผ๋ถ€์ž…๋‹ˆ๋‹ค ( ์„ค์ • ๋ฐฉ๋ฒ• ). ๊ทธ๊ฒƒ๋“ค์€ Android API ์œ„์— ๊ตฌ์ถ•๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด API๋Š” ์œ„์—์„œ ์–ธ๊ธ‰ ํ•œ ๊ณต๊ธ‰์ž ๋Œ€์‹  โ€œํ“จ์ฆˆ ์œ„์น˜ ๊ณต๊ธ‰์žโ€๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณต๊ธ‰์ž๋Š” ์ •ํ™•๋„, ๋ฐฐํ„ฐ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ๋“ฑ์„ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์šฉํ•  ๊ธฐ๋ณธ ๊ณต๊ธ‰์ž๋ฅผ ์ž๋™์œผ๋กœ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์‹œ์Šคํ…œ ์ „์ฒด ์„œ๋น„์Šค์—์„œ ์œ„์น˜๋ฅผ ์ง€์†์ ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•˜์—ฌ ์—…๋ฐ์ดํŠธํ•˜๋ฏ€๋กœ ์†๋„๊ฐ€ ๋น ๋ฆ…๋‹ˆ๋‹ค. ์ง€์˜ค ํŽœ์‹ฑ๊ณผ ๊ฐ™์€ ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Google์˜ ์œ„์น˜ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์•ฑ์ด์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค GooglePlayServicesClient. ํด๋ผ์ด์–ธํŠธ์— ์—ฐ๊ฒฐํ•˜๋ ค๋ฉด ํ™œ๋™ (๋˜๋Š” ์กฐ๊ฐ ๋“ฑ)์„ ๊ตฌํ˜„ GooglePlayServicesClient.ConnectionCallbacksํ•˜๊ณ  GooglePlayServicesClient.OnConnectionFailedListener์ธํ„ฐํŽ˜์ด์Šค ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค . ์ƒ˜ํ”Œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    public class MyActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {
    LocationClient locationClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        locationClient = new LocationClient(this, this, this);
    }

    @Override
    public void onConnected(Bundle bundle) {
    Location location = locationClient.getLastLocation() ;
        Toast.makeText(this, "Connected to Google Play Services", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDisconnected() {
    Toast.makeText(this, "Connected from Google Play Services.", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        // code to handle failed connection
        // this code can be found here โ€” http://developer.android.com/training/location/retrieve-current.html 
    }
  • ์™œ locationClient.getLastLocation()null์ž…๋‹ˆ๊นŒ?

๋Š” locationClient.getLastLocation()ํด๋ผ์ด์–ธํŠธ์—์„œ ๋งˆ์ง€๋ง‰์œผ๋กœ ์•Œ๋ ค์ง„ ์œ„์น˜๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Fused Location Provider๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์—ฐ๊ฒฐ๋œ ๊ฒฝ์šฐ์—๋งŒ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์œ„์น˜๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์—ฐ๊ฒฐ๋˜๋ฉด ์ฆ‰์‹œ ์œ„์น˜๋ฅผ ๊ฐ€์ ธ ์˜ค๋ ค๊ณ  ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค. ๋‹น์‹ ์˜ ํ™œ๋™์ด ์—ฐ๊ฒฐํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ํด๋ผ์ด์–ธํŠธ์ด๋ฉฐ ํ˜ธ์ถœํ•˜๋ฉด getLastLocation()์ฆ‰์‹œ์—์„œ onConnected()์ฒซ ๋ฒˆ์งธ ์œ„์น˜์— ์˜ฌ ์ˆ˜์žˆ๋Š” ์ถฉ๋ถ„ํ•œ ์‹œ๊ฐ„์ด๋˜์ง€ ์•Š์„ ์ˆ˜๋„.์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค location๋˜๊ณ  null.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ๊ณต๊ธ‰์ž๊ฐ€ ์œ„์น˜ getLastLocation()๋ฅผ ํŒŒ์•…ํ•  ๋•Œ๊นŒ์ง€ (๋ถˆํ™•์‹คํ•˜๊ฒŒ) ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€์„ ํ˜ธ์ถœ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ (๋” ๋‚˜์€) ์˜ต์…˜์€ com.google.android.gms.location.LocationListener์ฃผ๊ธฐ์  ์œ„์น˜ ์—…๋ฐ์ดํŠธ๋ฅผ ์ˆ˜์‹  ํ•˜๋„๋ก ์ธํ„ฐํŽ˜์ด์Šค ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค (์ฒซ ๋ฒˆ์งธ ์—…๋ฐ์ดํŠธ๋ฅผ ๋ฐ›์œผ๋ฉด ๋„์‹ญ์‹œ์˜ค).

    public class MyActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
    // . . . . . . . . more stuff here 
    LocationRequest locationRequest;
    LocationClient locationClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // . . . . other initialization code
        locationClient = new LocationClient(this, this, this);
    locationRequest = new LocationRequest();
    // Use high accuracy
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        // Set the update interval to 5 seconds
    locationRequest.setInterval(UPDATE_INTERVAL);
        // Set the fastest update interval to 1 second
    locationRequest.setFastestInterval(FASTEST_INTERVAL);
    }
    // . . . . . . . . other methods 
    @Override
    public void onConnected(Bundle bundle) {
        Location location = locationClient.getLastLocation();
        if (location == null)
            locationClient.requestLocationUpdates(locationRequest, this);
        else
            Toast.makeText(getActivity(), "Location: " + location.getLatitude() + ", " + location.getLongitude(), Toast.LENGTH_SHORT).show();
    }
    // . . . . . . . . other methods
    @Override
    public void onLocationChanged(Location location) {
        locationClient.removeLocationUpdates(this);
        // Use the location here!!!
    }

์ด ์ฝ”๋“œ์—์„œ๋Š” ํด๋ผ์ด์–ธํŠธ์— ์ด๋ฏธ ๋งˆ์ง€๋ง‰ ์œ„์น˜ ( onConnected) ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค . ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ์œ„์น˜ ์—…๋ฐ์ดํŠธ๋ฅผ ์š”์ฒญํ•˜๊ณ  ์—…๋ฐ์ดํŠธ๋ฅผ ๋ฐ›์œผ๋ฉด onLocationChanged()์ฆ‰์‹œ ์ฝœ๋ฐฑ์œผ๋กœ ์š”์ฒญ์„ ๋•๋‹ˆ๋‹ค .

(๊ฐ€) ์ฐธ๊ณ  locationClient.requestLocationUpdates(locationRequest, this);๊ฐ€์ด ์•ˆ์— ์žˆ์–ด์•ผํ•˜๋Š” onConnected์ฝœ๋ฐฑ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋‹น์‹ ์€ ์–ป์„ ๊ฒƒ์ด๋‹ค IllegalStateException๋‹น์‹ ์€ ๊ตฌ๊ธ€ ํ”Œ๋ ˆ์ด ์„œ๋น„์Šค ํด๋ผ์ด์–ธํŠธ์— ์—ฐ๊ฒฐํ•˜์ง€ ์•Š๊ณ  ์œ„์น˜์— ๋Œ€ํ•œ ์š”์ฒญํ•˜๋ ค๊ณ  ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—.

  • ์‚ฌ์šฉ์ž๊ฐ€ ์œ„์น˜ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉ ์ค‘์ง€ํ–ˆ์Šต๋‹ˆ๋‹ค

๋งŽ์€ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž๋Š” ๋ฐฐํ„ฐ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•˜๊ฑฐ๋‚˜ ๊ฐœ์ธ ์ •๋ณด ๋ณดํ˜ธ๋ฅผ ์œ„ํ•ด ์œ„์น˜ ์„œ๋น„์Šค๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์œ„์˜ ์ฝ”๋“œ๋Š” ์—ฌ์ „ํžˆ ์œ„์น˜ ์—…๋ฐ์ดํŠธ๋ฅผ ์š”์ฒญํ•˜์ง€๋งŒ onLocationChangedํ˜ธ์ถœ๋˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์œ„์น˜ ์„œ๋น„์Šค๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ์š”์ฒญ์„ ์ค‘์ง€ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•ฑ์—์„œ ์œ„์น˜ ์„œ๋น„์Šค๋ฅผ ํ™œ์„ฑํ™”ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ๋ฉ”์‹œ์ง€ ๋˜๋Š” ํ† ์ŠคํŠธ๋ฅผ ํ‘œ์‹œํ•˜๋ ค๊ณ ํ•ฉ๋‹ˆ๋‹ค. ์•ˆํƒ€๊น๊ฒŒ๋„ ์‚ฌ์šฉ์ž๊ฐ€ Google์˜ ์œ„์น˜ ์„œ๋น„์Šค API์—์„œ ์œ„์น˜ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉ ์ค‘์ง€ํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” Android API๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๋‹น์‹ ์˜ onCreate๋ฐฉ๋ฒ•์—์„œ :

    LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
    locationEnabled = false;
    Toast.makeText(getActivity(), "Enable location services for accurate data", Toast.LENGTH_SHORT).show();
}
else locationEnabled = true;

๊ทธ๋ฆฌ๊ณ  ๋ฉ”์†Œ๋“œ locationEnabled์—์„œ ๋‹ค์Œ onConnected๊ณผ ๊ฐ™์ด ํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค .

    if (location != null) {
    Toast.makeText(getActivity(), "Location: " + location.getLatitude() + ", " + location.getLongitude(), Toast.LENGTH_SHORT).show();
}
else if (location == null && locationEnabled) {
    locationClient.requestLocationUpdates(locationRequest, this);
}

์ตœ์‹  ์ •๋ณด

๋Œ€ํ™” ์ƒ์ž์—์„œ ํ•œ ๋ฒˆ์˜ ํด๋ฆญ์œผ๋กœ ๋ฌธ์„œ๊ฐ€ ์—…๋ฐ์ดํŠธ๋˜๊ณ  LocationClient๊ฐ€ ์ œ๊ฑฐ๋˜๊ณ  API๊ฐ€ GPS๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋„๋ก ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
    // All location settings are satisfied. The client can initialize
    // location requests here.
    // ...
}
});

task.addOnFailureListener(this, new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        if (e instanceof ResolvableApiException) {
            // Location settings are not satisfied, but this can be fixed
            // by showing the user a dialog.
            try {
                // Show the dialog by calling startResolutionForResult(),
                // and check the result in onActivityResult().
                ResolvableApiException resolvable = (ResolvableApiException) e;
                resolvable.startResolutionForResult(MainActivity.this,
                        REQUEST_CHECK_SETTINGS);
            } catch (IntentSender.SendIntentException sendEx) {
                // Ignore the error.
            }
        }
    }
});

๋งํฌ https://developer.android.com/training/location/change-location-settings#prompt

์ƒˆ๋กœ์šด ์œ„์น˜ ํด๋ผ์ด์–ธํŠธ : FusedLocationProviderClient

  private FusedLocationProviderClient fusedLocationClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}

์œ„์น˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „์— https://developer.android.com/training/location ์„ ๋ฐฉ๋ฌธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค .


๋‹ต๋ณ€

๋‚ด ๊ฒฝํ—˜์ƒ โ€œ์ ์ ˆํ•œ ์ •ํ™•๋„โ€๊ฐ€ ๋” ์ข‹์€ ์˜๋ฏธ๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ๋‚ด๊ฐ€ ๋น ์ง„ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋ฉด GPS๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด LocationManager๊ฐ€ ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์•ฑ์œผ๋กœ ์ฐจ๋Ÿ‰์„ ์ถ”์ ํ•˜๋ฉฐ, ๋ˆ„๋ฝ ๋œ ๋ถ€๋ถ„์ด์—†๋Š” ํ•œ Google Play ์„œ๋น„์Šค๋Š” ๋งค์šฐ ๋ถ€์ •ํ™• ํ•œ ์œ„์น˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.


๋‹ต๋ณ€

LocationManager ๋Œ€์‹  Google Play ์„œ๋น„์Šค ์œ„์น˜ API๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด :

Google Play ์„œ๋น„์Šค ์œ„์น˜ API๋Š” ์•ฑ์— ์œ„์น˜ ์ธ์‹์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ Android ํ”„๋ ˆ์ž„ ์›Œํฌ ์œ„์น˜ API (android.location)๋ณด๋‹ค ์„ ํ˜ธ๋ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ Android ํ”„๋ ˆ์ž„ ์›Œํฌ ์œ„์น˜ API๋ฅผ ์‚ฌ์šฉ์ค‘์ธ ๊ฒฝ์šฐ ๊ฐ€๋Šฅํ•œ ๋นจ๋ฆฌ Google Play ์„œ๋น„์Šค ์œ„์น˜ API๋กœ ์ „ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

์ „ํ™˜ํ•ด์•ผํ•˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด Google์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋งํ•ฉ๋‹ˆ๋‹ค.

Google Play ์„œ๋น„์Šค์˜ ์ผ๋ถ€์ธ Google ์œ„์น˜ ์„œ๋น„์Šค API๋Š” ์œ„์น˜ ์ œ๊ณต ์—…์ฒด, ์‚ฌ์šฉ์ž ์ด๋™ ๋ฐ ์œ„์น˜ ์ •ํ™•๋„๋ฅผ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š”๋ณด๋‹ค ๊ฐ•๋ ฅํ•œ ๊ณ ๊ธ‰ ํ”„๋ ˆ์ž„ ์›Œํฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ œ๊ณตํ•˜๋Š” ์ „๋ ฅ ์†Œ๋น„ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์œ„์น˜ ์—…๋ฐ์ดํŠธ ์˜ˆ์•ฝ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์œ„์น˜ ์„œ๋น„์Šค API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐํ„ฐ๋ฆฌ ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋˜๊ณ  ์ •ํ™•๋„๊ฐ€ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค.


๋‹ต๋ณ€

๊ฝค ์˜ค๋žซ๋™์•ˆ Google ์œ„์น˜ ์„œ๋น„์Šค API๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์น˜ ๊ฒฐ์ •์„์œ„ํ•œ ์—ฌ๋Ÿฌ ์†Œ์Šค๋ฅผ ๊ฐ–๋Š” ๋ณต์žก์„ฑ์„ ์บก์Šํ™”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ทธ๊ฒƒ์€ ๋„ˆ๋ฌด ๋ฌด๊ฒ๊ฒŒ ์บก์Šํ™” ๋˜์–ด ์ด์ƒํ•œ ์œ„์น˜๋ฅผ ์–ป์„ ๋•Œ ๊ทธ ์ด์ƒํ•œ ์œ„์น˜๊ฐ€ ์–ด๋””์—์„œ ์™”๋Š”์ง€ ๊ฒฐ์ •ํ•  ๋ฐฉ๋ฒ•์ด ์—†์Šต๋‹ˆ๋‹ค.

์‹ค์ƒํ™œ์—์„œ ๋‚˜๋Š” ์‹ค์ œ ์œ„์น˜์—์„œ 10 ํ‚ฌ๋กœ๋ฏธํ„ฐ ๋–จ์–ด์ง„ ๋ช‡ ๊ฐ€์ง€ ๊ดด๋ฌผ ๊ฐ’์ด ๋‚˜ํƒ€๋‚ฌ์Šต๋‹ˆ๋‹ค. ์œ ์ผํ•œ ์„ค๋ช…์€ ์ด๋Ÿฌํ•œ ๋ฏธ์นœ ์œ„์น˜๋Š” Google Wi-Fi ๋˜๋Š” NWK ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์˜ค๋ฅ˜์—์„œ ๋น„๋กฏ๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Wi-Fi ๋ฐ ๋„คํŠธ์›Œํฌ ํ† ํด๋กœ์ง€๋Š” ๋งค์ผ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— ํ•ญ์ƒ ์กด์žฌํ•˜๋Š” ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ถˆํ–‰ํžˆ๋„ (๊ทธ๋ฆฌ๊ณ  ๋†€๋ž๊ฒŒ๋„) API๋Š” ๊ฐœ๋ณ„ ์œ„์น˜๊ฐ€ ์–ด๋–ป๊ฒŒ ๋„์ถœ๋˜์—ˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์†๋„, ๊ฐ€์†๋„, ๋ฒ ์–ด๋ง ๋“ฑ์˜ ํƒ€๋‹น์„ฑ ๊ฒ€์‚ฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ดด๋ฌผ ๊ฐ’์„ ํ•„ํ„ฐ๋ง ํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

โ€ฆ ๋˜๋Š” ์ข‹์€ ์˜ค๋ž˜๋œ ํ”„๋ ˆ์ž„ ์›Œํฌ API๋กœ ๋Œ์•„๊ฐ€์„œ GPS ๋งŒ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค .Google์ด ์œตํ•ฉ API๋ฅผ ๊ฐœ์„  ํ•  ๋•Œ๊นŒ์ง€ํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.


๋‹ต๋ณ€

Google Play ์„œ๋น„์Šค์˜ ์ผ๋ถ€์ธ Google ์œ„์น˜ ์„œ๋น„์Šค API ๋Š” ์œ„์น˜ ์ œ๊ณต ์—…์ฒด , ์‚ฌ์šฉ์ž ์ด๋™ ๋ฐ ์œ„์น˜ ์ •ํ™•๋„ ๋ฅผ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š”๋ณด๋‹ค ๊ฐ•๋ ฅํ•œ ๊ณ ๊ธ‰ ํ”„๋ ˆ์ž„ ์›Œํฌ๋ฅผ ์ œ๊ณต ํ•ฉ๋‹ˆ๋‹ค . ๋˜ํ•œ ์ œ๊ณตํ•˜๋Š” ์ „๋ ฅ ์†Œ๋น„ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์œ„์น˜ ์—…๋ฐ์ดํŠธ ์˜ˆ์•ฝ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์œ„์น˜ ์„œ๋น„์Šค API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐํ„ฐ๋ฆฌ ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ ๋˜๊ณ  ์ •ํ™•๋„๊ฐ€ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค.

๋‘ ๊ฐ€์ง€ API Google Play ์„œ๋น„์Šค ์œ„์น˜ API ์™€ Android ํ”„๋ ˆ์ž„ ์›Œํฌ ์œ„์น˜ API์˜ ์ž์„ธํ•œ ์ฐจ์ด์ ์€ ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .


๋‹ต๋ณ€

์˜ˆ, Google Play ์„œ๋น„์Šค ์œ„์น˜ ์„œ๋น„์Šค API๋Š” ๋งค์šฐ ์ž˜๋ชป๋œ ์œ„์น˜ ์ •๋ณด๋ฅผ ์ œ๊ณต ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. WiFi ๋ชจ๋Ž€์ด ์ด๋™ํ•˜๊ณ  WiFi ๋ชจ๋Ž€์ด ์ž˜๋ชป๋œ ์œ„์น˜ ์ •๋ณด๋กœ ์—…๋ฐ์ดํŠธ๋ฉ๋‹ˆ๋‹ค (์˜ˆ : WiFi ๋ชจ๋Ž€์˜ ์œ„์น˜๋ฅผ โ€‹โ€‹์—…๋ฐ์ดํŠธํ•˜๋Š” Android ์žฅ์น˜์— ์˜ํ•ด ์œ„์น˜๊ฐ€ ์Šคํ‘ธํ•‘ ๋œ ๊ฒฝ์šฐ) WiFi ๋ชจ๋Ž€ ์‚ผ๊ฐ ์ธก๋Ÿ‰์—์„œ ์ž˜๋ชป๋œ ์œ„์น˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ดˆ๋ž˜ํ•  ์ˆ˜์žˆ๋Š” ๋‹ค๋ฅธ ์ƒํ™ฉ์ด ๋งŽ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์œ„์น˜๊ฐ€ ํ•„์ˆ˜ ์ธ ๋ชจ๋“  ์•ฑ์—์„œ GPS ๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.


๋‹ต๋ณ€

GPS ์„œ๋น„์Šค๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœํ•˜๋Š” ๋‘ ๊ฐ€์ง€ API Google Play ์„œ๋น„์Šค ์œ„์น˜ API์™€ Android ํ”„๋ ˆ์ž„ ์›Œํฌ ์œ„์น˜ API์˜ ์ฐจ์ด์ 

FusedLocationProviderClient

  1. ์ฒซ ๋ฒˆ์งธ ๊ฐ€์ ธ ์˜ค๊ธฐ์˜ ๊ฒฝ์šฐ ์œ„์น˜๋Š” null์ด ์•„๋‹ˆ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค (์˜ˆ : ์ผ๋ถ€ ๋‹ค๋ฅธ ์•ฑ์€ GoogleplayService ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ Lastknown ์œ„์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. null ์ธ ๊ฒฝ์šฐ ํ•ด๊ฒฐํ•ด์•ผ ํ•จ)
  2. ๋‹ค์Œ ์ˆœ์ฐจ Fetch์˜ ๊ฒฝ์šฐ requestLocationUpdates()๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉ ํ•˜์—ฌ ์œ„์น˜๋ฅผ ํŽ˜์น˜ํ•ฉ๋‹ˆ๋‹ค.
  3. ์œ„์น˜ ๊ฐ€์ ธ ์˜ค๊ธฐ์—๋งŒ ๊ธฐ๋ฐ˜์œผ๋กœ locationRequest.setInterval(milliseconds)ํ•˜๊ณ  setFastestInterval(milliseconds)๊ธฐ๋ฐ˜์œผ๋กœํ•˜์ง€, ์‚ฌ์šฉ์ž ์œ„์น˜ ๋ณ€๊ฒฝ
  4. ๋ฆฌํ„ด ๊ฐ’ LatLng๋ฅผ ํฌํ•จ ๊ฐ–๋Š”๋‹ค ๋‹จ์ง€ 7 ์ง„์ˆ˜ ๊ฐ’ : (11.9557996, 79.8234599 ์˜ˆ) ์ •ํ™•ํ•˜์ง€ ๋“ฑ์„
  5. ์•ฑ ์š”๊ตฌ ์‚ฌํ•ญ์—์„œ ํ˜„์žฌ ์œ„์น˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฌด์‹œํ•  ์ˆ˜์žˆ๋Š” ์ •ํ™•๋„ (50-100 ๋ฏธํ„ฐ)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๊ถŒ์žฅ
  6. ๋ฐฐํ„ฐ๋ฆฌ ์‚ฌ์šฉ์— ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค.

์œ„์น˜ ๊ด€๋ฆฌ์ž API

  1. locationManager.requestLocationUpdates ()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์œ„์น˜ ๊ฐ€์ ธ ์˜ค๊ธฐ๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
  2. ์‚ฌ์šฉ์ž ์œ„์น˜ ๋ณ€๊ฒฝ ๋ฐ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์œ„์น˜ ๊ฐ€์ ธ ์˜ค๊ธฐ locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, milliseconds, mindistance, Mylocationlistener)

  3. ๋ฐ˜ํ™˜ ๋œ LatLng ๊ฐ’์—๋Š” ์†Œ์ˆ˜์  ์ดํ•˜ 14 ๊ฐœ์˜ ๊ฐ’ (์˜ˆ : 11.94574594963342 79.81166719458997)์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  4. ๋ฏธํ„ฐ ๋‹จ์œ„์˜ ์ •ํ™•๋„๊ฐ€ ๋” ํ•„์š”ํ•œ ์œ„์น˜ ๊ธฐ๋ฐ˜ ์•ฑ์— ๊ถŒ์žฅ ๋ฉ๋‹ˆ๋‹ค.
  5. ๋ฐฐํ„ฐ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์€ ๊ฐ€์ ธ ์˜ค๊ธฐ ๊ฐ„๊ฒฉ๊ณผ ๊ฐ€์ ธ ์˜ค๊ธฐ ๊ฑฐ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœํ•ฉ๋‹ˆ๋‹ค.

์ด ๊ธ€์€ Android ์นดํ…Œ๊ณ ๋ฆฌ์— ๋ถ„๋ฅ˜๋˜์—ˆ๊ณ  , , ํƒœ๊ทธ๊ฐ€ ์žˆ์œผ๋ฉฐ ๋‹˜์— ์˜ํ•ด ์— ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.