A simple example of implementing GPS positioning in Android

  • 2020-06-01 10:59:55
  • OfStack

Today, I took more than an hour to write a small example of GPS to obtain the code of geographical location, including referring to some code on the Internet, and made some modifications to the code. I hope you can help me. The specific code is as follows: to use GPS devices of Adnroid platform, you need to add permissions first, so you need to add the following permissions:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

The specific implementation code is as follows:

First, determine whether the GPS module exists or is opened:

private void openGPSSettings() {
    LocationManager alm = (LocationManager) this
    if (alm
        .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
      Toast.makeText(this, "GPS The module is normal ", Toast.LENGTH_SHORT)

    Toast.makeText(this, " Please open the GPS ! ", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
    startActivityForResult(intent,0); // This is set to return to the get interface after completion 


If it is opened normally, it will go directly to the display page; if it is not opened normally, it will go to the GPS setting page:

Get the code as follows:

private void getLocation()
    //  Get location management services 
    LocationManager locationManager;
    String serviceName = Context.LOCATION_SERVICE;
    locationManager = (LocationManager) this.getSystemService(serviceName);
    //  Find service information 
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE); //  High precision 
    criteria.setPowerRequirement(Criteria.POWER_LOW); //  Low power consumption 

    String provider = locationManager.getBestProvider(criteria, true); //  To obtain GPS information 
    Location location = locationManager.getLastKnownLocation(provider); //  through GPS To obtain position 
    //  Set the listener to automatically update at minimum intervals N seconds (1 Seconds for 1*1000 I wrote it this way mainly for convenience ) Or the minimum displacement change exceeds N m 
    locationManager.requestLocationUpdates(provider, 100 * 1000, 500,
        locationListener);  }

Here you can get the location information, but you still need to display it, so use the following method to display it:

private void updateToNewLocation(Location location) {

    TextView tv1;
    tv1 = (TextView) this.findViewById(R.id.tv1);
    if (location != null) {
      double latitude = location.getLatitude();
      double longitude= location.getLongitude();
      tv1.setText(" Latitude: " + latitude+ "\n longitude " + longitude);
    } else {
      tv1.setText(" Unable to obtain geographic information ");


This will be able to get the current user's location, at least how to achieve on the map, below will be obtained, and displayed! Thanks to those who reference the code!

Related articles: