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定位的简单应用的更多相关文章
- [置顶]
xamarin android使用gps定位获取经纬度
看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...
- Android中GPS类及方法简介
GPS是Global Positioning System(全球定位系统)的简称,它的作用就是为全球的物体提供定位功能.GPS定位是一门高新技术,但对于Android程序员来说,开发GPS功能的应用程 ...
- Android中GPS简介及其应用
GPS是Global Positioning System(全球定位系统)的简称,它的作用就是为全球的物体提供定位功能.GPS定位是一门高新技术,但对于Android程序员来说,开发GPS功能的应用程 ...
- [android学习]android_gps定位服务简单实现
前言 gps定位服务的学习是这段时间gps课程的学习内容,之前老师一直在将概念,今天终于是实践课(其实就是给了一个案例,让自己照着敲).不过在照着案列敲了两遍之后,发现老师的案例是在是太老了,并且直接 ...
- 【Android】GPS定位基本原理浅析
位置服务已经成为越来越热的一门技术,也将成为以后所有移动设备(智能手机.掌上电脑等)的标配.而定位导航技术中,目前精度最高.应用最广泛的,自然非GPS莫属了.网络上介绍GPS原理的专业资料很多,而本文 ...
- android 获取GPS定位
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...
- Android开发——GPS定位
1.LocationManager LocationManager系统服务是位置服务的核心组件,它提供了一系列方法来处理与位置相关的问题. 与LocationManager相关的两个知识点: 1.1 ...
- IOS中GPS定位偏移纠正(适用于Google地图)
在这个神奇的国度里,我们总得学习一些有中国特色的东东,例如“火星坐标”.也许有人还不知道这是什么玩意,我就简要介绍一下吧. 如果你有带GPS模块的智能手机,打开定位功能,然后访问Google ...
- Arcgis API for Android之GPS定位
欢迎大家增加Arcgis API for Android的QQ交流群:337469080 先说说写这篇文章的原因吧,在群内讨论的过程中,有人提到了定位的问题,刚好,自己曾经在做相关工作的时候做过相关的 ...
随机推荐
- git 修改管理
查看修改: 撤销某一文件的修改(还没提交): 撤销所有文件的修改: git checkout .
- lua的table表处理 及注意事项
lua,一款很轻量级很nice很强大的脚本语言,做为lua中使用最为频繁的table表,在使用之时还是有颇多的好处与坑的: 下面是大牛 云风的一片关于lua table的blog,可使得对lua ta ...
- iOS-远程推送
说到远程推送,应该用的也挺多的,今天就基于SEA的云推送服务,做一个推送的小demo,来了解一下iOS中的远程推送是怎么一回事儿,首先你得有苹果的开发者账号,好咸蛋也差不多了,主要内容走起. 一.准备 ...
- python学习 变量的操作 与 基本数据类型
一变量(本文使用python3.5): #变量:是计算机内存中的一块区域,变量可以存储规定范围内的值,变量可以改变#在python中变量是计算机内存中数据的引用 python的变量没有明显的类型,具体 ...
- javascript_basic_03之函数、循环、数组
1.函数:函数(Function),方法(Method),过程(Procedure): 2.默认为假的情况: ①if(0){}:②if(0.0){}:③if(null){}:④if("&qu ...
- 快速入门系列--WebAPI--04在老版本MVC4下的调整
WebAPI是建立在MVC和WCF的基础上的,原来微软老是喜欢封装的很多,这次终于愿意将http编程模型的相关细节暴露给我们了.在之前的介绍中,基本上都基于.NET 4.5之后版本,其System.N ...
- UML基础系列:用例图
1. 概述 用例图(Use Case Diagram)描述“用户.需求.系统功能单元”之间的关系,是参与者所能观察和使用到的系统功能模型图. 用例图用于需求分析阶段 用例图包含6个基本元素:参与者(A ...
- 【WP开发】认清“不透明度”与“可见性”的区别
这两种情况,许多朋友平时都没有注意到: 1.设置Opacity属性的值为0: 2.将Visibility属性设置为Collapsed. 不少人会简单地认为这两种情况是一样的,都是让UI元素看不见. 我 ...
- 使用Depth Texture
使用Depth Textures: 可以将depth信息渲染到一张texture,有些效果的制作会需要scene depth信息,此时depth texture就可以派上用场了. Depth Text ...
- javascript中Date对象的应用——简易日历的实现
× 目录 [1]效果 [2]HTML [3]CSS[4]JS 前面的话 简易日历作为javascript中Date对象的常见应用,用途较广泛.本文将详细说明简易日历的实现思路 效果演示 HTML说明 ...