Android中GPS定位的用法实例

作者:shichen2014 时间:2021-07-26 23:26:29 

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卫星,运行结果如下图所示:

Android中GPS定位的用法实例

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

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

标签:Android,GPS
0
投稿

猜你喜欢

  • java和Spring中观察者模式的应用详解

    2023-04-21 00:16:17
  • Maven依赖管理的用法介绍

    2021-07-13 18:07:35
  • 学习Java内存模型JMM心得

    2023-11-28 20:56:17
  • struts2 validation.xml 验证规则代码解析

    2021-09-14 22:01:27
  • 详解SpringBoot工程的三种搭建方式

    2022-09-20 21:47:23
  • SpringBoot+Eureka实现微服务负载均衡的示例代码

    2021-09-19 07:10:13
  • Android 实现自定义圆形listview功能的实例代码

    2022-06-20 06:58:29
  • Jetpack Compose修饰符专项精讲

    2021-11-28 06:33:21
  • 聊聊Spring——AOP详解(AOP概览)

    2023-11-01 04:44:18
  • WPF开发技巧之花式控件功能扩展详解

    2022-07-13 05:56:52
  • 软件开发七大过程模型

    2023-12-22 17:06:14
  • WinForm中的几个实用技巧汇总

    2022-08-12 13:17:02
  • android中webview控件和javascript交互实例

    2023-09-06 14:09:28
  • Java二维数组与动态数组ArrayList类详解

    2023-07-31 19:53:56
  • SpringBoot之Json的序列化和反序列化问题

    2021-11-12 07:17:29
  • 基于C#实现图片合成功能

    2022-08-23 07:21:13
  • MapTask工作机制图文详解

    2021-12-13 11:03:27
  • Android实现Z轴布局效果

    2021-11-19 05:33:56
  • 详解Java同步—线程锁和条件对象

    2023-06-01 03:28:46
  • mybatis自定义类型处理器TypehHandler示例详解

    2023-10-11 04:30:40
  • asp之家 软件编程 m.aspxhome.com