SoFunction
Updated on 2025-03-02

Android LocationManager obtains geographic information such as longitude and latitude

Android LocationManager obtains geographic information such as longitude and latitude

Use LocationManager to implement positioning function

1 Real-time update of longitude and latitude

2 Obtain geographical information based on longitude and latitude (such as: country, street, etc.) (skip)

MainActivity is as follows:

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
/**
  * Demo description:
  * Use LocationManager to implement positioning function
  * 1 Real-time update of longitude and latitude
  * 2 Obtain geographical information based on longitude and latitude (such as: country, street, etc.) (skip)
  *
  *
  * Notes:
  * 0 When testing GPS positioning, it is best to be in a wider space, otherwise it will affect the positioning.
  * 1 Use (GPSProvider) to obtain Location is often null.
  * Because device positioning takes a certain amount of time, place the positioning logic in the requestLocationUpdates() method of LocationManager
  *
  * 2
  * (String provider, long minTime, float minDistance, LocationListener listener)
  * The first parameter: the location information provider, such as GPS
  * The second parameter: the time interval for updating position information, in milliseconds
  * The third parameter: the distance interval for updating position information, unit meter
  * The fourth parameter: callback when position information changes
  *
  * 3 The most important callback method in LocationListener onLocationChanged()
  * This method will be called when minTime and minDistance are both satisfied. Documentation description:
  * The minDistance parameter can also be used to control the
  * frequency of location updates. If it is greater than 0 then the
  * location provider will only send your application an update when
  * the location has changed by at least minDistance meters, AND
  * at least minTime millionseconds have passed.
  * For example, the interval time (minTime) reaches 3 seconds and the distance (minDistance) is greater than 5 meters
  * Then the method will be called.
  *
  * 4 Cancel the update of geolocation when onDestroy() in the Activity.
  *
  *
  * Permission configuration:
  * <uses-permission android:name=".ACCESS_FINE_LOCATION"/>
  * <uses-permission android:name=".ACCESS_COARSE_LOCATION"/>
  * <uses-permission android:name=".ACCESS_MOCK_LOCATION"/>
  * <uses-permission android:name=""/>
  */ 
public class MainActivity extends Activity { 
  private Context mContext; 
  private TextView mTextView; 
  private LocationManager mLocationManager; 
  private LocationListenerImpl mLocationListenerImpl; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(); 
    init(); 
    initLocationManager(mContext); 
  } 
   
  private void init(){ 
    mContext=this; 
    mTextView=(TextView) findViewById(); 
  } 
 
  private void initLocationManager(Context context){ 
     
    mLocationManager=(LocationManager)(Context.LOCATION_SERVICE); 
     
    //Get available location information Provider. That is, one or more of passive, network, and gps    List&lt;String&gt; providerList=(true); 
    for (Iterator&lt;String&gt; iterator = (); ();) { 
      String provider = (String) (); 
      ("provider="+provider); 
    } 
     
     
    //GPS is used to obtain location information    String GPSProvider=LocationManager.GPS_PROVIDER; 
    Location location=(GPSProvider); 
    if (location!=null) { 
      double longitude=(); 
      double altitude=(); 
      ("longitude="+longitude+",altitude="+altitude); 
    } else { 
      ("location==null"); 
    } 
     
    //Register location monitoring    mLocationListenerImpl=new LocationListenerImpl(); 
    (LocationManager.GPS_PROVIDER, 3000, 5, mLocationListenerImpl); 
  } 
   
   
  private class LocationListenerImpl implements LocationListener{ 
    //Call this method when the device position changes    @Override 
    public void onLocationChanged(Location location) { 
      if (location!=null) { 
        showLocation(location); 
      } 
    } 
 
    //Call this method when the state of the provider changes. For example, GPS changes from available to unavailable.    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
       
    } 
 
    //Call this method when the provider is opened. For example, the user opens GPS    @Override 
    public void onProviderEnabled(String provider) { 
       
    } 
 
    //Call this method when the provider is closed. For example, turn on GPS    @Override 
    public void onProviderDisabled(String provider) { 
       
    } 
     
  } 
   
   
  private void showLocation(Location location) { 
    // Get longitude    double longitude = (); 
    // Get latitude    double altitude = (); 
    String message="Longitude is:"+longitude+"\n"+"The latitude is:"+altitude; 
    (message); 
     
  } 
   
  @Override 
  protected void onDestroy() { 
    (); 
    if (mLocationManager!=null) { 
      (mLocationListenerImpl); 
    } 
  } 
 
 
   
 
} 

as follows:

<RelativeLayout xmlns:andro 
  xmlns:tools="/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:paddingLeft="@dimen/activity_horizontal_margin" 
  android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin" 
  tools:context=".MainActivity" > 
 
  <TextView 
    android: 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerInParent="true" 
    android:gravity="center" /> 
 
</RelativeLayout> 

If you have any questions, please leave a message or go to the community of this website to communicate and discuss. There are many articles on Android development on this website. I hope you can search and read it. I hope it can help you. Thank you for your support for this website!