android GpS

/*

* GpS

*/

 

//***获取当前的 GPS 坐标

 

// AndroidManifest.xml must have the following permission:

// <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {

public void onStatusChanged(String provider, int status, Bundle extras) {

// called when the provider status changes. Possible status: OUT_OF_SERVICE, TEMPORARILY_UNAVAILABLE or AVAILABLE.

}

public void onProviderEnabled(String provider) {

// called when the provider is enabled by the user

}

public void onProviderDisabled(String provider) {

// called when the provider is disabled by the user, if it's already disabled, it's called immediately after requestLocationUpdates

}

public void onLocationChanged(Location location) {

double latitute = location.getLatitude();

double longitude = location.getLongitude();

// do whatever you want with the coordinates

}

});

 

//***获取最近一次已知的 GPS 坐标

 

// AndroidManifest.xml must have the following permission:

// <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

double latitute, longitude = 0;

if(location != null){

latitute = location.getLatitude();

longitude = location.getLongitude();

}

 

//***GPS 坐标之间的距离

 

Location originLocation = new Location("gps");

Location destinationLocation = new Location("gps");

originLocation.setLatitude(originLatitude);

originLocation.setLongitude(originLongitude);

destinationLocation.setLatitude(originLatitude);

destinationLocation.setLongitude(originLongitude);

float distance = originLocation.distanceTo(destinationLocation);

 

//***注册监听GPS 状态变化

 

// AndroidManifest.xml must have the following permission:

// <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationManager.addGpsStatusListener(new GpsStatus.Listener(){

 

public void onGpsStatusChanged(int event) {

switch(event){

// Event sent when the GPS system has started

case GpsStatus.GPS_EVENT_STARTED:

// put your code here

break;

// Event sent when the GPS system has stopped

case GpsStatus.GPS_EVENT_STOPPED:

// put your code here

break;

// Event sent when the GPS system has received its first fix since starting

case GpsStatus.GPS_EVENT_FIRST_FIX:

// put your code here

break;

// Event sent periodically to report GPS satellite status

case GpsStatus.GPS_EVENT_SATELLITE_STATUS:

// put your code here

break;

     

}

}

});

 

//***注册监听邻近提示

 

// AndroidManifest.xml must have the following permission:

// <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

// Use PendingIntent.getActivity(Context, int, Intent, int), PendingIntent.getBroadcast(Context, int, Intent, int) or PendingIntent.getService(Context, int, Intent, int) to create the PendingIntent, which will be used to generate an Intent to fire when the proximity condition is satisfied.

PendingIntent pendingIntent;

// latitude the latitude of the central point of the alert region

// longitude the longitude of the central point of the alert region

// radius the radius of the central point of the alert region, in meters

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationManager.addProximityAlert(latitude, longitude, radius, -1, pendingIntent);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值