如何在 Android 中获取当前位置
您在使用 Android 位置系统的 NETWORK 提供程序检索当前位置坐标时遇到问题。尽管实现了各种现有的类,但您仍然无法获取当前坐标。
问题分析:
问题可能在于您缺少一个基本概念。以下是一些潜在的问题:
已修订代码:
这是代码的修订版本,可解决潜在的问题问题:
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>
此更新的代码解决了与使用网络提供程序在 Android 中获取当前位置相关的最常见问题。
以上是为什么我无法使用 Android 的网络提供商获取我的当前位置?的详细内容。更多信息请关注PHP中文网其他相关文章!