1.新建android工程,在默认布局中为TextView添加id,显示获取的信息。
a.activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/local"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
b.在MainActivity.java中,初始化id,以及获取位置服务。
package com.example.getLocal;
import java.util.List;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;
LocationManager locationManager;
MyListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.local);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener=new MyListener();
List<String> allProviders = locationManager.getAllProviders();
System.out.println(allProviders);
Criteria criteria=new Criteria();//标准
criteria.setCostAllowed(true);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = locationManager.getBestProvider(criteria, true);//获取最佳位置提供者
/**
* String provider, LocationManager.GPS_PROVIDER gps 提供者 ,可以代替bestProvider
* long minTime,
* 最小更新时间,单位s float minDistance, 最小更新距离 ,单位米 LocationListener listener
* 回调
*
*/
locationManager.requestLocationUpdates(bestProvider, 0,
0, listener);
}
/**
*
* @author chixi
*
* onLocationChanged 位置变化
*
* onStatusChanged 状态变化,室内室外,山里山外
*
* onProviderEnabled gps打开
*
* onProviderDisabled gps关闭
*/
class MyListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double altitude = location.getAltitude();// 海拔
double latitude = location.getLatitude();// 纬度
double longitude = location.getLongitude();// 经度
float accuracy = location.getAccuracy();// 精确度
tv.setText("海拔:" + altitude + "\n" + "纬度:" + latitude + "\n"
+ "经度:" + longitude + "\n" + "精确度:" + accuracy + "\n");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
/**
* 关闭activity,之后仍会有服务,在 onDestroy()注销位置服务
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
locationManager.removeUpdates(listener);
super.onDestroy();
}
}
c.在AndroidManifest.xml中添加位置权限
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
2.运行效果
3.一般来说,手机上获取的是准确的坐标,但是再对照地图时会发生火星偏移使得显示位置不对,可还是可以通过算法减少人为误差。