用GPS获取自己的位置
? 最近做GPS一块,所以整理一点资料,希望对大家有用!
private Button button;private TextView mTextview;LocationManager lm;double x, y;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);getAddress();button=(Button) findViewById(R.id.mTextview);button.setOnClickListener(new OnClickListener() {public void onClick(View v) {if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)!= true) { Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(callGPSSettingIntent); }}});}public void getAddress() {mTextview = (TextView) this.findViewById(R.id.mTextview);//LocationManager lm;Location loc;lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);boolean isTrue = lm.isProviderEnabled("gps");System.out.println("isTrue------------------->" + isTrue);// 获取location信息loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);System.out.println("loc------------------> " + loc);if (loc != null) {x = loc.getLatitude(); // 获取纬度y = loc.getLongitude(); // 获取经度// int i = (int) (loc.getLatitude() * 1E6);// int j = (int) (loc.getLongitude() * 1E6);}lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,new LocationListener() {public void onLocationChanged(Location location) {// TODO Auto-generated method stubmTextview.setText("纬度======" + location.getLatitude()+ "\n" + "经度=====" + location.getLongitude());}public void onProviderDisabled(String provider) {// TODO Auto-generated method stubmTextview.setText("纬度======" + provider + "\n"+ "经度=====" + provider);System.out.println("___________onProviderDisabled__________");}public void onProviderEnabled(String provider) {// TODO Auto-generated method stubmTextview.setText("纬度======" + provider + "\n"+ "经度=====" + provider);System.out.println("___________onProviderEnabled__________");}public void onStatusChanged(String provider, int status,Bundle extras) {// TODO Auto-generated method stubmTextview.setText("纬度======" + provider + "\n"+ "经度=====" + provider);System.out.println("___________onStatusChanged__________");}});}}?? 当然也要打开服务GetLocation中打开
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
?
?