android定位之GPS,WIFI和GPRS(NETWORK)方式的定位(含实现源码)
以下代码复制即可执行:
Java源码:
Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置;注册/注销来自某个 LocationProvider的周期性的位置更新;以及注册/注销接近某个坐标时对一个已定义Intent的触发等。今天我们就来看看Android 中LocatinManager的简单使用,以获取当前所在的位置为例。
首先,我们需要获取LocationManager的一个实例,这里需要注意的是他的实例只能通过下面这种方式来获取,直接实例化LocationManager是不被允许的。
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {LocationListener locationListener = new LocationListener() {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
}
// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
}
// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.e("Map",
"Location changed : Lat: "
+ location.getLatitude() + " Lng: "
+ location.getLongitude());
}
}
};
locationManager
.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 0, locationListener);
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Toast toast = Toast.makeText(getApplicationContext(),
"GPS已成功获取坐标:经度=" + latitude + "纬度=" + longitude,
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"打点失败,未获取经纬坐标!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
} else {
LocationListener locationListener = new LocationListener() {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
}
// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
}
// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.e("Map",
"Location changed : Lat: "
+ location.getLatitude() + " Lng: "
+ location.getLongitude());
}
}
};
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000, 0,
locationListener);
Location location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude(); // 经度
longitude = location.getLongitude(); // 纬度
Toast toast = Toast.makeText(getApplicationContext(),
"移动网络已成功获取坐标:经度=" + latitude + "纬度=" + longitude,
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
String content = "EXEC.[mobile_proc_CreateUserTrack]@UserID='"
+ Login.publictype.getUserId()
+ "',@lat='"
+ location.getLatitude()
+ "',@lng='"
+ location.getLongitude() + "'";
String result = HttpApi.PostData(content).trim();
Log.e("result", result);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"打点失败,未获取经纬坐标!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
xml代码:不需要
androidmanifest代码:我自己的项目写的太多权限 我晕了 ,好了。
在AndroidManifest.xml文件中加入如下权限:
<!-- 连接互联网Internet权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GPS定位权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = location.getLatitude(); //经度
double longitude = location.getLongitude(); //纬度
double altitude = location.getAltitude(); //海拔
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); double latitude = location.getLatitude(); //经度 double longitude = location.getLongitude(); //纬度 double altitude = location.getAltitude(); //海拔