Android Can';无法获取当前设备位置
我正在尝试获取我的android手机的当前位置,并在祝酒词中显示经度和纬度。这是我写的一个函数。在调试代码时,我看到控件从未进入onLocationChanged函数 从下面的android文档来看,当我调用“locationMgr.requestLocationUpdates”时,它应该调用回调函数onLocationChanged。但这在我的代码中似乎没有发生。 我检查了我的手机是否打开了GPS。我无法找出以下代码中的错误。请帮忙Android Can';无法获取当前设备位置,android,latitude-longitude,locationmanager,Android,Latitude Longitude,Locationmanager,我正在尝试获取我的android手机的当前位置,并在祝酒词中显示经度和纬度。这是我写的一个函数。在调试代码时,我看到控件从未进入onLocationChanged函数 从下面的android文档来看,当我调用“locationMgr.requestLocationUpdates”时,它应该调用回调函数onLocationChanged。但这在我的代码中似乎没有发生。 我检查了我的手机是否打开了GPS。我无法找出以下代码中的错误。请帮忙 public void getCurrentLocat
public void getCurrentLocation(){
LocationManager locationMgr;
locationMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// A new location update is received. Do something useful with it
String latitude = "latitude: " + location.getLatitude();
String longitude = "longitude: " + location.getLongitude();
String toastString = "location is" + latitude + "," +longitude;
Toast.makeText( getApplicationContext(),toastString,Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
// No code here
}
@Override
public void onProviderEnabled(String provider) {
// No code here
}
@Override
public void onStatusChanged(String provider, int status,Bundle extras)
{
// No code here
}
};
locationMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, listener);
}
我的清单文件中还有以下两行
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
谢谢你的帮助
我正在使用Eclipse,我的手机(OS:Thuderbolt)的API级别为15,目标为4.0.4。下面是我编写的示例,它使用LocationManager每两分钟获取一次位置数据。它并不完美,但应该足以解决您的问题:可以找到它 关注:
@Override
public void onLocationChanged(final Location location) {
this.location=location;
final Handler handler = new Handler();
Timer ourtimer = new Timer();
TimerTask timerTask = new TimerTask() {
int cnt=1;
public void run() {
handler.post(new Runnable() {
public void run() {
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
Double altitude = location.getAltitude();
Float accuracy = location.getAccuracy();
textView.setText("Latitude: " + latitude + "\n" + "Longitude: " + longitude+ "\n" + "Altitude: " + altitude + "\n" + "Accuracy: " + accuracy + "meters"+"\n" + "Location Counter: " + cnt);
try {
jsonData = new JSONObject();
jsonData.put("Latitude", latitude);
jsonData.put("Longitude", longitude);
jsonData.put("Altitude", altitude);
jsonData.put("Accuracy", accuracy);
System.out.println(jsonData.toString()); //not required, for testing only
if(url!=null) {
new HttpPostHandler().execute();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cnt++;
}
});
}};
ourtimer.schedule(timerTask, 0, 120000);
有几个原因导致您无法获取位置信息 1.)如果您试图在emulator上获取位置。然后,您必须使用DDMS手动推送坐标 2.)如果您正在设备上检查,但仍然无法获取位置。然后,正如你所说,你期待着它从全球定位系统。那么你应该有晴朗的天空才能看到。由于GPS接收器在屋顶或某些障碍物下不工作。他们一定能看到天空 3.)您可以使用wi-fi或手机发射塔获取位置信息。此外,如果定位精度不那么重要,您也可以选择最后一个已知的位置 我认为第二点可能会解决您的问题。试试代码。本教程还解决了我的位置跟踪问题
import android.app.AlertDialog;
import android.app.Service;
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.os.IBinder;
import android.provider.Settings;
import android.util.Log;
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
无论何时需要位置更新,只需拨打电话
gpsTracker.getLocation();
问题是google play服务库。 我必须下载库代码并在eclipse中编译它。然后,我在项目中添加了库的路径。这就解决了问题。
早些时候,我在我的项目中包含了googleplayservices.jar文件,但它不起作用。不知道为什么 有时重新启动设备会有所帮助。看看是否有帮助。谢谢你的及时回复。我试着重新启动我的手机。它仍然不工作。我没有使用模拟器。我在用我的安卓手机。我认为手机接收GPS是因为我可以在手机上打开谷歌地图应用程序,它可以跟踪我的当前位置。因此,我得出结论,全球定位系统工作正常。我错了吗?是的,因为谷歌地图使用缓存和你以前的已知位置,如果他们没有得到你的新位置,这是你能够看到谷歌地图的唯一原因。