gpt4 book ai didi

java - Android GPS不正确的查询位置数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:23:19 24 4
gpt4 key购买 nike

我没有为此使用模拟位置...事实上,代码就在上周运行良好。

我有一个应用程序可以收集 GPS 数据并使用应用程序本身生成的 X、Y 坐标吐出一个谷歌地图链接。我不是 100% 确定为什么它没有按应有的方式工作,但是当我请求应用程序根据手机提供的 GPS 位置建立谷歌地图链接时,它告诉我我离我的地点有 5 - 6 个街区origin(我当时实际所在的位置)不完全是我想要它做的

已知:

  • 我设置了适当的权限
  • 上周所有代码都运行良好

这是收集 GPS 信息的代码:

        Toast.makeText(context, "Gathering GPS Data...", Toast.LENGTH_SHORT).show();
gps = new gpsTracker(Settings.this);
if(gps.canGetLocation()){
try{
gps.getLocation();
lon = gps.getLongitude();
lat = gps.getLatitude();
Toast.makeText(getApplicationContext(), "Your location is - \nlat: " + lat + "\nlon: " + lon, Toast.LENGTH_LONG).show();
}
catch(Exception e){}
}
else{
gps.showSettingsAlert();
}

以上所有的类都来自:

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) {
}

@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;
}

最佳答案

根据指定的内容 [4-5 个 block 的差异],您可能仅从 networkProvider 获取 location

有了问题中提到的这个gpsTracker代码,
需要进行一些修改,而不是按原样使用代码:

1.有 2 个 if 循环验证 location 的来源是否启用并且没有 'else':

 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();
}
}
}
}

这意味着当您可以从两个来源获取 location 时,应用程序将完成两倍的工作。此外,获取的location 的来源始终是模棱两可的。

当您只需要大概的 location 时这很好,它主要不应该为 null。

如果你只想使用这个类来获取location,请尝试根据要求构造if-else,并确保它不会重复如果location 获取。
Eg. 如果 GPS 具有更高的偏好,适用于您的情况,请将 if 移至上方并将带有 else 的网络状况,例如:

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();
}
}
}
} else 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();
}
}
}

根据您的要求,我建议删除网络提供商部分并仅从 GPS 获取位置,尤其是在视线不成问题的情况下。

当您的代码运行良好时,它必须从 GPS 获取位置并将其设置在对象中。但是因为有两个“if”而没有“else”,你永远不知道获取的location是来自网络还是GPS——你可以查看location.getProvider()canGetLocation()

的条件下

2.此外,您可以记录消息或针对某个特定来源提示执行某些操作...例如:
在这部分:

 if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
}

只需将其分成两个不同的 if(s) 并相应地编写代码。到目前为止,这里什么都没有发生,所以除非你从外部检查,否则你不会知道两者是否都被禁用了。

建议:试试 LocationClient which uses GooglePlayServices对于 Retrieving Current Location .我发现它更可靠、更有用。检查这个Fused Location Provider example , 设置 LocationRequest object根据您的要求是关键。

另一个更新:刚刚遇到:useful ques on stack overflow - Good way of getting the users location


任何查找此问题/答案的人的更新
关于LocationClient的建议;
com.google.android.gms.location 下不再找到 LocationClient ,引用:
Android play services 6.5: LocationClient is missing

关于java - Android GPS不正确的查询位置数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23962769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com