GPS无法使用时如何获得纬度和经度?

时间:2015-09-24 05:13:00

标签: android gps

我已经使用GoogleApiClient实现了一个Android应用程序,该应用程序在每移动5米后发送位置更新并且工作正常。

考虑有时候不会得到Gps Signal。在这种情况下我们怎么能得到更新谁能帮助我如何处理这种情况?

4 个答案:

答案 0 :(得分:0)

试试这个,

         Location nwLocation = appLocationService
                        .getLocation(LocationManager.NETWORK_PROVIDER);
                if (nwLocation != null) {
                    double latitude = nwLocation.getLatitude();
                    double longitude = nwLocation.getLongitude();
                    Toast.makeText(
                            getApplicationContext(),
                            "Mobile Location (NW): \nLatitude: " + latitude
                                    + "\nLongitude: " + longitude,
                            Toast.LENGTH_LONG).show();
                }

可以找到完整的教程here

答案 1 :(得分:0)

请看一下link

我使用此代码来获取lat long:

public void getCurruntLocation() {
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}

  private class mylocationlistener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            Log.d("LOCATION CHANGED", location.getLatitude() + "");
            Log.d("LOCATION CHANGED", location.getLongitude() + "");
            latLng = location.getLatitude() + "," + location.getLongitude();
            Toast.makeText(RecentActivity.this,
                    location.getLatitude() + "" + location.getLongitude(),
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}

和您想要获取的简单调用getCurruntLocation()

答案 2 :(得分:0)

GoogleApiClient使用LocationRequest类来设置参数数量。其中之一 - .setPrioryty。如果你在某些情况下使用LocationRequest.PRIORITY_HIGH_ACCURACY,由于缺乏GPRS坐标的准确性,googleApiClient不会在没有gps的情况下调用onLocationChanged()。

答案 3 :(得分:0)

尝试以下代码, 创建appLocationService对象 AppLocationService appLocationService; appLocationService = new AppLocationService(                 AndroidLocationActivity.this); 位置nwLocation = appLocationService                         .getLocation(LocationManager.NETWORK_PROVIDER);                 if(nwLocation!= null){                     double latitude = nwLocation.getLatitude();                     double longitude = nwLocation.getLongitude();                     Toast.makeText(                             getApplicationContext()                             “移动位置(NW):\ n Latitude:”+纬度                                     +“\ n长度:”+经度,                             Toast.LENGTH_LONG).show();                 } else {                     showSettingsAlert( “网络”);                 } 并且您的AppLocationService.java正在关注。 import android.app.Service; import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; public class AppLocationService extends Service实现LocationListener {     受保护的LocationManager locationManager;     位置位置;     private static final long MIN_DISTANCE_FOR_UPDATE = 10;     private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;     public AppLocationService(Context context){         locationManager =(LocationManager)上下文                 .getSystemService(LOCATION_SERVICE);     }     public Location getLocation(String provider){         if(locationManager.isProviderEnabled(provider)){             locationManager.requestLocationUpdates(供应商,                     MIN_TIME_FOR_UPDATE,MIN_DISTANCE_FOR_UPDATE,this);             if(locationManager!= null){                 location = locationManager.getLastKnownLocation(provider);                 返回地点;             }         }         return null;     }     @覆盖     public void onLocationChanged(Location location){     }     @覆盖     public void onProviderDisabled(String provider){     }     @覆盖     public void onProviderEnabled(String provider){     }     @覆盖     public void onStatusChanged(String provider,int status,Bundle extras){     }     @覆盖     public IBinder onBind(Intent arg0){         return null;     } } 并在manifest.xml文件中添加以下权限  < uses-permission android:name =“android.permission.ACCESS_FINE_LOCATION”/>