我是Android的新手,我正在使用在城市和村庄捕获GPS的应用程序,在城市我的代码工作正常,但在村庄我没有得到。可以请求解决方案。我需要的是我应该得到精确的GPS坐标(即GPS_PROVIDER
而不是网络提供者)
我使用过的代码:
当我点击捕获GPS按钮时,我正在呼叫gpsFinding()
// gps code new
public void gpsFinding() {
loc = null;
turnGPSOn();
progressBar = new ProgressDialog(this);
progressBar
.setMessage("Please Wait!...Searching for GPS Coordinates...");
progressBar.setCancelable(false);
progressBar.setOnDismissListener(this);
progressBar.show();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,// network
// provider,GPS_PROVIDER
1000, 0, listener);
startThread();
}
public void startThread() {
cnt = 0;
new Thread() {
public void run() {
while (cnt != 20) {
try {
Thread.sleep(1000);
cnt++;
System.out.println("waiting...");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
progressBar.dismiss();
}
}.start();
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
loc = location;
gpsData = loc.getLatitude() + "-" + loc.getLongitude();
double lati, longi;
lati = loc.getLatitude();
longi = loc.getLongitude();
String displayGps = " Latitude : " + lati + " , Longitude : "
+ longi;
tv_ex_gps.setText(displayGps);
Helper.GPS = gpsData.trim();
btn_save.requestFocus();
btn_save.requestFocusFromTouch();
// tv_gpsLati.setText("Latitude : " +
// displayGps.trim().split("\\,")[0]);
// tv_gpsLongi.setText("Longitude : " +
// displayGps.trim().split("\\,")[1]);
// Toast.makeText(TrackAssetCode.this,
// "Co-ordinates are received.", 5000).show();
locationManager.removeUpdates(listener);
location = null;
turnGPSOff();
progressBar.dismiss();
} else {
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
private void turnGPSOn() {
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!provider.contains("gps")) { // if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
private void turnGPSOff() {
System.out.println("We are in Off");
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (provider.contains("gps")) { // if gps is enabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
@Override
public void onDismiss(DialogInterface dialog) {
if (gpsData.length() == 0)
showGPSAlert();
}
public void showGPSAlert() {
b = new AlertDialog.Builder(Existing_Kiosk.this);
b.setTitle("Message");
b.setMessage("GPS Co-ordinates not received yet. Do you want wait some more time ?");
b.setIcon(android.R.drawable.ic_dialog_info);
b.setCancelable(false);
b.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
progressBar = new ProgressDialog(Existing_Kiosk.this);
progressBar
.setMessage("Please Wait!...Searching for GPS Coordinates...");
progressBar.setCancelable(false);
progressBar.setOnDismissListener(Existing_Kiosk.this);
progressBar.show();
startThread();
}
});
b.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
turnGPSOff();
// btn_traastCod_gps.setEnabled(true);
}
});
b.show();
}
答案 0 :(得分:1)
GPS(或更准确地说是GNSS)是唯一能够提供精确位置的系统。因此,您对exclusivley使用GPS提供商的方法是正确的。
但:
你需要自由观看天空。
因此它在室内不起作用。
它在树林里很糟糕。
对天空的自由视野期望必须等待20到40秒才能获得第一个有效位置。
对于天空不好的城市峡谷,第一个有效坐标可能需要更长的时间。但你无能为力,你无法改善。 (你可能要等到进入车辆之前有一个位置)。
在你的应用程序中,考虑在具有良好的GPS时显示,以便用户有反馈。
您的代码不应限制为20秒,重新安排您不等待GPS的代码,而是在收到有效位置后将状态切换为hasGPS = true。
答案 1 :(得分:0)
作为一个猜测,你在乡村时是否连接到数据网络? GNSS接收器从网络获得帮助,可以使工作更快。如果您连接到WiFi,它也可能有效,但这取决于移动设备。如果您可以使用浏览器成功浏览网页,则可以知道您是否已连接到数据网络。