我正在尝试通过GPS获取当前用户位置。我已经阅读了关于这方面的谷歌文档,并尝试了一些教程,但似乎没有什么能正常工作。这是我的代码:
public class useGPS extends Activity implements LocationListener
{
private static final String TAG = "useGPS:";
LocationManager myLocManager;
TextView locationData;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate( savedInstanceState );
setContentView( R.layout.use_gps );
myLocManager = ( LocationManager )this.getSystemService( LOCATION_SERVICE );
Location location = myLocManager.getLastKnownLocation( myLocManager.GPS_PROVIDER) ;
if( location != null )
{
Log.d( TAG,location.toString() );
Toast.makeText( this, "Location changed...", Toast.LENGTH_SHORT);
this.onLocationChanged( location );
}
}
@Override
protected void onResume()
{
super.onResume();
myLocManager.requestLocationUpdates( myLocManager.GPS_PROVIDER, 3000, 0, this );
}
@Override
protected void onPause()
{
super.onPause();
myLocManager.removeUpdates( this );
}
@Override
public void onLocationChanged(Location location)
{
Log.d( TAG,"onLocationChanged with location: "+location.toString() );
// Return a string representation of the latitude & longitude. Pass it to the textview and update the text
String text = "Lat: "+location.getLatitude()+"nLong: "+location.getLongitude();
locationData = (TextView)findViewById( R.id.locationData );
locationData.setText( text );
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText( getApplicationContext(), "Provider Disabled...", Toast.LENGTH_SHORT );
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(), "Provider Enabled...", Toast.LENGTH_SHORT );
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
我知道GPS在室内往往不起作用,所以我在室外站了5分钟,但没有什么不同。当我使用模拟器模拟一个位置时,它返回了lat&长的位置很好-没有错误。
此外,我在清单文件中包含了必要的权限,如下所示:
<uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION"></uses-permission>
你知道为什么这不起作用吗?
好的,所以我解决了这个问题——我决定调整代码来创建监听器,而不是在类中实现它。我还确保在requestLocationUpdates上调用了NETWORK_PROVIDER和GPS_PROVIDER,以覆盖这两个基础。这是修改后的代码,请注意,注释掉的地理编码部分暂时会导致程序崩溃,我还没有解决错误。。。
public class useGPS extends Activity
{
private static final String TAG = "useGPS:";
TextView locationData, addressData;
LocationManager myLocManager;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate( savedInstanceState );
setContentView( R.layout.use_gps );
// Acquire a reference to the system Location Manager
myLocManager = (LocationManager) this.getSystemService( Context.LOCATION_SERVICE );
// Define a listener that responds to location updates
locationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
// Return a string representation of the latitude & longitude. Pass it to the textview and update the text
String text = "Lat: "+location.getLatitude()+"nLong: "+location.getLongitude();
locationData = (TextView)findViewById( R.id.locationData );
locationData.setText( text );
/**
try
{
// Reverse Geocoding
Geocoder geocoder = new Geocoder( getApplicationContext(), Locale.getDefault() );
List<Address> addresses = geocoder.getFromLocation( location.getLatitude(), location.getLongitude(),0 );
if( addresses != null )
{
// Get return address data
Address returnedAddress = addresses.get( 0 );
StringBuilder strReturnedAddress = new StringBuilder( "Address:n" );
for( int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++ )
{
// Return the address
strReturnedAddress.append( returnedAddress.getAddressLine(i) ).append( "n" );
}
// Set our textview to the address returned
addressData.setText( strReturnedAddress.toString() );
} else {
addressData.setText( "Sorry, no address returned..." );
}
} catch( IOException e )
{
// Catch the Exception
e.printStackTrace();
addressData.setText( "Error: Cannot get Address!" );
}
*/
}
@Override
public void onProviderDisabled(String provider)
{
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
};
// The two lines below request location updates from both GPS & the network location provider
myLocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0,locationListener );
myLocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0,locationListener );
}
@Override
protected void onResume()
{
super.onResume();
// The two lines below request location updates from both GPS & the network location provider
myLocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0,locationListener );
myLocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0,locationListener );
}
@Override
protected void onPause()
{
super.onPause();
// The two lines below request location updates from both GPS & the network location provider
myLocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0,locationListener );
myLocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0,locationListener );
}
}
最后一点-到目前为止,我不确定是否需要onResume()和onPause()方法上的两个requestLocationUpdate方法。onResume()方法可能会被更改为获取最后一个已知位置(如果有),而不是重新建立位置更新方法。
希望这能帮助其他人。。。