新建 Android Project,注意選擇Google APIs:
打開 AndroidManifest.xml,在其中加入GPS使用權限:
< uses-permission android:name= "android.permission.ACCESS_COARSE_LOCATION" /> < uses-permission android:name= "android.permission.ACCESS_FINE_LOCATION" />
main.java的代碼如下:
public class main extends Activity {
/** Called when the activity is first created. */
private LocationManager locationManager ;
private String provider ;
private Location location ;
private Address address ;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. main );
// 獲取 LocationManager 服務
locationManager = (LocationManager) this
.getSystemService(Context. LOCATION_SERVICE );
// 獲取 Location Provider
getProvider();
// 如果未設置位置源,打開 GPS 設置界面
openGPS();
// 獲取位置
location = locationManager .getLastKnownLocation( provider );
// 顯示位置信息到文字標簽
updateWithNewLocation( location );
// 注冊監聽器 locationListener ,第 2 、 3 個參數可以控制接收 gps 消息的頻度以節省電力。第 2 個參數為毫秒,
// 表示調用 listener 的周期,第 3 個參數為米 , 表示位置移動指定距離后就調用 listener
locationManager .requestLocationUpdates( provider , 2000, 10,
locationListener );
}
// 判斷是否開啟 GPS ,若未開啟,打開 GPS 設置界面
private void openGPS() {
if ( locationManager .isProviderEnabled(android.location.LocationManager. GPS_PROVIDER )
|| locationManager .isProviderEnabled(android.location.LocationManager. NETWORK_PROVIDER )
) {
Toast. makeText ( this , " 位置源已設置! " , Toast. LENGTH_SHORT ).show();
return ;
}
Toast. makeText ( this , " 位置源未設置! " , Toast. LENGTH_SHORT ).show();
// 轉至 GPS 設置界面
Intent intent = new Intent(Settings. ACTION_SECURITY_SETTINGS );
startActivityForResult(intent,0);
}
// 獲取 Location Provider
private void getProvider(){
// 構建位置查詢條件
Criteria criteria = new Criteria();
// 查詢精度:高
criteria.setAccuracy(Criteria. ACCURACY_FINE );
// 是否查詢海撥:否
criteria.setAltitudeRequired( false );
// 是否查詢方位角 : 否
criteria.setBearingRequired( false );
// 是否允許付費:是
criteria.setCostAllowed( true );
// 電量要求:低
criteria.setPowerRequirement(Criteria. POWER_LOW );
// 返回最合適的符合條件的 provider ,第 2 個參數為 true 說明 , 如果只有一個 provider 是有效的 , 則返回當前 provider
provider = locationManager .getBestProvider(criteria, true );
}
// Gps 消息監聽器
private final LocationListener locationListener = new LocationListener() {
// 位置發生改變后調用
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
// provider 被用戶關閉后調用
public void onProviderDisabled(String provider){
updateWithNewLocation( null );
}
// provider 被用戶開啟后調用
public void onProviderEnabled(String provider){ }
// provider 狀態變化時調用
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
// Gps 監聽器調用,處理位置信息
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText = (TextView)findViewById(R.id. text );
if (location != null ) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = " 緯度 :" + lat + "/n 經度 :" + lng;
} else {
latLongString = " 無法獲取地理信息 " ;
}
myLocationText.setText( " 您當前的位置是 :/n" +
latLongString+ "/n" +getAddressbyGeoPoint(location));
}
// 獲取地址信息
private List<Address> getAddressbyGeoPoint(Location location) {
List<Address> result = null ;
// 先將 Location 轉換為 GeoPoint
// GeoPoint gp =getGeoByLocation(location);
try {
if (location != null ) {
// 獲取 Geocoder ,通過 Geocoder 就可以拿到地址信息
Geocoder gc = new Geocoder( this , Locale. getDefault ());
result= gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
如果,要想在模擬器中看到效果,還需要在 DDMS的Emulator Control面板中進行一些設置。如果你看不到Emulator Control面板,可以從window->Show view->Other…中打開它:
在 Emulator Control面板中,你可以找到一個Location Controls的地方:
你可以在 Longitude和Latitude中輸入一個虛擬的經緯度,然后點擊Send,可以把模擬器的位置修改為指定位置。此時運行程序,即可看到如圖所示的信息:
有時候,你可能出現“無法獲取地理信息”的錯誤。這可能是沒有開啟“啟用 GPS衛星”選項。不要奇怪,在模擬器中,“使用無線網絡”是無效的,使用該選項無法獲取地理信息,因為模擬器中根本沒有sim卡,也就無法通過基站來定位了。
如果在真機上就不同了。如果機器沒有內置 GPS模塊,那么啟用GPS衛星選項反而無法進行定位,具體情況只有多試幾次才能明白。
@font-face { font-family: "宋體"; }@font-face { font-family: "Cambria"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 10pt; font-size: 12pt; font-family: "Times New Roman"; }div.Section1 { page: Section1; }
Geocoder需要訪問internet,在真機上調試請打開wifi或者3G網絡。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
