在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定位的简单应用的更多相关文章

  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. 一则线上MySql连接异常的排查过程

    Mysql作为一个常用数据库,在互联网系统应用很多.有些故障是其自身的bug,有些则不是,这里以前段时间遇到的问题举例. 问题 当时遇到的症状是这样的,我们的应用在线上测试环境,JMeter测试过程中 ...

  2. MVVM架构~mvc,mvp,mvvm大话开篇

    返回目录 百度百科的定义: MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数据,View负 责显示.作为一种新 ...

  3. ThinkPHP框架里隐藏index.php

    本文所写的配置在ThinkPHP3.2.2上测试过.按理也兼容其它版本. 首先修改配置文件: 'URL_CASE_INSENSITIVE' => true, // 默认false 表示URL区分 ...

  4. 数据库MySQL开篇

    作为一名前端,后天方面的知识虽说不一定要精通后台但是对后台方面的知识有一定了解是非常必要的. 今天先说一些概念方面的,因本人才疏学浅,对于数据库这方面的自学相当于摸着石头过河,有什么写的不对的地方,希 ...

  5. Atitit mtp ptp rndis midi协议的不同区别

    Atitit mtp ptp rndis midi协议的不同区别 1. PTP:1 2. MTP:1 3. Mtp 与usb区别2 4. 不过和UMS相比,MTP也有不足之处:3 5.  MTP协议介 ...

  6. JS的prototype和__proto__ Constructor

    一.prototype和__proto__的概念 prototype是 注意是 只有函数的一个属性才有的(每个函数都有一个prototype属性),这个属性是一个指针,指向一个普通对象并且不是原型对象 ...

  7. 请求一个action,将图片的二进制字节字符串在视图页面以图片形式输出

    有些时候需要将二进制图片字节在发送浏览器以图片形式显示: 下面是一些示例代码: 控制器: /// <summary> /// 将图片的二进制字节字符串在视图页面以图片形式输出 /// &l ...

  8. rabbitMQ第四篇:远程调用

    前言:前面我们讲解的都是本地服务器,现在如果需要远程计算机上运行一个函数,等待结果.这就是一个不同的故事了,这种模式通常被称为远程过程调用或者RPC. 本章教程我们使用RabbitMQ搭建一个RPC系 ...

  9. HTML的16个全局属性

    前面的话 在HTML中,属性能表达相当丰富的语义,而且属性也会额外提供很多实用的功能,HTML共支持16个常见的全局属性. HTML原有属性 accesskey 作用:浏览器用来创建激活或聚焦元素的快 ...

  10. JSP网站开发基础总结《九》

    本篇属于附加篇,在之前的总结中给大家提到过一个关于登录状态验证的效果,当时是通过Session对象完成的,今天我查了一下,JSP为我们封装了一个用于过滤用的过滤器类Filter,通过它我们就可以非常轻 ...