LocationManager定位

MainActivity.java

package com.example.hdMAP;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

/*本文实现基本通过gps或者network实现定位功能 * * */
public class MainActivity extends Activity {
    public final String TAG = "GPS";
    LocationListener mLocationListener = null;
    LocationManager mLocationManager = null;
    String mProviderName = null;
    TextView LatitudeText = null;
    TextView LongitudeText = null;
    TextView AltitudeText = null;
    TextView AccuracyText = null;
    TextView TimeText = null;
    TextView SpeedText = null;
    TextView BearingText = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
// 纬度
        LatitudeText = (TextView) findViewById(R.id.tv_Latitude);
// 经度
        LongitudeText = (TextView) findViewById(R.id.tv_Longitude);
// 海拔
        AltitudeText = (TextView) findViewById(R.id.tv_Altitude);
// 精度
        AccuracyText = (TextView) findViewById(R.id.tv_Accuracy);
// 时间
        TimeText = (TextView) findViewById(R.id.tv_Time);
// 速度
        SpeedText = (TextView) findViewById(R.id.tv_Speed);
// 方位
        BearingText = (TextView) findViewById(R.id.tv_Bearing);
// 获取一个locationManager
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 重写监听器
        mLocationListener = new LocationListener() {

// 当provider状态改变时候调用这个函数
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

// 当provider可用手时候调用这个函数
            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

// 当provider不可用时候调用这个函数
            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

// 当设备位置发生改变时候调用这个函数
            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                LatitudeText.setText("纬度-Latitude : " + location.getLatitude());
                LongitudeText.setText("经度-Longitude :" + location.getLongitude());
                AltitudeText.setText("海拔-Altitude :" +location.getAltitude());
                AccuracyText.setText("精度-Accuracy :" +location.getAccuracy());
                TimeText.setText("时间-Time: " +location.getTime());
                SpeedText.setText("速度-Speed :" +location.getTime());
                BearingText.setText("方位-Bearing :"+location.getBearing());
            }
        };
// criteria传入的主要是一些条件,然后调用locationManager.getbestprovider方法获取最符合条件的provider
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setBearingRequired(false);
        criteria.setPowerRequirement(Criteria.ACCURACY_LOW);
        criteria.setAltitudeRequired(false);
// 获取最佳provider,false的意思是无论设备的gps或者network有没有开启,全部检测其最佳provider,当为true时,只有选择设备开启的provider中检测最佳provider
        mProviderName = mLocationManager.getBestProvider(criteria, false);
        Log.d(TAG, mProviderName);
// 绑定监听器,每隔1000毫秒且1米距离才会更新数据
        mLocationManager.requestLocationUpdates(mProviderName, 1000, 1, mLocationListener);
    }


}

activity_main.layout

<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" >

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

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

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

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

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

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

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

</LinearLayout>

androidMainfeist.xml

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

    <uses-sdk  android:minSdkVersion="14" android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

    <application  android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
        <activity  android:name=".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>

你可能感兴趣的:(android)