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

Android 使用Wifi或移动网络获取GPS坐标,android,location,Android,Location,我还有一个问题,我需要使用互联网获取坐标,但当我连接移动互联网(Edge)时,我在我的公寓中获得的坐标是相同的,而使用WI-Fi时,坐标是不同的。 我的代码是: public class FetchCordinates extends AsyncTask<String, Integer, String> { ProgressDialog progDailog = null; public LocationManager mLocationManager; p

我还有一个问题,我需要使用互联网获取坐标,但当我连接移动互联网(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");

        }

    }

}
公共类FetchCordinates扩展异步任务{
ProgressDialog progDailog=null;
公共场所经理;
公共蔬菜位置监听器mVeggsterLocationListener;
@凌驾
受保护的void onPreExecute(){
mVeggsterLocationListener=新的VeggsterLocationListener();
mlLocationManager=(LocationManager)getActivity()
.getSystemService(Context.LOCATION\u服务);
mLocationManager.RequestLocationUpdate(
LocationManager.NETWORK_提供程序,1000,10,
mVeggsterLocationListener);
progDailog=newprogressdialog(getActivity());
progDailog.setTitle(“Пчччччччччааааа107;
程序日志
.setMessage(“Пччгжжжжж,Яжж,эжжжжжжжжж…”);
progDailog.setUndeterminate(true);
progDailog.setCancelable(假);
progDailog.setButton(DialogInterface.BUTTON_负片,“аааааааааа107,
新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
FetchCordinates.this.cancel(true);
位置经理
.removeUpdates(mVeggsterLocationListener);
}
});
progDailog.show();
}
@凌驾
受保护的void onCancelled(){
Log.d(“myLogs”,“被用户取消!”);
progDailog.disclose();
}
@凌驾
受保护的void onPostExecute(字符串结果){
progDailog.disclose();
如果(myLat==0){
新建AlertDialog.Builder(getActivity())
.setTitle(“бббббббббббббббб107
.setMessage(“ПППППбзжж…”)
.setPositiveButton(“öκ”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int(其中){
//继续删除
}
}).show();
}否则{
pg=新进展();
pg.execute(“”);
}
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
long-timeofstartingread=System.currentTimeMillis();
而(myLat==0.0){
if(System.currentTimeMillis()-TimeOfStartingRead>timeToGetGps\u ms){
打破
}
}
mLocationManager.RemoveUpdate(mVeggsterLocationListener);
返回null;
}
公共类VeggsterLocationListener实现LocationListener{
@凌驾
已更改位置上的公共无效(位置){
试一试{
myLat=location.getLatitude();
myLong=location.getLongitude();
Toast.makeText(
getActivity(),
“麦拉”
+“,:”+myLong,
吐司。长度(短)。show();
}捕获(例外e){
}
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
Log.d(“myLogs”、“OnProviderDisabled”);
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
Log.d(“myLogs”、“onProviderEnabled”);
}
@凌驾
public void onStatusChanged(字符串提供程序,int状态,
捆绑(附加){
Log.d(“myLogs”、“onStatusChanged”);
}
}
}

不幸的是,GPS默认不太可靠,但在室外环境中应精确到5-10米;正如你可能想象的那样,室内情况更糟。许多人报告说,他们甚至无法在室内环境中接收坐标

不幸的是,GPS的准确度还取决于手边的智能手机和天气状况,因此确实没有确定准确度的具体方法

所以我最好的猜测是,不可能得到比你现在得到的更好的结果


对不起(我猜)

好吧,我想你是对的。