如何在Android中獲取當前位置我在使用Android定位系統的網絡提供商獲取當前的位置坐標時遇到了麻煩。已經閱讀了很多教程,并為我的項目實現了4或5個現有的類,它們都給了我最后的坐標,而不是當前的坐標。我很確定這個問題是我錯過的基本問題,但我無法理解它到底是什么。我現在使用的密碼:這是我的主要活動package com.example.locationtests;import android.os.Bundle;import android.app.Activity;import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GPSTracker mGPS = new GPSTracker(this);
TextView text = (TextView) findViewById(R.id.texts);
if(mGPS.canGetLocation ){
mGPS.getLocation();
text.setText("Lat"+mGPS.getLatitude()+"Lon"+mGPS.getLongitude());
}else{
text.setText("Unabletofind");
System.out.println("Unable");
}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;}}這是我用來跟蹤的類:package com.example.locationtests;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;
import android.content.Intent;import android.location.Location;import android.location.LocationListener;
import android.location.LocationManager;import android.os.Bundle;import android.provider.Settings;
import android.util.Log;public final class GPSTracker implements LocationListener {
private final Context mContext;
// flag for GPS status
public boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;實際上,GPS定位工作得很好,但網絡定位卻不行。當我移動時,當我打開設備時,坐標上的GPS一直在變化,但當我關閉它并在NetworkProvider上中繼時,情況就不一樣了。
3 回答

忽然笑
TA貢獻1806條經驗 獲得超5個贊
LocationListener
private final LocationListener mLocationListener = new LocationListener() { @Override public void onLocationChanged(final Location location) { //your code here }};
LocationManager
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, mLocationListener);}
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

慕桂英3389331
TA貢獻2036條經驗 獲得超8個贊
- 3 回答
- 0 關注
- 493 瀏覽
添加回答
舉報
0/150
提交
取消