您可以同时使用 GPS 和 Google 地图获取当前位置。如果你想通过谷歌地图获取位置,那么请show this 教程。你也可以see this之前提出的问题。
如果你想在没有谷歌地图的情况下获取位置,那么你可以使用位置管理器。
为此,我们需要在 OnCreate 方法中添加以下代码:
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
在自定义类MyLocationListener中使用实现了LocationListener接口。
我们将添加我们自己的个人位置监听器,这样我们就可以覆盖一些函数来做我们想做的事。在我们的例子中,我们只会在闲置的情况下在屏幕上打印一条消息:
onLocationChanged ( Location Update )
onProviderDisabled ( GPS Off )
onProviderEnabled (GPS On )
并添加另一个强制方法,它不会做任何事情。
onStatusChanged (We are not adding nothing here).
然后在 AndroidManifest.xml 文件中授予权限:
android:name="android.permission.ACCESS_FINE_LOCATION"