方法及其签名 | 描述 |
List<String> getAllProviders() | 获取所有与设备关联的定位模块的列表 |
String getBestProvider(Criteria, boolean) | 获取设定的标准(Criteria对象)中最适合的一个设备 |
GpsStatus getGpsStatus(GpsStatus) | 获取GPS当前状态 |
Location getLastKnownLocation(String) | 获取最近一次的可用地点信息 |
boolean isProviderEnabled(String) | 判断参数所提及的设备是否可用 |
方法及其签名 | 描述 |
double getAltitude() | 获取当前高度 |
float getBearing() | 获取当前方向 |
double getLatitude() | 获取当前纬度 |
double getLongitude() | 获取当前经度 |
float getSpeed() | 获取当前速度 |
package org.timm.android;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.EditText;public class LocationTryActivity extends Activity { EditText text; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);text = (EditText)findViewById(R.id.textShow);Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);showLocation(location);locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, new LocationListener(){ public void onLocationChanged(Location location) {// TODO Auto-generated method stubshowLocation(location); } public void onProviderDisabled(String provider) {// TODO Auto-generated method stubshowLocation(null); } public void onProviderEnabled(String provider) {// TODO Auto-generated method stubshowLocation(locationManager.getLastKnownLocation(provider)); } public void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub }}); } public void showLocation(Location currentLocation){if(currentLocation != null){ String s = ""; s += " Current Location: ("; s += currentLocation.getLongitude(); s += ","; s += currentLocation.getLatitude(); s += ") Speed: "; s += currentLocation.getSpeed(); s += " Direction: "; s += currentLocation.getBearing(); text.setText(s);}else{ text.setText("");} }}最后一点需要说明的是,需要在AndroidManifest.xml中设置许可: