网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

android如何获取经纬度-创新互联

android 定位的两种方式:GPS_PROVIDER and NETWORK_PROVIDER

在巍山等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、成都网站建设 网站设计制作按需制作,公司网站建设,企业网站建设,品牌网站建设,营销型网站建设,成都外贸网站制作,巍山网站建设费用合理。

定位的可以借助LocationManager来实现

MainActivity代码


static final String TAG = "MainActivity";
 
 
 private TextView locationTV;
 private LocationManager locationManager;
 private String provider;
 
 ArrayList dataList = new ArrayList();
 
 @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 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) {
   // 显示当前设备的位置信息
   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;
   }
 
 
   // 关闭程序时将监听器移除
   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();
  }
 };

网页名称:android如何获取经纬度-创新互联
文章分享:http://bjjierui.cn/article/josoe.html

其他资讯