【问题标题】:android - can't get gps coordinateandroid - 无法获取gps坐标
【发布时间】:2018-05-13 13:20:45
【问题描述】:

我想获取用户坐标,gps 已开启,我已请求权限,它具有权限。 位置已设置,我在谷歌地图等其他应用程序上有我的位置,但在我的应用程序中,它不起作用:

    public boolean startService() {
        try {
            FetchCordinates fetchCordinates = new FetchCordinates();
            fetchCordinates.execute();
            return true;
        } catch (Exception error) {
            return false;
        }
    }

 LocationManager mlocManager = null;
            LocationListener mlocListener;
            mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            mlocListener = new MyLocationListener();
            mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
            if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                  startService();
            } 

public class FetchCordinates extends AsyncTask<String, Integer, String> {
        AlertDialog.Builder a;
        AlertDialog dialog;

        public double lati = 0.0;
        public double longi = 0.0;

        public LocationManager mLocationManager;
        public FetchCordinates.VeggsterLocationListener mVeggsterLocationListener;

        @Override
        protected void onPreExecute() {
            mVeggsterLocationListener = new FetchCordinates.VeggsterLocationListener();
            mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 0,
                    mVeggsterLocationListener);

            a = new AlertDialog.Builder(Admins.this);
            a.setMessage("در حال  به دست آوردن موقعیت جغرافیایی...");
            a.setPositiveButton(("بیخیال"), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    FetchCordinates.this.cancel(true);
                }
            });

            dialog = a.show();
            TextView messageText = (TextView) dialog.findViewById(android.R.id.message);
            messageText.setGravity(Gravity.RIGHT);
            messageText.setTypeface(typeface);
        }

        @Override
        protected void onCancelled() {
            System.out.println("Cancelled by user!");
            dialog.dismiss();
            mLocationManager.removeUpdates(mVeggsterLocationListener);
        }

        @Override
        protected void onPostExecute(String result) {
            dialog.dismiss();
            Log.v("this","got the location");

        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            while (this.lati == 0.0 && !isCancelled()) {

            }
            return null;
        }

        public class VeggsterLocationListener implements LocationListener {

            @Override
            public void onLocationChanged(Location location) {

                int lat = (int) location.getLatitude(); // * 1E6);
                int log = (int) location.getLongitude(); // * 1E6);
                int acc = (int) (location.getAccuracy());

                String info = location.getProvider();
                try {

                    // LocatorService.myLatitude=location.getLatitude();

                    // LocatorService.myLongitude=location.getLongitude();

                    lati = location.getLatitude();
                    longi = location.getLongitude();

                } catch (Exception e) {
                    // progDailog.dismiss();
                    // Toast.makeText(getApplicationContext(),"Unable to get Location"
                    // , Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onProviderDisabled(String provider) {
                Log.i("OnProviderDisabled", "OnProviderDisabled");
            }

            @Override
            public void onProviderEnabled(String provider) {
                Log.i("onProviderEnabled", "onProviderEnabled");
            }

            @Override
            public void onStatusChanged(String provider, int status,
                                        Bundle extras) {
                Log.i("onStatusChanged", "onStatusChanged");

            }

        }

    }

它向我显示了获取位置的对话框,但它永远不会获取当前位置。 我的代码有什么问题?

【问题讨论】:

标签: android android-maps-v2


【解决方案1】:

请提供您在Manifest.xml 文件中所写的内容。可能是权限有问题。

你需要 android.permission.ACCESS_COARSE_LOCATIONandroid.permission.ACCESS_FINE_LOCATION

也适用于 API 5.0 或更高版本:

android.hardware.location.gps

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 2016-09-19
    • 2015-04-10
    • 1970-01-01
    相关资源
    最近更新 更多