Android使用GPS

Android的gps服务,主要有两个类LocationManager和LocationProvider。LocationManager与android底层交互,提供系统位置的访问方法,LocationProvider定义位置服务的提供方法,gps、network或者passive。下面是个简单的例子:

package com.yeetrack.android.mygps;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

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

public class MainActivity extends Activity
{
    private TextView locationView;

    private LocationManager locationManager;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //使用Geocoder实现地址位置和经纬度之间的转化
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());

        locationView = (TextView)findViewById(R.id.locationId);

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        StringBuilder stringBuilder = new StringBuilder("可利用的provider");

        //providers有gps,network和passive。gps最精确,network是通过网络定位,不太精确,passive是被动接受推送
        List<String> providers = locationManager.getProviders(true); 
        //位置变化监听接口
        LocationListener locationListener = 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

            }

            @Override
            public void onProviderDisabled(String provider)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLocationChanged(Location location)
            {
                /**
                 * 这里进行位置变化的一些操作
                 */
                System.out.println("位置改变了");

            }
        };

        for(String provider : providers)
        {
            locationManager.requestLocationUpdates(provider, 0, 1000, locationListener);
            stringBuilder.append("\n"+provider).append(":");
            Location location = locationManager.getLastKnownLocation(provider);
            if(location != null)
            {
                //经度
                double lat = location.getLatitude();
                //纬度
                double lng = location.getLongitude();
                double altitute = location.getAltitude();
                stringBuilder.append("(");
                stringBuilder.append(lat);
                stringBuilder.append(",");
                stringBuilder.append(lng);
                stringBuilder.append(",高度"+altitute);
                stringBuilder.append(",速度"+location.getSpeed());
                stringBuilder.append(",方向"+location.getBearing());
                stringBuilder.append(")");

                //再将经纬度转化为地名
                try
                {
                    List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
                    Address add = addresses.get(0);
                    for(int i=0;i<=add.getMaxAddressLineIndex();i++)
                        System.out.println(add.getAddressLine(i));
                } catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else
                stringBuilder.append("null");

        }

        //设置过滤条件
        Criteria criteria = new Criteria();
        //设置精确度
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        //设置耗电度
        criteria.setPowerRequirement(Criteria.POWER_HIGH);
        //是否需要高度信息
        criteria.setAltitudeRequired(true);
        //是否需要方位信息
        criteria.setBearingRequired(true);
        //方位经度
        criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);
        //是否需要速度信息
        criteria.setSpeedRequired(true);
        //速度经度
        criteria.setSpeedAccuracy(Criteria.ACCURACY_HIGH);
        //是否产生费用
        criteria.setCostAllowed(true);

        //获取符合条件的location provider
        String bestProvider = locationManager.getBestProvider(criteria, true);
        System.out.println(bestProvider);

        List<String> matchProviders = locationManager.getProviders(false);
        for(String index : matchProviders)
            System.out.println(index);

        //已知地名,获取该点的坐标等信息
        String address = "北京天安门";

        try
        {
            List<Address> addresses  = geocoder.getFromLocationName(address, 5);
            if(addresses.size()!=0)
            {
                for(Address index : addresses)
                {
                    System.out.println("+++");
                    System.out.println(index.getLatitude());
                    System.out.println(index.getLongitude());
                }
            }
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        locationView.setText(stringBuilder);
    }
}
版权声明

本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。

© 空空博客,本文链接:https://www.yeetrack.com/?p=667