【发布时间】:2012-07-26 05:14:46
【问题描述】:
目标:确定一次 GPS 位置,然后将纬度/经度放入两个单独的 EditText 框中以显示给用户。
问题:我已经测试了我的方法,它在模拟器中运行良好,我向它发送 GPS 坐标,它运行良好且花花公子。我在两个单独的设备(v2.1 手机、v4.0 ICS 平板电脑)上尝试了它,并且 GPS 图标出现在通知中(一切都很好),但只是停留在那里试图确定位置。我已经测试了很多次,每次测试都给了足够的时间。 编辑:当我外出时在手机上工作正常,所以我可以把它划掉。平板电脑仍有问题。
我在布里斯班市中心,所以我怀疑这是位置问题 - 此外,通过 GPS 定位我时,Google 地图工作正常。
如果我关闭 GPS 以使其默认返回网络定位,它会很好地工作并为我提供位置。
方法:
(我调用的方法);
public void locateCoordinates(EditText latitudeInput, EditText longitudeInput) {
// Find coordinates, place them in EditText boxes.
// Create the locating services if needed
if(locManager == null) {
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
}
if(locListener != null) {
// Unregister listener from locManager (in case of double click of Locate button)
locManager.removeUpdates(locListener);
}
locListener = new LocListenerImpl(locManager, latitudeInput, longitudeInput);
if(locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locListener);
} else if(locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
Toast.makeText(this,
"GPS unavailable, trying network-based location instead.",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,
"No locating methods enabled", Toast.LENGTH_SHORT).show();
}
}
(监听器实现类);
public class LocListenerImpl implements LocationListener {
private LocationManager locManager = null;
private EditText latitudeInput = null;
private EditText longitudeInput = null;
public LocListenerImpl(LocationManager locManager, EditText latitudeInput, EditText longitudeInput) {
this.latitudeInput = latitudeInput;
this.longitudeInput = longitudeInput;
this.locManager = locManager;
}
public void onLocationChanged(Location location) {
if(location != null) {
latitudeInput.setText(String.valueOf(location.getLatitude()));
longitudeInput.setText(String.valueOf(location.getLongitude()));
locManager.removeUpdates(this); // Location found, unregister listener
}
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
我正在努力查看我做错了什么或可能导致这种情况的原因。任何帮助表示赞赏!
【问题讨论】:
-
您是在建筑物内还是在某种程度上看不到天空?
-
我刚走到外面,然后进入湛蓝的天空,试了一下,在我的手机上工作,但在我的平板电脑上不行。我的平板电脑是华硕 Eee Pad Transformer,所以它并不便宜
-
看看您是否可以使用另一台平板电脑,否则,请尝试在您的平板电脑上下载一些 GPS 应用程序,看看它是否有效。您的产品可能有缺陷。
标签: android gps location latitude-longitude