2013-09-23 48 views
0

我有下一个问题,我需要使用互联网获取坐标,但是当我使用移动互联网(边缘)连接时,我在使用WI Fi坐标时,是不同的。 我的代码是:使用Wifi或移动网络获取GPS坐标

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

     } 

    } 

} 

回答

2

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

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

所以我最好的猜测是,它是根本不可能获得比你目前收到更好的结果。

对不起(我猜)

+0

好吧,我的事情你是rigth。 –