2012-12-10 24 views
4

我想使用gps提供程序获取设备的当前位置,但实际上等待gps位置发生变化。如何获得当前的GPS位置而无需等待Android上的位置更改?

所以我有一个简单的应用程序侦听的位置变化。这适用于NETWORK_PROVIDER立即,但GPS_PROVIDER我等了15分钟,并没有发生任何行动。

我使用API​​级8

public class MainActivity extends Activity { 
    TextView tv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tv = (TextView) this.findViewById(R.id.textView1); 
     LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     LocationListener ll = new mylocationlistener(); 

     //lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll); // this works fine 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 
    } 

    private class mylocationlistener implements LocationListener { 
     public void onLocationChanged(Location location) { 
      Date today = new Date(); 
      Timestamp currentTimeStamp = new Timestamp(today.getTime()); 
      if (location != null) { 
       Log.d("LOCATION CHANGED", location.getLatitude() + ""); 
       Log.d("LOCATION CHANGED", location.getLongitude() + ""); 
       String str = "\n CurrentLocation: " + "\n Latitude: " 
         + location.getLatitude() + "\n Longitude: " 
         + location.getLongitude() + "\n Accuracy: " 
         + location.getAccuracy() + "\n CurrentTimeStamp " 
         + currentTimeStamp + "\n Altitude: " 
         + location.getAltitude() + "\n Bearing" 
         + location.getBearing() + "\n Speed" 
         + location.getSpeed() + "\n "; 
       Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG) 
         .show(); 
       tv.append(str); 
      } 
     } 

     public void onProviderDisabled(String provider) { 
      Toast.makeText(MainActivity.this, "Error onProviderDisabled", 
        Toast.LENGTH_LONG).show(); 
     } 

     public void onProviderEnabled(String provider) { 
      Toast.makeText(MainActivity.this, "onProviderEnabled", 
        Toast.LENGTH_LONG).show(); 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
      Toast.makeText(MainActivity.this, "onStatusChanged", 
        Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

我没有把清单中的权限,所以这不是问题。

+0

你确定你已经启用了您的GPS? – gsingh2011

+0

是的,我很确定... –

+0

这取决于您的设备以及GPS信号强度,因为较新的手机具有高敏感的GPS系统,但旧手机需要开阔的天空来检测信号。 –

回答

2

你测试里面?除非您身处室外,否则您可能根本无法获得GPS锁,这将意味着手机会尽可能频繁地锁定锁(因为您已在requestLocationUpdates调用中为最小时间和距离指定了0和0) )。

不幸的是,你只需要等待设备获得GPS锁定,如果确实如此,由于缺乏信号的不,你只要继续等待。您可以做的第一件事是减轻这个负担,即尝试获取GPS提供商的最后一个已知位置。这将返回一个位置,如果该设备最近有一个GPS锁和存储位置:

Location lastLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

除此之外,你应该尝试从网络供应商的位置,因为这很可能会回到一个位置就像你已经观察到的那样,它比GPS更快。这是因为大量的位置数据源(手机信号塔和WIFI),无论何时都可以使用,包括室内,只要您有无线信号。

+0

是啊,我在里面,我并没有意识到里面被会让我失去所有的信号?想我就只能使用模拟器... –

+1

这是一个好主意。还有一个出色的应用程序,其他人称为“假GPS”,可让您模拟位置锁定以进行测试。我强烈建议你尝试一下,如果你只需要调试一个基于位置的应用程序。链接如下:https://play.google.com/store/apps/details?id = com.lexa.fakegps – mattgmg1990

0
try{ 
       LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 


       //Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
       Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

       double longitude = location.getLongitude(); 
       double latitude = location.getLatitude(); 
       String stlongitude = Double.toString(longitude); 
       String stlatitude = Double.toString(latitude); 

       TextView celllocation = (TextView) findViewById(R.id.txtCellLocation); 
       celllocation.setText(stlatitude + " , " + stlongitude); 


       //String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude); 
       //Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
       //startActivity(intent); 
    } 

    catch (Exception a) { 
     LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

     double longitude = location.getLongitude(); 
     double latitude = location.getLatitude(); 
     String stlongitude = Double.toString(longitude); 
     String stlatitude = Double.toString(latitude); 

     TextView celllocation = (TextView) findViewById(R.id.txtCellLocation); 
     celllocation.setText(stlatitude + " , " + stlongitude); 

    }