真机器机器上测试得不到经纬度
奇怪几台真机器机器上测试得不到经纬度
在android2.2?下的 avd是可以得到 的。(开发环境通过 但真机器失败 总是等待状态。用高德地图可以看到该位置经纬度,手机是无问提的。摩托的 )
是否需要把google map的 等jar包也放入智能手机里??
代码如下package org.crazyit.gps;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.EditText;
/**
* Description:
* <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class LocationTest extends Activity
{
// 定义LocationManager对象
LocationManager locManager;
// 定义程序界面中的EditText组件
EditText show;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序界面上的EditText组件
show = (EditText) findViewById(R.id.show);
// 创建LocationManager对象
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 从GPS获取最近的最近的定位信息
Location location = locManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
// 使用location根据EditText的显示
updateView(location);
// 设置每3秒获取一次GPS的定位信息
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER
, 3000, 8, new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
// 当GPS定位信息发生改变时,更新位置
updateView(location);
}
@Override
public void onProviderDisabled(String provider)
{
updateView(null);
}
@Override
public void onProviderEnabled(String provider)
{
// 当GPS LocationProvider可用时,更新位置
updateView(locManager
.getLastKnownLocation(provider));
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras)
{
}
});
}
// 更新EditText中显示的内容
public void updateView(Location newLocation)
{
if (newLocation != null)
{
StringBuilder sb = new StringBuilder();
sb.append("实时的位置信息:\n");
sb.append("经度:");
sb.append(newLocation.getLongitude());
sb.append("\n纬度:");
sb.append(newLocation.getLatitude());
//sb.append("\n高度:");
//sb.append(newLocation.getAltitude());
//sb.append("\n速度:");
//sb.append(newLocation.getSpeed());
//sb.append("\n方向:");
//sb.append(newLocation.getBearing());
show.setText(sb.toString());
}
else
{
// 如果传入的Location对象为空则清空EditText
show.setText("");
}
}
}
[解决办法]
http://stackoverflow.com/questions/3120256/android-reliably-getting-the-current-location
参考:
ublic class MyLocation {
Timer timer1;
LocationManager lm;
public boolean getLocation(Context context)
{
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 20000);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
lm.removeUpdates(this);
//use location as it is the latest value
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//use location as we have not received the new value from listener
}
}
}
We start the listener and wait for update for some time (20 seconds in my example). If we receive update during this time we use it. If we don't receive an update during this time we use getLastKnownLocation value and stop the listener.
You can see my complete code here http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-an/3145655#3145655
EDIT (by asker): This is most of the answer, but my final solution uses a Handler instead of a Timer.
share