来自网络提供商Android的位置

时间:2019-06-26 06:19:59

标签: android location android-gps

我们正尝试在不使用位置管理器或Google位置协议的情况下获取位置。即使Internet已关闭,我们也试图直接从网络提供商那里获取位置。请帮忙。谢谢。

2 个答案:

答案 0 :(得分:1)

我使用了不需要互联网的服务。

public class LocationSS extends Service{
private static final String TAG=
"BOOMBOOMTESTGPS";

private LocationManager mLocationManager = null;

private static final int LOCATION_INTERVAL = 5000;

private static final float LOCATION_DISTANCE = 5f;

private class LocationListener implements android.location.LocationListener

{

Location mLastLocation;

public LocationListener(String provider)

    {
        Log.e(TAG, "LocationListener " + provider);
        mLastLocaenter code heretion = new Location(provienter code hereder);
    }

    @Override
    public void onLocationChanged(Location location)
    {
        Log.e(TAG, "onLocationChanged: " + location);

        String Loca="Log : "+ String.valueOf(location.getLatitude()) + ", Lat :"+String.valueOf(location.getLongitude());

        SharedPreferences xxx = LocationSS.this.getSharedPreferences("lo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = xxx.edit();
        editor.putString("loco", Loca);
        editor.apply();
        Toast.makeText(LocationSS.this,Loca,Toast.LENGTH_LONG).show();

        mLastLocation.set(location);
    }

    @Override
    public void onProviderDisabled(String provider)
    {
        Log.e(TAG, "onProviderDisabled: " + provider);
    }

    @Override
    public void onProviderEnabled(String provider)
    {
        Log.e(TAG, "onProviderEnabled: " + provider);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
        Log.e(TAG, "onStatusChanged: " + provider);
    }
}

LocationListener[] mLocationListeners = new LocationListener[] {
        new LocationListener(LocationManager.GPS_PROVIDER),
        new LocationListener(LocationManager.NETWORK_PROVIDER)
};

@Override
public IBinder onBind(Intent arg0)
{
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.e(TAG, "onStartCommand");
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

@Override
public void onCreate()
{
    Log.e(TAG, "onCreate");
    initializeLocationManager();
    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[1]);
    } catch (java.lang.SecurityException ex) {
        Log.i(TAG, "fail to request location update, ignore", ex);
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "network provider does not exist, " + ex.getMessage());
    }
    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[0]);
    } catch (java.lang.SecurityException ex) {
        Log.i(TAG, "fail to request location update, ignore", ex);
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "gps provider does not exist " + ex.getMessage());
    }
}

@Override
public void onDestroy()
{
    Log.e(TAG, "onDestroy");
    super.onDestroy();
    if (mLocationManager != null) {
        for (int i = 0; i < mLocationListeners.length; i++) {
            try {
                mLocationManager.removeUpdates(mLocationListeners[i]);
            } catch (Exception ex) {
                Log.i(TAG, "fail to remove location listners, ignore", ex);
            } }} }

private void initializeLocationManager() {
    Log.e(TAG, "initializeLocationManager");
    if (mLocationManager == null) {
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);}}}

答案 1 :(得分:0)

这是获取位置的解决方案

  

http://ip-api.com/json/

这是网址,并获取您的位置详细信息,例如纬度,经度,城市国家/地区和许多其他数据