可以查看本教程以获得完整的示例。
Get Current Location coordinates , City name
这里还有我的工作代码,它可以同时处理两个
- GPS提供者
- 网络提供者
公共类MyLocationListener扩展服务实现了LocationListener {私有静态最终字符串标记= "MyLocationListener";私有上下文上下文=空;私有位置=空;私有locationManager LocationManager =空;公共静态布尔isGPSEnabled =假;布尔isNetworkEnabled =假;布尔canGetLocation =假;公共双纬度= 0.0;公共双经度= 0.0;公共字符串location_address = null;//更改更新的最小距离,以米私有静态最终长MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;//私有静态最终长MIN_TIME_BW_UPDATES = 1000 * 60 * 1;// 1分钟公共MyLocationListener(上下文ctx) {Log.v(标记+ ".MyLocationListener","MyLocationListener构造函数调用“);this.context = ctx;getLocationValue();} public Location getLocationValue() {Log.v(标记+ ".getLocationValue","getLocationValue方法调用”);尝试{ .isProviderEnabled(LocationManager.GPS_PROVIDER);= ( locationManager )上下文.getSystemService(LOCATION_SERVICE);isGPSEnabled = locationManager locationManager isNetworkEnabled =locationManager isNetworkEnabled (isGPSEnabled) { this.canGetLocation = true;Log.v(TAG + ".getLocationValue",“全球定位系统提供者启用”);// Toast.makeText(上下文,"Gps",1).show();if ( == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this);if (locationManager != null) { location = locationManager locationManager if (location != null) {纬度= location.getLatitude();经度= location.getLongitude();Log.v(标签,"Gps -坐标是:“+纬度+”“+经度);}如果(isNetworkEnabled) { // Toast.makeText(上下文,”网“,1).show();Log.v(标记+ ".getLocationValue",“网络提供者已启用”);locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this);if (locationManager != null) { location = locationManager locationManager如果(location != null) {纬度= location.getLatitude();经度= location.getLongitude();Log.v(标记,“协调是:”+纬度+“”+经度);}}{ showSettingsAlert();} catch (异常e) { e.printStackTrace();}返回位置;}停止使用GPS侦听器调用此函数将停止在您的* app * */ public /** stopUsingGPS() { if (locationManager = null) {locationManager}公共双getLatitude() { if (location != null) {纬度= location.getLatitude();}返回纬度;}公共双getLongitude() { if (location != null) {经度= location.getLongitude();}返回经度;} /** *函数以检查启用了GPS/wifi的**@返回布尔* */公共布尔值canGetLocation() {返回this.canGetLocation;} /** *函数按下设置按钮显示设置警报对话框将* lauch选项* */ public AlertDialog.Builder showSettingsAlert() {AlertDialog.Builder alertDialog =新的AlertDialog.Builder(上下文);alertDialog.setTitle("GPS设置“);alertDialog .setMessage(未启用GPS)。您想转到“设置”菜单吗?“;alertDialog.setPositiveButton(”设置“,新的DialogInterface.OnClickListener() ){公共无效onClick(DialogInterface对话框,int哪个){意向=新意图( Settings.ACTION_LOCATION_SOURCE_SETTINGS);context.startActivity(意图);});alertDialog.setNegativeButton(“取消”,新的DialogInterface.OnClickListener() {公共无效onClick(DialogInterface对话框,int ){ dialog.cancel();});alertDialog.show();}@覆盖公共空onLocationChanged(位置){}@覆盖公共空onProviderDisabled(字符串提供程序){}@覆盖公共空onProviderEnabled(字符串提供程序){}@覆盖公共空onProviderEnabled(字符串提供程序,int状态,束出){}@重写公共IBinder onBind(意图){返回null;}}
在任何活动中,只要您愿意,都可以这样使用上面的代码。
MyLocationListener mylistner = new MyLocationListener(context);
double lat = mylistner.latitude;
double lon = mylistner.longitude;
注意事项:我在我的清单中也有blow权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />