乾杯,我想通過Android獲取當前的GPS位置。我也跟着this Tutorial和Vogellas article。雖然它不起作用。使用LocationManager.NETWORK_PROVIDER時,無論我站在何處,我總是可以獲得51的經度和9的經度。當使用LocationManager.GPS_PROVIDER時,我一無所獲。如何獲得Android的GPS位置
儘管使用GMaps時一切正常:S不知道爲什麼。我怎樣才能獲得像GMaps一樣的當前GPS位置?
這裏是我的代碼:
package com.example.gpslocation;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
public class GPS_Location extends Activity implements LocationListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps__location);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.gps__location, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
int latitude = (int) (location.getLatitude());
int longitude = (int) (location.getLongitude());
Log.i("Geo_Location", "Latitude: " + latitude + ", Longitude: " + longitude);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
對於更高級別,更容易的方式來獲得GPS定位,你可以使用這個庫:https://github.com/delight-im/Android-SimpleLocation – caw 2015-03-04 22:19:20
檢查http:// stackoverflow。問題/ 1513485 /如何獲得當前的GPS位置編程在Android/38897436#38897436 – 2016-08-11 13:13:59