溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android中怎么實現GPS定位

發布時間:2021-06-26 15:46:42 來源:億速云 閱讀:377 作者:Leah 欄目:移動開發

本篇文章為大家展示了Android中怎么實現GPS定位,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

在AndroidManifest.xml中加入權限:

<uses-permission android:name="android.permission.ACCESSFINELOCATION"/>
<uses-permission android:name="android.permission.ACCESSCOARSELOCATION"/>

package com.example.tt;  import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;  public class MainActivity extends Activity {          @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);                          Button button=(Button)findViewById(R.id.button1);         button.setOnClickListener(new OnClickListener() {                          @Override             public void onClick(View arg0) {                 // TODO Auto-generated method stub                 String serviceString=Context.LOCATION_SERVICE;                 LocationManager locationManager=(LocationManager)getSystemService(serviceString);                 String provider=LocationManager.GPS_PROVIDER;                 Location location=locationManager.getLastKnownLocation(provider);                 getLocationInfo(location);                                 locationManager.requestLocationUpdates(provider, 2000, 0, locationListener);             }         });              }     private void getLocationInfo(Location location) {         String latLongInfo;         TextView lo=(TextView)findViewById(R.id.textView1);         if(location!=null){             double lat=location.getLatitude();             double lng=location.getLongitude();             latLongInfo="Lat:"+lat+"\nLong:"+lng;             lo.setText(latLongInfo);         }else {             latLongInfo="No location found";             lo.setText(latLongInfo);         }            }     private final LocationListener locationListener =new LocationListener() {                @Override         public void onStatusChanged(String provider, int status, Bundle extras) {             // TODO Auto-generated method stub                      }                @Override         public void onProviderEnabled(String provider) {             getLocationInfo(null);                      }                @Override         public void onProviderDisabled(String provider) {             getLocationInfo(null);                   }                @Override         public void onLocationChanged(Location location) {             getLocationInfo(location);             Toast.makeText(MainActivity.this, "位置改變了::::::::::::", 3000).show();         }     }; }

當需要使用基站定位時,可以將String provider=LocationManager.GPS_PROVIDER;改為**String provider=LocationManager.NETWORK_PROVIDER;
**
具體如果還要判斷GPS搜索不到時切換基站定位,那樣的功能就不要我寫了,新手都應該會.
還有就是如果用到Google定位到哪個城市地點什么的,也easy了。

上述內容就是Android中怎么實現GPS定位,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女