无法在android中找到确切的当前位置

时间:2014-05-27 12:46:20

标签: android gps location

我使用下面的代码找到当前位置,但我得到了一些设备(三星7'和10'inch和nexus 10'inch)确切的当前位置,但不幸的是我找不到三星s3中的位置

我不知道,问题是什么。找不到位置。

这是我的代码:

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;

boolean canGetLocation = false;

Location location;
double latitude;
double longitude;

//The minimum distance to change updates in metters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10 metters

//The minimum time beetwen 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);
                    updateGPSCoordinates();
                }
            }

            //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);
                        updateGPSCoordinates();
                    }
                }
            }
        }
    }
    catch (Exception e)
    {
        //e.printStackTrace();
        Log.e("Error : Location", "Impossible to connect to LocationManager", e);
    }

    return location;
}

public void updateGPSCoordinates()
{
    if (location != null)
    {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
    }
}

/**
 * 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;
}

/**
 * Function to get longitude
 */
public double getLongitude()
{
    if (location != null)
    {
        longitude = location.getLongitude();
    }

    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 */
public boolean canGetLocation()
{
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog
 */
public void showSettingsAlert()
{
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    //Setting Dialog Title
    alertDialog.setTitle(R.string.GPSAlertDialogTitle);

    //Setting Dialog Message
    alertDialog.setMessage(R.string.GPSAlertDialogMessage);

    //On Pressing Setting button
    alertDialog.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() 
    {   
        @Override
        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(R.string.cancel, new DialogInterface.OnClickListener() 
    {   
        @Override
        public void onClick(DialogInterface dialog, int which) 
        {
            dialog.cancel();
        }
    });

    alertDialog.show();
}

/**
 * Get list of address by latitude and longitude
 * @return null or List<Address>
 */
public List<Address> getGeocoderAddress(Context context)
{
    if (location != null)
    {
        Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
        try 
        {
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            return addresses;
        } 
        catch (IOException e) 
        {
            //e.printStackTrace();
            Log.e("Error : Geocoder", "Impossible to connect to Geocoder", e);
        }
    }

    return null;
}

/**
 * Try to get AddressLine
 * @return null or addressLine
 */
public String getAddressLine(Context context)
{
    List<Address> addresses = getGeocoderAddress(context);
    if (addresses != null && addresses.size() > 0)
    {
        Address address = addresses.get(0);
        String addressLine = address.getAddressLine(0);

        return addressLine;
    }
    else
    {
        return null;
    }
}

/**
 * Try to get Locality
 * @return null or locality
 */
public String getLocality(Context context)
{
    List<Address> addresses = getGeocoderAddress(context);
    if (addresses != null && addresses.size() > 0)
    {
        Address address = addresses.get(0);
        String locality = address.getLocality();

        return locality;
    }
    else
    {
        return null;
    }
}

/**
 * Try to get Postal Code
 * @return null or postalCode
 */
public String getPostalCode(Context context)
{
    List<Address> addresses = getGeocoderAddress(context);
    if (addresses != null && addresses.size() > 0)
    {
        Address address = addresses.get(0);
        String postalCode = address.getPostalCode();

        return postalCode;
    }
    else
    {
        return null;
    }
}

/**
 * Try to get CountryName
 * @return null or postalCode
 */
public String getCountryName(Context context)
{
    List<Address> addresses = getGeocoderAddress(context);
    if (addresses != null && addresses.size() > 0)
    {
        Address address = addresses.get(0);
        String countryName = address.getCountryName();

        return countryName;
    }
    else
    {
        return null;
    }
}

@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 intent) 
{
    return null;
}

}

5 个答案:

答案 0 :(得分:5)

当位置检查准确性时。如果它不够准确,那么就不要处理它。

@Override
public void onLocationChanged(Location location) {
        if (!location.hasAccuracy()) {
            return;
        }
        if (location.getAccuracy() > 5) {
            return;
        }
     // do something with location accurate to 5 meters here.
    }

答案 1 :(得分:2)

执行此操作,在设备中加载应用程序,移至开阔地,运行应用程序,等待2分钟。回到办公室里面,然后执行上面的代码

它对我有用。

希望它对你有所帮助。

答案 2 :(得分:1)

我们在三星设备上工作过,也有问题。请确保以下内容:

  1. GPS已启用(街道级别也应启用)
  2. 启用移动网络(如果需要,还启用使用数据包数据选项)
  3. 在手机中下载并安装一些第三方小部件,并等待小部件中的位置坐标显示/刷新。 (这是因为小部件中集成了超时概念,并不断尝试获取坐标)
  4. 从设备转到Google地图,并检查您的位置是否正在被识别。 (有时,我们观察到Google地图能够识别我们无法做到的坐标!!)
  5. 确保通知标题栏上的GPS卫星信号闪烁。
  6. 如果需要,设置一个刷新计时器,并添加toast消息,以便在获得后显示lat long。
  7. 对于单独的三星设备,GPS坐标第一次没有立即反映(它是空的,可以持续长达半小时:(多么令人讨厌!!)。所以,我们曾经在办公室外面等待一段时间,直到收到GPS坐标。

答案 3 :(得分:1)

我遇到了同样的问题。重点是:您的&#34; requestLocationUpdates&#34;之间需要一个时间差。和&#34; getLastKnownLocation&#34;

尝试在&#34; onStart&#34;中启动requestLocationUpdates或者&#34; onCreate&#34;方法

  

protected void onStart(){

     
   super.onStart();
   locationManager.requestLocationUpdates(
                      LocationManager.GPS_PROVIDER,
                      MIN_TIME_BW_UPDATES,
                      MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
         

}

  

这会激活您的GPS。你必须等待几秒钟才能找到一些位置。 所以我把&#34; getlastKnownLocation&#34; - OnClickEvent中的方法。如果找不到位置,则只显示Toast。

  

public void onClick(查看v){

     
  m_CurrentLocation = m_LocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  if (m_CurrentLocation != null)
       // Your action with the last known location
  else
       Toast.makeText(YourActivity.this, "No GPS Location found", Toast.LENGTH_SHORT).show();
}
  

答案 4 :(得分:1)

LocationManager存在大量错误,为什么不尝试将融合位置提供程序与LocationClient一起使用。谷歌的开发者也在上一次谷歌I / O期间推荐了这一点。

除非设备在早于Froyo的版本上运行且没有播放服务,否则没有理由使用LocationManager。