当前位置: 移动技术网 > IT编程>移动开发>Android > Android中GPS定位的用法实例

Android中GPS定位的用法实例

2019年07月24日  | 移动技术网IT编程  | 我要评论

2345奥拉星2,学习英语口语,五月节日

gps定位是目前很多手机都有的功能,且非常实用。本文以实例形式讲述了android中gps定位的用法。分享给大家供大家参考之用。具体方法如下:

一般在android中通过gps获得当前位置,首先要获得一个locationmanager实例,通过该实例的getlastknownlocation()方法获得第一个的位置,该方法的说明如下:

void android.location.locationmanager.requestlocationupdates(string provider, long mintime, float mindistance, locationlistener listener)

provider即定位方式,可以采用gps定位(locationmanager.gps_provider)或者网络定位(locationmanager.network_provider),本文是gps定位,因此使用locationmanager.gps_provider。mintime是位置更新的间隔时间。listener是位置改变的监听器,自己定义一个locationlistener(),重写onlocationchanged(),加入位置改变时的动作。

布局文件如下:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:paddingbottom="@dimen/activity_vertical_margin"
  android:paddingleft="@dimen/activity_horizontal_margin"
  android:paddingright="@dimen/activity_horizontal_margin"
  android:paddingtop="@dimen/activity_vertical_margin"
  tools:context=".mainactivity" >
 
  <textview
    android:id="@+id/txt_time"
    style="@style/my_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="时间:" />
 
  <textview
    android:id="@+id/txt_lat"
    style="@style/my_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="经度:" />
 
  <textview
    android:id="@+id/txt_lng"
    style="@style/my_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="纬度:" />
 
</linearlayout>

mainactivity.java文件如下:

package com.hzhi.my_gps;
import java.text.simpledateformat;
import java.util.date;
import java.util.timer;
import java.util.timertask;
import android.location.criteria;
import android.location.location;
import android.location.locationlistener;
import android.location.locationmanager;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.app.activity;
import android.content.context;
import android.view.menu;
import android.widget.textview;
public class mainactivity extends activity {
   
  textview txt_time;
  textview txt_lat;
  textview txt_lng;
  locationmanager lom;
  location loc;
  double lat;
  double lng;
  simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  date now;
  string str_date;
  timer timer;
 
  @override
  protected void oncreate(bundle savedinstancestate) {
     
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
     
    get_con();
    get_gps();
     
    timer = new timer(true);
    timer.schedule(task, 0, 1000);
  }
 
  @override
  public boolean oncreateoptionsmenu(menu menu) {
    // inflate the menu; this adds items to the action bar if it is present.
    getmenuinflater().inflate(r.menu.main, menu);
    return true;
  }
   
  public void get_gps(){
     
    lom = (locationmanager) getsystemservice(context.location_service);
    loc = lom.getlastknownlocation(locationmanager.gps_provider);
     
    if (loc != null) {
      lat = loc.getlatitude();
      lng = loc.getlongitude();
      txt_lat.settext("纬度:" + string.valueof(lat));
      txt_lng.settext("经度:" + string.valueof(lng));
    }
    else{
      txt_lat.settext("纬度:未知" );
      txt_lng.settext("经度:未知" );
    }
     
    criteria criteria = new criteria();
    criteria.setaccuracy(criteria.accuracy_fine);
    criteria.setaltituderequired(false);
    criteria.setbearingrequired(false);
    criteria.setcostallowed(true);
    criteria.setpowerrequirement(criteria.power_low);
    string provider = lom.getbestprovider(criteria, true);
     
    lom.requestlocationupdates(provider, 1000, 10, los);
  }
   
  locationlistener los = new locationlistener(){
    public void onlocationchanged(location location){
      if (location != null) {
        lat = location.getlatitude();
        lng = location.getlongitude();
        txt_lat.settext("纬度:" + string.valueof(lat));
        txt_lng.settext("经度:" + string.valueof(lng));
      }
      else{
        txt_lat.settext("纬度:未知" );
        txt_lng.settext("经度:未知" );
      }
    };
     
    public void onproviderdisabled(string provider){
     
    };
     
    public void onproviderenabled(string provider){ };
     
    public void onstatuschanged(string provider, int status,
    bundle extras){ };
  };
   
  // 获取控件
  public void get_con(){
     
    txt_time = (textview) findviewbyid(r.id.txt_time);
    txt_lat = (textview) findviewbyid(r.id.txt_lat);
    txt_lng = (textview) findviewbyid(r.id.txt_lng);
  }
   
  handler handler = new handler(){
     
    public void handlemessage(message msg){
      switch (msg.what){
      case 1:
        get_time();
        break;
      }
    }
  };
   
  timertask task = new timertask(){ 
     public void run() { 
       message message = new message();   
       message.what = 1;   
       handler.sendmessage(message);  
    } 
  };
   
  // 获取时间
  public void get_time(){
     
    now = new date(system.currenttimemillis());
    str_date = formatter.format(now);
    txt_time.settext("时间:" + str_date);
  }
}

在androidmanifest.xml文件中加入权限:

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

运行前先打开gps卫星,运行结果如下图所示:

读者可以在室外信号充足的地方试试,是可以获取gps位置的。

希望本文所述对大家的android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网