在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(),加入位置改变时的动作。

布局文件:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context=".MainActivity" >
  11.  
  12. <TextView
  13. android:id="@+id/txt_time"
  14. style="@style/my_text"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:text="时间:" />
  18.  
  19. <TextView
  20. android:id="@+id/txt_lat"
  21. style="@style/my_text"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:text="经度:" />
  25.  
  26. <TextView
  27. android:id="@+id/txt_lng"
  28. style="@style/my_text"
  29. android:layout_width="fill_parent"
  30. android:layout_height="wrap_content"
  31. android:text="纬度:" />
  32.  
  33. </LinearLayout>

MainActivity.java文件:

  1. package com.hzhi.my_gps;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7.  
  8. import android.location.Criteria;
  9. import android.location.Location;
  10. import android.location.LocationListener;
  11. import android.location.LocationManager;
  12. import android.os.Bundle;
  13. import android.os.Handler;
  14. import android.os.Message;
  15. import android.app.Activity;
  16. import android.content.Context;
  17. import android.view.Menu;
  18. import android.widget.TextView;
  19.  
  20. public class MainActivity extends Activity {
  21.  
  22. TextView txt_time;
  23. TextView txt_lat;
  24. TextView txt_lng;
  25. LocationManager lom;
  26. Location loc;
  27. Double lat;
  28. Double lng;
  29. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  30. Date now;
  31. String str_date;
  32. Timer timer;
  33.  
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36.  
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_main);
  39.  
  40. get_con();
  41. get_gps();
  42.  
  43. timer = new Timer(true);
  44. timer.schedule(task, 0, 1000);
  45.  
  46. }
  47.  
  48. @Override
  49. public boolean onCreateOptionsMenu(Menu menu) {
  50. // Inflate the menu; this adds items to the action bar if it is present.
  51. getMenuInflater().inflate(R.menu.main, menu);
  52. return true;
  53. }
  54.  
  55. public void get_gps(){
  56.  
  57. lom = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  58. loc = lom.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  59.  
  60. if (loc != null) {
  61. lat = loc.getLatitude();
  62. lng = loc.getLongitude();
  63. txt_lat.setText("纬度:" + String.valueOf(lat));
  64. txt_lng.setText("经度:" + String.valueOf(lng));
  65. }
  66. else{
  67. txt_lat.setText("纬度:未知" );
  68. txt_lng.setText("经度:未知" );
  69. }
  70.  
  71. Criteria criteria = new Criteria();
  72. criteria.setAccuracy(Criteria.ACCURACY_FINE);
  73. criteria.setAltitudeRequired(false);
  74. criteria.setBearingRequired(false);
  75. criteria.setCostAllowed(true);
  76. criteria.setPowerRequirement(Criteria.POWER_LOW);
  77. String provider = lom.getBestProvider(criteria, true);
  78.  
  79. lom.requestLocationUpdates(provider, 1000, 10, los);
  80.  
  81. }
  82.  
  83. LocationListener los = new LocationListener(){
  84.  
  85. public void onLocationChanged(Location location){
  86.  
  87. if (location != null) {
  88. lat = location.getLatitude();
  89. lng = location.getLongitude();
  90. txt_lat.setText("纬度:" + String.valueOf(lat));
  91. txt_lng.setText("经度:" + String.valueOf(lng));
  92. }
  93. else{
  94. txt_lat.setText("纬度:未知" );
  95. txt_lng.setText("经度:未知" );
  96. }
  97.  
  98. };
  99.  
  100. public void onProviderDisabled(String provider){
  101.  
  102. };
  103.  
  104. public void onProviderEnabled(String provider){ };
  105.  
  106. public void onStatusChanged(String provider, int status,
  107. Bundle extras){ };
  108.  
  109. };
  110.  
  111. // 获取控件
  112. public void get_con(){
  113.  
  114. txt_time = (TextView) findViewById(R.id.txt_time);
  115. txt_lat = (TextView) findViewById(R.id.txt_lat);
  116. txt_lng = (TextView) findViewById(R.id.txt_lng);
  117.  
  118. }
  119.  
  120. Handler handler = new Handler(){
  121.  
  122. public void handleMessage(Message msg){
  123. switch (msg.what){
  124. case 1:
  125. get_time();
  126. break;
  127. }
  128. }
  129.  
  130. };
  131.  
  132. TimerTask task = new TimerTask(){
  133. public void run() {
  134. Message message = new Message();
  135. message.what = 1;
  136. handler.sendMessage(message);
  137. }
  138. };
  139.  
  140. // 获取时间
  141. public void get_time(){
  142.  
  143. now = new Date(System.currentTimeMillis());
  144. str_date = formatter.format(now);
  145. txt_time.setText("时间:" + str_date);
  146.  
  147. }
  148.  
  149. }

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

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

运行前先打开GPS卫星,运行结果:

由于在室内,并且手机质量不好,没获取出来,在室外是可以获取的。

Android中GPS定位的简单应用的更多相关文章

  1. [置顶] xamarin android使用gps定位获取经纬度

    看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...

  2. Android中GPS类及方法简介

    GPS是Global Positioning System(全球定位系统)的简称,它的作用就是为全球的物体提供定位功能.GPS定位是一门高新技术,但对于Android程序员来说,开发GPS功能的应用程 ...

  3. Android中GPS简介及其应用

    GPS是Global Positioning System(全球定位系统)的简称,它的作用就是为全球的物体提供定位功能.GPS定位是一门高新技术,但对于Android程序员来说,开发GPS功能的应用程 ...

  4. [android学习]android_gps定位服务简单实现

    前言 gps定位服务的学习是这段时间gps课程的学习内容,之前老师一直在将概念,今天终于是实践课(其实就是给了一个案例,让自己照着敲).不过在照着案列敲了两遍之后,发现老师的案例是在是太老了,并且直接 ...

  5. 【Android】GPS定位基本原理浅析

    位置服务已经成为越来越热的一门技术,也将成为以后所有移动设备(智能手机.掌上电脑等)的标配.而定位导航技术中,目前精度最高.应用最广泛的,自然非GPS莫属了.网络上介绍GPS原理的专业资料很多,而本文 ...

  6. android 获取GPS定位

    AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...

  7. Android开发——GPS定位

    1.LocationManager LocationManager系统服务是位置服务的核心组件,它提供了一系列方法来处理与位置相关的问题. 与LocationManager相关的两个知识点: 1.1 ...

  8. IOS中GPS定位偏移纠正(适用于Google地图)

    在这个神奇的国度里,我们总得学习一些有中国特色的东东,例如“火星坐标”.也许有人还不知道这是什么玩意,我就简要介绍一下吧.      如果你有带GPS模块的智能手机,打开定位功能,然后访问Google ...

  9. Arcgis API for Android之GPS定位

    欢迎大家增加Arcgis API for Android的QQ交流群:337469080 先说说写这篇文章的原因吧,在群内讨论的过程中,有人提到了定位的问题,刚好,自己曾经在做相关工作的时候做过相关的 ...

随机推荐

  1. Javascript模块化编程笔记

    最近在读阮一峰的博客http://www.ruanyifeng.com/blog/2012/10/javascript_module.html,随手记录一些重要笔记.  Javascript模块的雏形 ...

  2. 说说设计模式~适配器模式(Adapter)

    返回目录 之前和大家一起谈了工厂模式和单例模式,今天来看一下另一种非常常用的模式,它就是适配器模式,第一次看到这个模式是通过“张逸”老师的“设计之道”这篇文章,在这里表adapter讲的很透彻,今天把 ...

  3. Apache Commons CLI命令行启动

    今天又看了下Hangout的源码,一般来说一个开源项目有好几种启动方式--比如可以从命令行启动,也可以从web端启动.今天就看看如何设计命令行启动... Apache Commons CLI Apac ...

  4. Java程序员的日常 —— 注册工厂的妙用

    注册工厂是一种很常用的框架书写方法,它适合于快速创建相同类型的对象. 举个栗子 比如一个家具工厂,有沙发.椅子.茶几等等,正常的编程模式是这样的: //创建 class 沙发{} class 椅子{} ...

  5. Atitit 研发体系建立 数据存储与数据知识点体系知识图谱attilax 总结

    Atitit 研发体系建立 数据存储与数据知识点体系知识图谱attilax 总结 分类具体知识点原理规范具体实现(oracle,mysql,mssql是否可以自己实现说明 数据库理论数据库的类型 数据 ...

  6. 简单的JPA注解例子

    package ssh.entity; import java.math.BigDecimal; import java.util.Date; import javax.persistence.*; ...

  7. fir.im Weekly - 不能错过的 GitHub Top 100 开源库

    好的工具&资源,会带来更多的灵感.本期 fir.im Weekly 精选了一些实用的 iOS,Android 的使用工具和源码分享,还有前端.UI方面的干货.一起来看下:) Swift 开源项 ...

  8. fir.im Weekly - 热门 iOS 第三方库大盘点

    本期 fir.im Weekly 收集的热度资源,大部分关于Android.iOS 开发工具.源码和脑洞大开的 UI 动画,希望给你带来更多的工作创意与灵感. 盘点国内程序员不常用的热门iOS第三方库 ...

  9. Lock VS Monitor

    Lock Monitor   多线程操作的时候,为防止死锁,我们经常采用加Lock的方式解决,下面就谈一下Lock的具体运用和Lock可以用什么来替换 首先,看代码: private static o ...

  10. Netty学习五:Buffers

    1. Netty中的缓冲 在Netty中并没有使用Java自带的ByteBuffer,而是自己实现提供了一个缓存区来用于标识一个字节序列,并帮助用户操作原始字节或者自定义的POJO. Java NIO ...