如何在 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中文網其他相關文章!