Androidã§ä½ç½®æ å ±ãæ±ãã¨ãã«ä½¿ãLocationManagerã®ä½¿ãæ¹ã®è¦ç¹ã¾ã¨ã
http://developer.android.com/training/basics/location/locationmanager.html
ãããèªãã§ã®ã¾ã¨ãã¡ã¢ãåºæ¬çã«è¦ç´ãâ»ã¯åã®ä»ã足ã

ä½ãã°ããã! Androidããã°ã©ãã³ã° 第2ç -SDK4対å¿- (Smart Mobile Developer)
- ä½è : éå®å實
- åºç社/ã¡ã¼ã«ã¼: ç¿æ³³ç¤¾
- çºå£²æ¥: 2013/04/23
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- ãã®ååãå«ãããã° (7件) ãè¦ã
Manifest
ã¾ã¨ã
ã¾ããã ãããACCESS_FINE_LOCATIONã¨INTERNETãã¼ããã·ã§ã³ãå¿ è¦ã«ãªãã£ã¦ãã¨ãã
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" />
â»æ¸ãå ´æã¯applicationã¿ã°ã¨ååãä¸ãããªããã
â»ãã¨ãéçºæã«ã¨ãã¥ã¬ã¼ã¿ã®æ¬ä¼¼æ
å ±ã使ãã¨ãã¯ACCESS_MOCK_LOCATIONãå¿
è¦ã
LocationManagerã®åç §ãåå¾
- LocationManagerãä½ç½®æ å ±ããã«ããããã®ã¡ã¤ã³ã¯ã©ã¹
- åå¾ã¯ä¸»ã«onCreate()ã§ãããâ»ã¤ã¼ãã¨ã¯Fragmentã ã£ããonActivityCreated()å ããªã»ã»ã»ã
LocationManager locationManager =
(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Location Providerãé¸æãã
- ä½ç½®æ
å ±ã®åå¾æ¹æ³ã«ãã£ã¦ãåå¾æéã»ç²¾åº¦ã»éä¿¡è²»ã»é»æ± æ¶è²»ãªã©ãå¤ãã£ã¦ããã®ã§ãããããã®ç¹æ§ãèæ
®ãã¦ä½¿ãåãããã¨â»ä»ãã¨ãGPSã¨ãããã¯ã¼ã¯ãã¼ã¹ï¼ä¸»ã«Wi-fiï¼ãããªãã¯ã
- ä¾ãã°åå¾æéã§ããã¨ãé常ã¯ç²¾åº¦ãããé«ãæ¹æ³ï¼GPSï¼ã§ããã»ã©ã精度ãä½ãæ¹æ³ï¼ãããã¯ã¼ã¯ãã¼ã¹ï¼ããåå¾æéããããã
- GPSã使ã£ãåå¾æ¹æ³ãé¸ã¶å ´åã¯ä»¥ä¸ã®ã¨ãã
LocationProvider provider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
â»éä¿¡è²»ï¼åæ:monetary cost)ã£ã¦ãªãã ãï¼ã£ã¦æã£ã¦èª¿ã¹ã¦ã¿ããã©ããã¶ããå°æ¥çã«æ´»ç¨ããIFã£ã½ãã¦ãç¾ç¶ã§ã¯èæ ®ãããªãï¼å ¨é¨costã¯ããããªãå¤å®ï¼ã«ãªã£ã¦ãããããã£ã¦ã確å®ããæ å ±ã½ã¼ã¹ã¯è¦ã¤ããããªãã¦ãStackOverflowã«æ稿ãããâæ¨æ¸¬âããã®æ¨æ¸¬ã ãããï¼
- ä¸æ¹ã§æ¡ä»¶ã«å¿ãã¦ãAndroidã«é©åãªProviderãé¸æããããã¨ãã§ãã
- 以ä¸ã¯ã精度ãé«ããéä¿¡è²»ãããããªãæ¡ä»¶è¨å®
- 常ã«æ¡ä»¶ã«ãã£ãProviderãè¿ãããã¨ã¯éããªããã¨ã«æ³¨æã
// Retrieve a list of location providers that have fine accuracy, no monetary cost, etc Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setCostAllowed(false); ... String providerName = locManager.getBestProvider(criteria, true); // If no suitable provider is found, null is returned. if (providerName != null) { ... }
Location Providerãå©ç¨ã§ããã確èªãã
- GPSã¨ãè¨å®ã§åããã¦ããã¨ãããã
- ãã®å ´åã¯ã¦ã¼ã¶ã¼ã«éç¥ãã¦ããã®ã¾ã¾GPSã®è¨å®ç»é¢ã«é£ãã¦è¡ã£ã¦ãããã¨ããããã
@Override protected void onStart() { super.onStart(); // This verification should be done during onStart() because the system calls // this method when the user returns to the activity, which ensures the desired // location provider is enabled each time the activity resumes from the stopped state. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); if (!gpsEnabled) { // Build an alert dialog here that requests that the user enable // the location services, then when the user clicks the "OK" button, // call enableLocationSettings() } } private void enableLocationSettings() { Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(settingsIntent); }
Location Listenerã®ã»ããã¢ãã
- LocationManagerã«ã¯ããã¤ãä½ç½®æ å ±ã®æ´æ°ãåãåãæ¹æ³ããã
- ä¸çªã·ã³ãã«ãªã®ã¯LocationListenerã®onLocationChanged() ã³ã¼ã«ããã¯ã¡ã½ããã§æ¾ãæ¹æ³
- æ´æ°é »åº¦ã¯æéã¨ç§»åè·é¢ã®è¤åæå®
- 以ä¸ã®ã³ã¼ãã¯æä½10ç§éãã¨ãã10ã¡ã¼ãã«ä»¥ä¸åããæã«çºç«ããè¨å® â»ã¡ãªã¿ã«ããã10ç§ãã£ããããªããçºç«ãã¨ã10ã¡ã¼ãã«åãããããªããçºç«ãã¨ããããããããªãã¦ãæ´æ°ã¿ã¤ãã³ã°ã決ãããã³ãã«ãããã ãã ãã注æã ãï¼é»æ± æ¶è²»ãæããããã«ãã¢ããªã1åã«1度æ´æ°ã§ããã°å åãã¨ããå ´åã«ï¼åã«è¡¨ç¤ºãã¦ããã°ãããããç´°ããã¿ã¤ãã³ã°ã§æ´æ°ã¯ãããªãããã¨ããæå³ãããã§ããã詳細ã¯APIドキュメントãã
private final LocationListener listener = new LocationListener() { @Override public void onLocationChanged(Location location) { // A new location update is received. Do something useful with it. In this case, // we're sending the update to a handler which then updates the UI with the new // location. Message.obtain(mHandler, UPDATE_LATLNG, location.getLatitude() + ", " + location.getLongitude()).sendToTarget(); ... } ... }; mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, // 10-second interval. 10, // 10 meters. listener);
è¤æ°ã®ä½ç½®æ å ±å ããã®æ´æ°æ å ±ããã³ããªã³ã°ãã
- æ®éã¯ç²¾åº¦ãé«ãã»ããä½ç½®æ å ±åå¾ã«æéããããã®ã§ããªãã¹ãæ©ãä½ç½®ã表示ããããã¨ããªã©ã¯ã両æ¹ã®Provider(GPSã¨ãããã¯ã¼ã¯)ãç»é²ãã¦ããã
- onLocationChanged() ã«ã¯ã両æ¹ã®æ´æ°æ å ±ããæ´æ°æéã»ç²¾åº¦ã¨ã¨ãã«éããã¦ããã®ã§ããã®ï¼ã¤ã®æ å ±ãå¤å®ãã¦æ´æ°æéãå¤ãã£ããã精度ãä½ãã£ããããæ å ±ããã¾ããã£ã«ã¿ãªã³ã°ããããã«å®è£ ãã¹ã
- ã§ããã®ããã«å®è£ ãããµã³ãã«ã以ä¸
private static final int TWO_MINUTES = 1000 * 60 * 2; /** Determines whether one Location reading is better than the current Location fix * @param location The new Location that you want to evaluate * @param currentBestLocation The current Location fix, to which you want to compare the new one */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location // because the user has likely moved if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location are from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers are the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); }
â»ãã®è¾ºã¯ãã¹ãæ¸ããªããåçµããã®ãããã¨æããï¼ãã¨ã§ãã¥ã¼ãã³ã°ã¨ããããããããªããããªï¼
getLastKnownLocation()ããã¾ã使ã
- ä½ç½®æ å ±ã®åå¾ãã¡ãã£ã¨ãå¾ ã¦ãªãç¶æ³ããããããããªãããããªã¨ãã¯getLastKnownLocation()ã使ããã
- ã§ãããã®æ å ±ã¯å¾ã ã«ãã¦å¤ããããã使ãããã©ããã¯èªåã§å¤æããªãã¨ãããªããã
- ãã使ããªãã¨å¤æãã¦ãæ°ããæ å ±ãå¾ ã¤ã®ã ã¨ããã°ãé©åãªã¡ãã»ã¼ã¸ãã¦ã¼ã¶ã¼ã«è¡¨ç¤ºãã¦ãããã
ä½ç½®æ å ±ã®æ´æ°ãæ¢ãã
- ä¸è¦ãªä½ç½®æ å ±ã®åå¾ã¯ããªããã¨ãé»æ± ã¨ãããã¯ã¼ã¯å¸¯å使ã£ã¡ãããããã
- ãã¨ãã°ã¦ã¼ã¶ã¼ãç»é¢ãå¤ããã¨ããªããã¯ä»¥ä¸ã®ããã«onStop()ã§removeUpdate()ãã³ã¼ã«ãã¦ãæ´æ°ãæ¢ããã
protected void onStop() { super.onStop(); mLocationManager.removeUpdates(listener); }
â»ç»é¢å¤ãã¦ãåå¾ãã¥ããã»ããããã¢ããªã ã£ããï¼Runkeeperã¨ãã¿ããã«ï¼ããã¯ã°ã©ã¦ã³ãå¦çã«ããããã¦å¯¾å¦ããããã
éã¸ãªã³ã¼ãã£ã³ã°ã§ä½æã®è¡¨ç¤º
- ããã¾ã§èª¬æããå 容ã§ä½ç½®æ å ±ã¯ã¨ãããã©ãæ°å¤ã§ã®æ å ±ã¯ã¦ã¼ã¶ã¼ã«ã¯ä¼ãããªããããã ããä½æã表示ãããã
- GeocoderAPIã使ãã¾ããããããã¯Webãµã¼ãã¹ã«ä¾åãã¦ããã®ã§ç¶æ³ã«ãã£ã¦ã¯ä½¿ããªãå ´åãããã®ã§æ³¨æãããã
- 以ä¸ãµã³ãã«ã³ã¼ãã ãã©ãAsyncTaskã使ããã¦ãããã¨ã«æ³¨ç®ããããgetFromLocation()ã¯ãããã¯ããHTTPéä¿¡ãè¡ãããUIã¹ã¬ããããç´æ¥å¼ããããã¡ã ãï¼â»éä¿¡ãçµäºããã¾ã§ããã¹ã¦ãããªã¼ãºãããï¼
private final LocationListener listener = new LocationListener() { public void onLocationChanged(Location location) { // Bypass reverse-geocoding if the Geocoder service is not available on the // device. The isPresent() convenient method is only available on Gingerbread or above. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && Geocoder.isPresent()) { // Since the geocoding API is synchronous and may take a while. You don't want to lock // up the UI thread. Invoking reverse geocoding in an AsyncTask. (new ReverseGeocodingTask(this)).execute(new Location[] {location}); } } ... }; // AsyncTask encapsulating the reverse-geocoding API. Since the geocoder API is blocked, // we do not want to invoke it from the UI thread. private class ReverseGeocodingTask extends AsyncTask<Location, Void, Void> { Context mContext; public ReverseGeocodingTask(Context context) { super(); mContext = context; } @Override protected Void doInBackground(Location... params) { Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); Location loc = params[0]; List<Address> addresses = null; try { // Call the synchronous getFromLocation() method by passing in the lat/long values. addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); } catch (IOException e) { e.printStackTrace(); // Update UI field with the exception. Message.obtain(mHandler, UPDATE_ADDRESS, e.toString()).sendToTarget(); } if (addresses != null &s;&s; addresses.size() > 0) { Address address = addresses.get(0); // Format the first line of address (if available), city, and country name. String addressText = String.format("%s, %s, %s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", address.getLocality(), address.getCountryName()); // Update the UI via a message handler. Message.obtain(mHandler, UPDATE_ADDRESS, addressText).sendToTarget(); } return null; } }
ã¾ã¨ãã£ã¦ããã»ã¼æ訳ã«ãªã£ãå¸ã¬ã¹ã»ã»ã»ã

ã¹ãã¼ãã«ããã°ã©ãã³ã° Androidå ¥éç·¨ 第3ç SDK4.x対å¿
- ä½è : å åå ¤
- åºç社/ã¡ã¼ã«ã¼: ãªãã¯ãã¬ã³ã
- çºå£²æ¥: 2013/05/11
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- ãã®ååãå«ãããã° (5件) ãè¦ã