我使用 Telnet 获得了模拟器中的纬度和经度值。(即)GPS。
我有一个疑问(即),如果没有谷歌地图,我们能否使用纬度和经度获取当前位置。
我使用 Telnet 获得了模拟器中的纬度和经度值。(即)GPS。
我有一个疑问(即),如果没有谷歌地图,我们能否使用纬度和经度获取当前位置。
您可以同时使用 GPS 和 Google 地图获取当前位置。如果您想通过 Google 地图获取位置,请展示本教程。你可以看到这个先前提出的问题。
如果你想在没有谷歌地图的情况下获取位置,那么你可以使用位置管理器。
为此,我们需要在 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"
您可以通过此代码获取纬度和经度
GPS_Location mGPS = new GPS_Location(MyApplication.getAppContext());
if (mGPS.canGetLocation) {
mLat = mGPS.getLatitude();
mLong = mGPS.getLongitude();
} else {
System.out.println("cannot find");
}
您必须将 gps 权限和其他权限添加到您的应用程序