android如何獲取經(jīng)緯度
android 定位的兩種方式:GPS_PROVIDER and NETWORK_PROVIDER
定位的可以借助LocationManager來實現(xiàn)
MainActivity代碼
static final String TAG = "MainActivity";
private TextView locationTV;
private LocationManager locationManager;
private String provider;
ArrayList<ContactModel> dataList = new ArrayList<ContactModel>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initView();
locationTV = (TextView) findViewById(R.id.locaiton_tv);
locationManager = (LocationManager) getSystemService(this.LOCATION_SERVICE);
// 獲取所有可用的位置提供器
List<String> providerList = locationManager.getProviders(true);
if (providerList.contains(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
// 當沒有可用的位置提供器時,彈出Toast提示用戶
Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT).show();
return;
}
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
// 顯示當前設(shè)備的位置信息
showLocation(location);
}
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);
}
private void showLocation(Location location) {
String currentPosition = "latitude is " + location.getLatitude() + "\n"+ "longitude is " + location.getLongitude();
locationTV.setText(currentPosition);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (locationManager != null) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
// 關(guān)閉程序時將監(jiān)聽器移除
locationManager.removeUpdates(locationListener);
}
}
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Toast.makeText(MainActivity.this,"onLocationChanged",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
Toast.makeText(MainActivity.this,"onStatusChanged",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this,"onProviderEnabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this,"onProviderDisabled",Toast.LENGTH_SHORT).show();
}
};
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android通過原生APi獲取所在位置的經(jīng)緯度
- android通過gps獲取定位的位置數(shù)據(jù)和gps經(jīng)緯度
- Android 通過當前經(jīng)緯度獲得城市的實例代碼
- Android獲取當前位置的經(jīng)緯度數(shù)據(jù)
- Android獲取經(jīng)緯度計算距離介紹
- android手機獲取gps和基站的經(jīng)緯度地址實現(xiàn)代碼
- Android簡單獲取經(jīng)緯度的方法
- Android GPS獲取當前經(jīng)緯度坐標
- Android編程實現(xiàn)根據(jù)經(jīng)緯度查詢地址并對獲取的json數(shù)據(jù)進行解析的方法
- Android通過原生方式獲取經(jīng)緯度與城市信息的方法
相關(guān)文章
Android Jetpack Compose實現(xiàn)列表吸頂效果
安卓傳統(tǒng)的Recyclerview打造懸浮頭部StickyHeader的吸頂效果,十分麻煩,而在Compose中就簡單多了。因此,本文將采用Jetpack Compose實現(xiàn)列表吸頂效果,需要的可以參考一下2022-02-02
Android 后臺發(fā)送郵件示例 (收集應(yīng)用異常信息+Demo代碼)
今天介紹個更簡單的方法,我們把異常信息收集后,通過后臺發(fā)送郵件方法,把相關(guān)異常信息發(fā)送到我們指定的郵箱里面2013-07-07
Android checkbox的listView具體操作方法
這篇文章主要介紹了Android checkbox的listView具體操作方法,重點就是存儲每個checkbox的狀態(tài)值,感興趣的小伙伴們可以參考一下2015-12-12
Android 定位系統(tǒng)(GPS)開發(fā)詳解
GPS定位是智能手機上一個比較有意思的功能,LBS等服務(wù)都有效的利用了GPS定位功能,本文就跟大家分享下Android開發(fā)中的GPS定位知識2016-07-07
解決在eclipse中將android項目生成apk并且給apk簽名的實現(xiàn)方法詳解
本篇文章是對在eclipse中將android項目生成apk并且給apk簽名的實現(xiàn)方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Android 中 Activity顯示隱式跳轉(zhuǎn)
這篇文章主要介紹了Android 中 Activity顯示隱式跳轉(zhuǎn)的實現(xiàn)方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02
Android開發(fā)中簡單設(shè)置啟動界面的方法
這篇文章主要介紹了Android開發(fā)中簡單設(shè)置啟動界面的方法,涉及Android界面布局、加載、跳轉(zhuǎn)等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
安卓APP測試之使用Burp Suite實現(xiàn)HTTPS抓包方法
這篇文章主要介紹了安卓APP測試之使用Burp Suite實現(xiàn)HTTPS抓包方法,本文詳解講解了測試環(huán)境和各個軟件的配置方法,需要的朋友可以參考下2015-04-04

