How to Get Current Location in Android
You are having trouble retrieving your current position coordinates using the NETWORK provider of the Android location system. Despite implementing various existing classes, you are still unable to obtain the current coordinates.
Problem Analysis:
The issue may lie in a fundamental concept that you are missing. Here are some potential problems:
Revised Code:
Here is a revised version of your code that addresses the potential problems:
MainActivity.java:
import android.Manifest; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private static final int REQUEST_FINE_LOCATION = 123; private LocationManager locationManager; private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.texts); // Check and request location permission if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_FINE_LOCATION); } else { initLocationManager(); } } private void initLocationManager() { locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); // Request location updates from the NETWORK provider if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); } } private LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { if (location != null) { text.setText("Lat: " + location.getLatitude() + ", Lon: " + location.getLongitude()); } } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == REQUEST_FINE_LOCATION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { initLocationManager(); } } }
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.locationtests"> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
This updated code addresses the most common issues related to getting current location in Android using the NETWORK provider.
The above is the detailed content of Why Am I Unable to Get My Current Location Using Android\'s NETWORK Provider?. For more information, please follow other related articles on the PHP Chinese website!