一直在使用Googl 的gps定位功能,现在学习一下android简单的gps定位功能
public class GPSLocation extends Activity{ private static final String TAG="GPSLocation"; Button gpsButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gps_location); gpsButton=(Button)findViewById(R.id.gpsButton); gpsButton.setOnClickListener(gpsListener); } View.OnClickListener gpsListener=new View.OnClickListener() { @Override public void onClick(View v) { LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE); MyLocationListener myLocationListener=new MyLocationListener(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener); } }; private class MyLocationListener implements LocationListener{ @Override public void onLocationChanged(Location location) { Log.i(TAG, "onLocationChanged"); String latitude=String.valueOf(location.getLatitude()); String Longitude=String.valueOf(location.getLongitude()); Toast.makeText(GPSLocation.this, "My current location is:Latitude:"+latitude+",longtitude:"+Longitude, Toast.LENGTH_LONG).show(); System.out.println("My current location is:Latitude:"+latitude+",longtitude:"+Longitude); } @Override public void onProviderDisabled(String provider) { Log.i(TAG, "onProviderDisabled"); } @Override public void onProviderEnabled(String provider) { Log.i(TAG, "onProviderEnabled"); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.i(TAG, "onStatusChanged"); } } }
相关的权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission> |