国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

android如何獲取經(jīng)緯度

2019-12-12 02:36:44
字體:
供稿:網(wǎ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 {   // 當(dāng)沒有可用的位置提供器時,彈出Toast提示用戶   Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT).show();   return;  }  Location location = locationManager.getLastKnownLocation(provider);  if (location != null) {   // 顯示當(dāng)前設(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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 潍坊市| 义乌市| 九龙坡区| 扶绥县| 卢氏县| 昌图县| 鹤庆县| 成安县| 万年县| 察雅县| 林甸县| 九台市| 甘孜| 克什克腾旗| 洛川县| 和政县| 错那县| 都昌县| 石屏县| 青田县| 水富县| 凉山| 勃利县| 涪陵区| 新津县| 广河县| 大理市| 德保县| 理塘县| 信宜市| 沅陵县| 诸城市| 正蓝旗| 鄂温| 南充市| 嘉峪关市| 山西省| 彭泽县| 微山县| 乡宁县| 承德市|