【问题标题】:Location from Network Provider Android来自网络提供商 Android 的位置
【发布时间】:2019-06-26 06:19:59
【问题描述】:

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

【问题讨论】:

  • 分享你的代码sinppet...
  • 嗨 Shivam,我们刚刚开始实施。需要最好的方法或一些代码sn-p(如果有)。将不胜感激。谢谢。

标签: android location android-gps


【解决方案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);}}}

【讨论】:

    【解决方案2】:

    这里是获取位置的解决方案

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

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

    【讨论】:

    • 我想在没有网络连接的情况下获取位置
    • 让我找到解决办法
    猜你喜欢
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2016-08-14
    相关资源
    最近更新 更多