欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android如何获取经纬度

程序员文章站 2023-11-21 17:49:40
android 定位的两种方式:gps_provider and network_provider 定位的可以借助locationmanager来实现 mainacti...

android 定位的两种方式:gps_provider and network_provider

定位的可以借助locationmanager来实现

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) {
   // 显示当前设备的位置信息
   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();
  }
 };

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。