Android GPS应用:动态获取位置信息

  • 内容
  • 评论
  • 相关

activity:

package com.example.anpro02;

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.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	private LocationManager locationManager;
	private EditText show;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@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);
		
		show = (EditText) findViewById(R.id.e1);
		
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		
		updateView(location);
		
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, new LocationListener() {
			
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				updateView(locationManager.getLastKnownLocation(provider));
				
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				updateView(null);
				
			}
			
			@Override
			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub
				updateView(location);
				
			}
		});
		return true;
	}
	
	private void updateView(Location location){
		if(location != null){
			StringBuffer sbf = new StringBuffer();
			sbf.append("实时位置信息:\n经度:");
			sbf.append(location.getLongitude());
			sbf.append("\n纬度: ");
			sbf.append(location.getLatitude());
			sbf.append("\n高度:");
			sbf.append(location.getAltitude());
			sbf.append("\n速度: ");
			sbf.append(location.getSpeed());
			sbf.append("\n方向: ");
			sbf.append(location.getBearing());
			sbf.append("\n精度: ");
			sbf.append(location.getAccuracy());
			show.setText(sbf.toString());
		}
	}

}

布局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"
    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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:id="@+id/e1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cursorVisible="false"
        android:editable="false" />

</RelativeLayout>

加入相关权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.anpro02"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.anpro02.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如果把该程序与Google Map结合,让程序根据GPS提供的信息实时地显示用户在地图上的位置,即可开发出GPS导航系统。

评论

0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注