Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have created a small application with step by step description to get current location's GPS coordinates.</p> <p>Complete example source code is in <em><a href="http://www.rdcworld-android.blogspot.in/2012/01/get-current-location-coordinates-city.html" rel="noreferrer">Get Current Location coordinates , City name - in Android</a></em>.</p> <hr> <p><strong>See how it works:</strong></p> <ul> <li><p>All we need to do is add this permission in the manifest file:</p> <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt; </code></pre></li> <li><p>And create a LocationManager instance like this:</p> <pre><code>LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); </code></pre></li> <li><p>Check if GPS is enabled or not.</p></li> <li><p>And then implement LocationListener and get coordinates:</p> <pre><code>LocationListener locationListener = new MyLocationListener(); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000, 10, locationListener); </code></pre></li> <li><p>Here is the sample code to do so</p></li> </ul> <hr> <pre><code>/*---------- Listener class to get coordinates ------------- */ private class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location loc) { editLocation.setText(""); pb.setVisibility(View.INVISIBLE); Toast.makeText( getBaseContext(), "Location changed: Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT).show(); String longitude = "Longitude: " + loc.getLongitude(); Log.v(TAG, longitude); String latitude = "Latitude: " + loc.getLatitude(); Log.v(TAG, latitude); /*------- To get city name from coordinates -------- */ String cityName = null; Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); List&lt;Address&gt; addresses; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); if (addresses.size() &gt; 0) { System.out.println(addresses.get(0).getLocality()); cityName = addresses.get(0).getLocality(); } } catch (IOException e) { e.printStackTrace(); } String s = longitude + "\n" + latitude + "\n\nMy Current City is: " + cityName; editLocation.setText(s); } @Override public void onProviderDisabled(String provider) {} @Override public void onProviderEnabled(String provider) {} @Override public void onStatusChanged(String provider, int status, Bundle extras) {} } </code></pre> <hr>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload