| 1 | 获取当前的GPS坐标 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 权限 | <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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 获取最近一次已知的GPS坐标 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 权限 | <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(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 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); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 注册监听GPS状态变化 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 权限 | <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; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 注册监听邻近提示 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 权限 | <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); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
Android 常用代码---GPS
最新推荐文章于 2024-10-31 19:37:26 发布
本文介绍如何在Android设备上实现GPS定位功能,包括实时获取GPS坐标、获取最近一次已知坐标、计算两点间距离、监听GPS状态变化及设置邻近提示等关键技术点。
7672

被折叠的 条评论
为什么被折叠?



