当前位置"/>
如何通过gps得到当前位置
译自/
今天我们将看看如何通过gps得到我们当前的位置。
1.在“Firstdroid.Tutorial.Gps”目录下新建一个android项目,叫UseGps。
2.为了能够访问gps,我们需要在AndroidManifest.xml 配置文件中添加权限<uses-permission android:name=“android.permission.ACCESS_FINE_LOCATION”/>
AndroidManifest.xml文件:
<?xml version=“1.0″ encoding=“utf-8″?>
<manifest xmlns:android=“”
package=“Firstdroid.Tutorial.Gps”
android:versionCode=“1″
android:versionName=“1.0″>
<application android:icon=“@drawable/icon” android:label=“@string/app_name”>
<activity android:name=“.UseGps”
android:label=“@string/app_name”>
<intent-filter>
<action android:name=“android.intent.action.MAIN” />
<category android:name=“android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
<uses-permission android:name=“android.permission.ACCESS_FINE_LOCATION”></uses-permission>
</manifest>
3.现在我们需要添加一个位置监听器,这样才能使gps每时每刻感知到新的位置,回调方法会被系统调用。
为了完成这些,需要在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);
我们将添加自己的位置监听器,这样才能复写一些方法,来做我们想要的事。在这个例子中,我们只需要在如下情况中打印一个消息到屏幕上:
· onLocationChanged ( Location Update ) 有位置更新
· onProviderDisabled ( GPS Off ) gps关闭状态
· onProviderEnabled (GPS On ) gps打开状态
另外,添加其他强制的方法,什么都不做。
· onStatusChanged (什么都不做).
UseGps.java 如下:
package Firstdroid.Tutorial.Gps;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class UseGps extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 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);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = “My current location is: “ +
“Latitud = “ + loc.getLatitude() +
“Longitud = “ + loc.getLongitude();
Toast.makeText( getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
“Gps Disabled”,
Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
“Gps Enabled”,
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}/* End of Class MyLocationListener */
}/* End of UseGps Activity */
运行图片 如上,另外需要注意的是:gps必须在空旷处才能生效,所以测试时 到室外或窗口,以免造成困扰。
更多推荐
如何通过gps得到当前位置
发布评论