SoFunction
Updated on 2025-03-11

Android obtains positioned location data and gps latitude and longitude through gps


package .android_test;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class MainActivity extends Activity {
private Location location=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
//Get the name of the location service
String serviceName = this.LOCATION_SERVICE;
//Add to obtain the management object of location service
LocationManager locationManager = (LocationManager)getSystemService(serviceName);
// Get positioned location data through GPS
location = (locationManager.GPS_PROVIDER);
updateToNewLocation(location);

/**Listener for service management objects*/
//Parameter 1: Positioning method Parameter 2: Monitoring update interval time (ms) Parameter 3: Monitoring update distance (m) Parameter 4: Monitoring method
    (locationManager.GPS_PROVIDER, 2000, 10, new LocationListener() {

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
        public void onProviderEnabled(String provider) {
        }
        public void onProviderDisabled(String provider) {
        }
        public void onLocationChanged(Location location) {
            updateToNewLocation(location);
        }
    });
}
private void updateToNewLocation(Location location) {

    TextView tv1;
    tv1 = (TextView) (.tv1);
    if (location != null) {
        double  latitude = ();
        double longitude= ();
("Latitude and longitude are: n"+"dimensional: " +  latitude+ "n longitude" + longitude);
    } else {
("Geographic information cannot be obtained");
    }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(, menu);
    return true;
}