【问题标题】:Get current location (city name) without gps and mobile network在没有 GPS 和移动网络的情况下获取当前位置(城市名称)
【发布时间】:2019-04-19 11:07:57
【问题描述】:

我有一个没有 SIM 卡和 GPS 设置为节省电池模式的 Android 平板电脑。 平板电脑通过以太网(带电缆)连接到互联网,并通过 WIFI 连接到 LAN。 我编写了一个查找当前位置(城市名称)的代码,它在我的手机中运行良好。 (我手机的 GPS 处于活动状态并通过 wifi(调制解调器)或移动网络连接到互联网)。

    package com.xenon.location;
    import android.app.Activity;
    import android.app.Service;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.location.Address;
    import android.location.Geocoder;
    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.support.annotation.Nullable;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.app.AlertDialog;

    import java.io.IOException;
    import java.util.List;
    import java.util.Locale;

    import static android.content.Context.LOCATION_SERVICE;

    public class GPSTracker extends Service implements LocationListener {

    private final Context mContext;
    private final Activity mActivity;

    boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;
    boolean canGetLocation = false;

    Location location;
    double latitude;
    double longitude;

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;

    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

    protected LocationManager locationManager;

    public GPSTracker(Activity activity) {
        this.mContext = activity.getBaseContext();
        this.mActivity = activity;
        getLocation();
    }

    public Location getLocation() {
        try {
            //locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
              // no network provider is enabled
            }else{
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    if (ContextCompat.checkSelfPermission(
                            mActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(mActivity
                                , new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}
                                , 0);
                    }
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        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;
    }

    public void stopUsingGPS() {
        if (locationManager != null) {
            if (ContextCompat.checkSelfPermission(mActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(mActivity, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
                        0);
                locationManager.removeUpdates(GPSTracker.this);
            }
        }
    }

    public double getLatitude() {
        if (location != null) {
            latitude = location.getLatitude();
        }
        return latitude;
    }

    public double getLongitude() {
        if (location != null) {
            longitude = location.getLongitude();
        }
        return longitude;
    }

    public String getCityName() {
        String result = "";
        if (location != null) {
            double latitude, longitude;
            List<Address> list;
            Locale locale = new Locale("tr");
            Geocoder geocoder = new Geocoder(mContext, locale);
            try {
                list = geocoder.getFromLocation(getLatitude(), getLongitude(), 2);

                Address address = list.get(0);

    /*
                String gpsMsg = "CountryCode: " + address.getCountryCode() +
                        " ,AdminArea : " + address.getAdminArea() +
                        " ,CountryName : " + address.getCountryName() +
                        " ,SubLocality : " + address.getSubLocality();
    */
                result = address.getAdminArea();

            } catch (IOException e) {
                e.printStackTrace();
            }

            catch (Exception e){

            }
        }
        return result;
    }

    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        alertDialog.setTitle("GPS is settings");

        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

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

        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

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

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

在平板电脑上运行时:

boolean isNetworkEnabled = false;
boolean canGetLocation = false;
        locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

isNetworkEnabled 和 canGetLocation 总是返回 false;

【问题讨论】:

    标签: dictionary networking gps location wifi


    【解决方案1】:

    您无法获取位置,因为您禁用了 GPS。 但如果您想在没有 GPS 的情况下获取您的位置,请尝试获取您的设备连接的小区信息(MNC MCC CELLID 和 LAC),然后尝试使用 API (unwiredlab.com) 将该小区信息转换为位置。

    【讨论】:

    • 好的。谢谢。我将研究“获取细胞信息(MNC MCC CELLID AND LAC)”
    • 我的设备只有局域网连接,通过局域网连接到互联网
    猜你喜欢
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多