经度和纬度在Android中显示为0

经度和纬度在Android中显示为0,第1张

概述*************TrackGPS.java*****************************importandroid.app.AlertDialog;importandroid.app.Service;importandroid.content.Context;importandroid.content.DialogInterface;importandroid.content.Intent;impo

  ************* TrackGPS.java *****************************      import androID.app.AlertDialog;    import androID.app.Service;    import androID.content.Context;    import androID.content.DialogInterface;    import androID.content.Intent;    import androID.content.pm.PackageManager;    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.v4.app.ActivityCompat;    import androID.util.Log;    import androID.Widget.Toast;    /**     * Created by ANQ on 8/8/2016.     */    public class TrackGPS extends Service implements LocationListener {        private final Context mContext;        boolean checkGPS = false;        boolean checkNetwork = false;        boolean canGetLocation = false;        Location loc;        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 TrackGPS(Context mContext) {            this.mContext = mContext;            getLocation();        }        private Location getLocation() {            try {                locationManager = (LocationManager) mContext                        .getSystemService(LOCATION_SERVICE);                // getting GPS status                checkGPS = locationManager                        .isProvIDerEnabled(LocationManager.GPS_PROVIDER);                // getting network status                checkNetwork = locationManager                        .isProvIDerEnabled(LocationManager.NETWORK_PROVIDER);                if (!checkGPS && !checkNetwork) {                    Toast.makeText(mContext, "No Service ProvIDer Available", Toast.LENGTH_SHORT).show();                } else {                    this.canGetLocation = true;                    // First get location from Network ProvIDer                    if (checkNetwork) {                        Toast.makeText(mContext, "Network", Toast.LENGTH_SHORT).show();                        try {                            locationManager.requestLocationUpdates(                                    LocationManager.NETWORK_PROVIDER,                                    MIN_TIME_BW_UPDATES,                                    MIN_disTANCE_CHANGE_FOR_UPDATES, this);                            Log.d("Network", "Network");                            if (locationManager != null) {                                loc = locationManager                                        .getLastKNownLocation(LocationManager.NETWORK_PROVIDER);                            }                            if (loc != null) {                                latitude = loc.getLatitude();                                longitude = loc.getLongitude();                            }                        }                        catch(SecurityException e){                        }                    }                }                // if GPS Enabled get lat/long using GPS Services                if (checkGPS) {                    Toast.makeText(mContext,"GPS",Toast.LENGTH_SHORT).show();                    if (loc == null) {                        try {                            locationManager.requestLocationUpdates(                                    LocationManager.GPS_PROVIDER,                                    MIN_TIME_BW_UPDATES,                                    MIN_disTANCE_CHANGE_FOR_UPDATES, this);                            Log.d("GPS Enabled", "GPS Enabled");                            if (locationManager != null) {                                loc = locationManager                                        .getLastKNownLocation(LocationManager.GPS_PROVIDER);                                if (loc != null) {                                    latitude = loc.getLatitude();                                    longitude = loc.getLongitude();                                }                            }                        } catch (SecurityException e) {                        }                    }                }            } catch (Exception e) {                e.printstacktrace();            }            return loc;        }        public double getLongitude() {            if (loc != null) {                longitude = loc.getLongitude();            }            return longitude;        }        public double getLatitude() {            if (loc != null) {                latitude = loc.getLatitude();            }            return latitude;        }        public boolean canGetLocation() {            return this.canGetLocation;        }        public voID showSettingsAlert() {            AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);            alertDialog.setTitle("GPS Not Enabled");            alertDialog.setMessage("Do you wants to turn On GPS");            alertDialog.setPositivebutton("Yes", new DialogInterface.OnClickListener() {                public voID onClick(DialogInterface dialog, int which) {                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);                    mContext.startActivity(intent);                }            });            alertDialog.setNegativebutton("No", new DialogInterface.OnClickListener() {                public voID onClick(DialogInterface dialog, int which) {                    dialog.cancel();                }            });            alertDialog.show();        }        public voID stopUsingGPS() {            if (locationManager != null) {                locationManager.removeUpdates(TrackGPS.this);            }        }        @OverrIDe        public IBinder onBind(Intent intent) {            return null;        }        @OverrIDe        public voID onLocationChanged(Location location) {        }        @OverrIDe        public voID onStatusChanged(String s, int i, Bundle bundle) {        }        @OverrIDe        public voID onProvIDerEnabled(String s) {        }        @OverrIDe        public voID onProvIDerDisabled(String s) {        }    }************* MainActivity.java ****************    import androID.support.v7.app.AppCompatActivity;    import androID.os.Bundle;    import androID.support.v7.app.AppCompatActivity;    import androID.os.Bundle;    import androID.vIEw.VIEw;    import androID.Widget.button;    import androID.Widget.Toast;    public class MainActivity extends AppCompatActivity {        private button b_get;        private TrackGPS gps;        double longitude;        double latitude;        @OverrIDe        protected voID onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentVIEw(R.layout.activity_main);            b_get = (button)findVIEwByID(R.ID.get);            b_get.setonClickListener(new VIEw.OnClickListener() {                @OverrIDe                public voID onClick(VIEw vIEw) {                    gps = new TrackGPS(MainActivity.this);                    if(gps.canGetLocation()){                        longitude = gps.getLongitude();                        latitude = gps .getLatitude();                        Toast.makeText(getApplicationContext(),"Longitude:"+Double.toString(longitude)+"\nLatitude:"+Double.toString(latitude),Toast.LENGTH_SHORT).show();                    }                    else                    {                        gps.showSettingsAlert();                    }                }            });        }        @OverrIDe        protected voID onDestroy() {            super.onDestroy();            gps.stopUsingGPS();        }    }

当我运行该应用程序时,纬度和经度显示为0.0,我正在使用androID棉花糖来测试该应用程序. “ http://clover.studio/2016/08/09/getting-current-location-in-android-using-location-manager/”这是我用来创建应用程序的链接.

解决方法:

请管理所需的棉花糖许可.

首先,您在清单文件中添加此权限

 <uses-permission androID:name="androID.permission.ACCESS_COARSE_LOCATION" /> <uses-permission androID:name="androID.permission.ACCESS_FINE_LOCATION" />

在您的活动之后首先这样声明两个变量,

 private static final int REQUEST_CODE_PERMISSION = 1; String mPermission = Manifest.permission.ACCESS_FINE_LOCATION;

在onCreate方法之后

 if(Build.VERSION.SDK_INT>= 23) {        if (checkSelfPermission(mPermission) != PackageManager.PERMISSION_GRANTED) {            ActivityCompat.requestPermissions(MainActivity.this,                    new String[]{mPermission,                    },                    REQUEST_CODE_PERMISSION);            return;        }        else        {            *here manage your code if permission already access        }    }
总结

以上是内存溢出为你收集整理的经度和纬度在Android中显示为0全部内容,希望文章能够帮你解决经度和纬度在Android中显示为0所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1121014.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存