用GPS获取自己的位置

  最近做GPS一块,所以整理一点资料,希望对大家有用!

  private Button button;
	private TextView mTextview;
	LocationManager lm;
	double x, y;
	/** Called when the activity is first created. */
	@Override
	public 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 stub
						mTextview.setText("纬度======" + location.getLatitude()
								+ "\n" + "经度=====" + location.getLongitude());
					}

					public void onProviderDisabled(String provider) {
						// TODO Auto-generated method stub
						mTextview.setText("纬度======" + provider + "\n"
								+ "经度=====" + provider);
						System.out
								.println("___________onProviderDisabled__________");
					}

					public void onProviderEnabled(String provider) {
						// TODO Auto-generated method stub
						mTextview.setText("纬度======" + provider + "\n"
								+ "经度=====" + provider);
						System.out
								.println("___________onProviderEnabled__________");
					}

					public void onStatusChanged(String provider, int status,
							Bundle extras) {
						// TODO Auto-generated method stub
						mTextview.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" />

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhchzh1000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值