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

Android实现地理定位功能

程序员文章站 2023-12-17 19:21:52
最近试着写了一下android的定位,代码在下面,比较简单 首先需要的权限有

最近试着写了一下android的定位,代码在下面,比较简单
首先需要的权限有

<uses-permission android:name="android.permission.access_coarse_location" /> 
 <uses-permission android:name="android.permission.access_fine_location"/> 
 <uses-permission android:name="android.permission.internet"/> 

然后是里面的代码

package com.example.a5.myapplication; 
 
import android.manifest; 
import android.content.context; 
import android.content.pm.packagemanager; 
import android.location.criteria; 
import android.location.location; 
import android.location.locationlistener; 
import android.location.locationmanager; 
import android.support.v4.app.activitycompat; 
import android.support.v7.app.alertdialog; 
import android.support.v7.app.appcompatactivity; 
import android.os.bundle; 
import android.view.view; 
import android.view.window; 
import android.view.windowmanager; 
import android.webkit.webview; 
import android.widget.button; 
import android.widget.textview; 
 
public class mainactivity extends appcompatactivity { 
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  requestwindowfeature(window.feature_no_title); 
  //全屏 
  getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, 
    windowmanager.layoutparams.flag_fullscreen); 
  setcontentview(r.layout.activity_main); 
  button button = (button) findviewbyid(r.id.button); 
  button.setonclicklistener(new view.onclicklistener() { 
   @override 
   public void onclick(view v) { 
    //获取位置服务 
    locationmanager locationmanager = (locationmanager) mainactivity.this.getsystemservice(context.location_service); 
    criteria criteria = new criteria(); 
    //accuracy_high/accuracy_low精度选择 
    criteria.setaccuracy(criteria.accuracy_fine); 
    //高度 
    criteria.setaltituderequired(true); 
    //方位信息 
    criteria.setbearingrequired(true); 
    //是否允许付费 
    criteria.setcostallowed(true); 
    //对电量的要求 
    criteria.setpowerrequirement(criteria.power_low); 
    //速度 
    criteria.setspeedrequired(true); 
    //获取最佳服务 
    string provider = locationmanager.getbestprovider(criteria, true); 
    //权限检查 
    if (activitycompat.checkselfpermission(mainactivity.this, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(mainactivity.this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) { 
     return; 
    } 
    //获取到了位置 
    location location = locationmanager.getlastknownlocation(provider); 
    //开启地理位置监听定位类型、毫秒、米、监听时间 
    locationmanager.requestlocationupdates(provider,3000,1,new listener()); 
    locateresult(location); 
 
   } 
  }); 
 } 
 public class listener implements locationlistener { 
 
  @override 
  public void onlocationchanged(location location) { 
   //位置变化,获取最新的位置 
   locateresult(location); 
  } 
 
  @override 
  public void onstatuschanged(string provider, int status, bundle extras) { 
 
  } 
 
  @override 
  public void onproviderenabled(string provider) { 
 
  } 
 
  @override 
  public void onproviderdisabled(string provider) { 
 
  } 
 } 
 
 public void locateresult(location location){ 
  new alertdialog.builder(mainactivity.this) 
    .setmessage("纬度" + string.valueof(location.getlatitude()) + 
      "\n经度" + string.valueof(location.getlongitude()) + 
      "\n速度" + string.valueof(location.getspeed()) + 
      "\n高度" + string.valueof(location.getaltitude()) + 
      "\n朝向" + string.valueof(location.getbearing()) 
    ) 
    .setnegativebutton("取消",null) 
    .show(); 
 } 
} 

最后上图:

Android实现地理定位功能

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

上一篇:

下一篇: