Android全球定位系统GPS实时获取位置-刘宇

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuyu973971883/article/details/52550807

GPS是全球定位系统,他能够获取到你当前的位置、方向、速度、高度等信息,这样可以帮助我们实现很多功能,如获取当前位置等信息、距离计算、邻近报警等功能。下面我就带大家一起来简单实现第一个功能获取当前位置等信息,大牛绕过。

效果图:

——————————————————————————————————————

首先先介绍一下GPS定位系统API中的两个重要的方法:

一、LocationManager:位置管理器。要想操作定位相关设备,必须先定义个LocationManager。我们可以通过如下代码创建LocationManger对象。
LocationManger locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 

二、LocationListener
LocationListener,位置监听,监听位置变化,监听设备开关与状态。

三、LocationProvider

LocationProvider类可以获取与位置提供者相关的信息Location类是对具体位置信息的抽象表示。

使用GPS定位的关键之一就是获取LocationProvider,每一个LocationProvider对象都表示一个抽象的定位系统。无论使用GPS做什么,都需要首先获取合适的LocationProvider对象

//  获取passive Location Provider
LocationProvider passiveProvider =
		mLocationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
//  获取gps Location Provider
LocationProvider gpsProvider =
                mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//  获取network Location Provider
LocationProvider passiveProvider = 
		mLocationManager. getLastKnownLocation (LocationManager.NETWORK_PROVIDER);

四、需要的权限

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

——————————————————————————————————

代码:

activity_main.xml:

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java:

package com.oak.learngpslocationdemo;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView tv;//用于显示信息的TextView
	private LocationManager mLocationManager;//位置管理器
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv = (TextView) findViewById(R.id.tv);//获取到TextView
        //获取到位置管理器实例
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //获取到GPS_PROVIDER
        Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        //侦听位置发生变化,2000毫秒更新一次,位置超过8米也更新一次
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, new LocationListener() {
			
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProviderEnabled(String provider) {
				// 当GPS Location Provider可用时,更新位置
				updata(mLocationManager.getLastKnownLocation(provider));
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void onLocationChanged(Location location) {
				// 当GPS定位信息发生改变时,更新位置
				updata(location);
			}
		});
        //更新位置信息显示到TextView
        updata(location);
    }
    private void updata(Location location){
    		if(location != null){
	    		StringBuilder sb = new StringBuilder();
	    		sb.append("实时的位置信息:\n");
				sb.append("经度:");
				sb.append(location.getLongitude());
				sb.append("\n纬度:");
				sb.append(location.getLatitude());
				sb.append("\b高度:");
				sb.append(location.getAltitude());
				sb.append("\n速度:");
				sb.append(location.getSpeed());
				sb.append("\n方向:");
				sb.append(location.getBearing());
				tv.setText(sb.toString());
    		}
    }
}


好啦,到这里就结束了一个简单的获取位置的demo,还有距离计算和邻近报警请看下篇博客,不动的童鞋们可以留言给我哦,我会第一时间回复的。

By:Brycen Liu

猜你喜欢

转载自blog.csdn.net/liuyu973971883/article/details/52550807