天天看点

手机GPS获取海拔

首先GPS在室内是没有信号的,所以只能在室外或者窗口获取到海拔等一系列数据。

java代码如下

package com.example.altitudeproject;

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.util.Log;   

import android.view.View;   

import android.widget.Button;   

import android.widget.TextView;   

public class MainActivity extends Activity {   

    TextView tv1;   

    Location location;   

    @Override  

    public void onCreate(Bundle savedInstanceState) {   

        super.onCreate(savedInstanceState);   

        setContentView(R.layout.activity_main);  

        // 定义UI组件   

        Button b1 = (Button) findViewById(R.id.bt_button1);   

        tv1 = (TextView) findViewById(R.id.tv_textview1);   

        // 获取LocationManager对象   

        LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);   

        // 定义Criteria对象   

        Criteria criteria = new Criteria();   

        // 设置定位精确度 Criteria.ACCURACY_COARSE 比较粗略, Criteria.ACCURACY_FINE则比较精细   

        criteria.setAccuracy(Criteria.ACCURACY_FINE);   

        // 设置是否需要海拔信息 Altitude   

        criteria.setAltitudeRequired(true);   

        // 设置是否需要方位信息 Bearing   

        criteria.setBearingRequired(true);   

        // 设置是否允许运营商收费   

        criteria.setCostAllowed(true);   

        // 设置对电源的需求   

        criteria.setPowerRequirement(Criteria.POWER_LOW);   

        // 获取GPS信息提供者   

        String bestProvider = lm.getBestProvider(criteria, true);   

        // 获取定位信息   

        location = lm.getLastKnownLocation(bestProvider);   

        // 给按钮绑定点击监听器   

        b1.setOnClickListener(new View.OnClickListener() {   

            @Override  

            public void onClick(View v) {   

                updateLocation(location);   

            }   

        });   

        // 位置监听器   

        LocationListener locationListener = new LocationListener() {   

            // 当位置改变时触发   

            @Override  

            public void onLocationChanged(Location location) {   

                Log.i("onLocationChanged", location.toString());   

                updateLocation(location);   

            }   

            // Provider失效时触发   

            @Override  

            public void onProviderDisabled(String arg0) {   

                Log.i("yao", arg0);   

            }   

            // Provider可用时触发   

            @Override  

            public void onProviderEnabled(String arg0) {   

                Log.i("onProviderEnabled", arg0);   

            }   

            // Provider状态改变时触发   

            @Override  

            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {   

                Log.i("onStatusChanged", "onStatusChanged");   

            }   

        };   

        // 500毫秒更新一次,忽略位置变化   

        lm.requestLocationUpdates(bestProvider, 500, 0, locationListener);   

    }   

    // 更新位置信息   

    private void updateLocation(Location location) {   

        if (location != null) {   

            tv1.setText("定位信息如下:" + location.toString()+"\n\t海拔:"+location.getAltitude() +"\n\t方向:"+location.getBearing()+ "\n\t经度:" + location.getLongitude() + "\n\t纬度:"  

                    + location.getLatitude()+"\n\t提供商:"+location.getProvider()+"\n\t速度:"+location.getSpeed()+"\n\t时间:"+location.getTime());   

        } else {   

            Log.i("updateLocation", "没有获取到定位对象Location");   

        }   

    }   

}  

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="${relativePackage}.${activityClass}" >

     <Button

         android:id="@+id/bt_button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="获取海拔" />

    <TextView

         android:id="@+id/tv_textview1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="具体信息"

        android:layout_below="@id/bt_button1" />

</RelativeLayout>

AndroidManifest.xml中需要配置权限:

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

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