使用Wifi或移动网络获取GPS坐标

时间:2013-09-23 16:17:42

标签: android location

我有下一个问题,我需要使用互联网获取坐标,但是当我连接移动互联网(Edge)时,我在我的公寓中得到相同的坐标,当我使用WI Fi坐标时是不同的。 我的代码是:

public class FetchCordinates extends AsyncTask<String, Integer, String> {
    ProgressDialog progDailog = null;

    public LocationManager mLocationManager;
    public VeggsterLocationListener mVeggsterLocationListener;

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

        mLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 1000, 10,
                mVeggsterLocationListener);

        progDailog = new ProgressDialog(getActivity());


        progDailog.setTitle("Получение координат");
        progDailog
                .setMessage("Получение Вашего местоположения, ожидайте, это может занять несколько минут...");
        progDailog.setIndeterminate(true);


        progDailog.setCancelable(false);
        progDailog.setButton(DialogInterface.BUTTON_NEGATIVE, "Отмена",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        FetchCordinates.this.cancel(true);
                        mLocationManager
                                .removeUpdates(mVeggsterLocationListener);
                    }
                });
        progDailog.show();

    }

    @Override
    protected void onCancelled() {
        Log.d("myLogs", "Cancelled by user!");
        progDailog.dismiss();

    }

    @Override
    protected void onPostExecute(String result) {
        progDailog.dismiss();
        if (myLat == 0) {
            new AlertDialog.Builder(getActivity())
                    .setTitle("Ошибка получения координат")
                    .setMessage("Попробуйте позже...")
                    .setPositiveButton("Ок",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // continue with delete
                                }
                            }).show();

        } else {

            pg = new Progress();
            pg.execute("");
        }

    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        long timeOfStartingThread = System.currentTimeMillis();
        while (myLat == 0.0) {
            if (System.currentTimeMillis() - timeOfStartingThread > timeToGetGps_ms) {
                break;
            }
        }
        mLocationManager.removeUpdates(mVeggsterLocationListener);
        return null;
    }

    public class VeggsterLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {

            try {



                myLat = location.getLatitude();
                myLong = location.getLongitude();

                 Toast.makeText(
                 getActivity(),
                 " Координаты получены :" + myLat
                 + ", :" + myLong,
                 Toast.LENGTH_SHORT).show();

            } catch (Exception e) {

            }

        }

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

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

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

        }

    }

}

1 个答案:

答案 0 :(得分:2)

遗憾的是,GPS默认不是很可靠,但在室外环境中应该精确到5-10米;你可能想象的在室内更糟糕。很多人报告说,他们甚至无法在室内环境中接收坐标。

不幸的是,GPS的准确性还取决于手头的智能手机和天气条件,所以确实没有具体的方法来确定准确的准确度。

所以我最好的猜测是,根本无法获得比您目前收到的更好的结果。

抱歉(我猜)