• Android Location在GPS中的应用(一)


    新建Android Project,注意选择Google APIs:

    打开AndroidManifest.xml,在其中加入GPS使用权限:

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

     

    main.java的代码如下:

    public class main extends Activity {

        /** Called when the activity is first created. */

    private LocationManager locationManager ;

    private String provider ;

    private Location location ;

    private Address address ;

        @Override

        public void onCreate(Bundle savedInstanceState) {

            super .onCreate(savedInstanceState);

            setContentView(R.layout. main );

            // 获取 LocationManager 服务

            locationManager = (LocationManager) this

                    .getSystemService(Context. LOCATION_SERVICE );

            // 获取 Location Provider

            getProvider();

            // 如果未设置位置源,打开 GPS 设置界面

            openGPS();

            // 获取位置

            location = locationManager .getLastKnownLocation( provider );

            // 显示位置信息到文字标签

            updateWithNewLocation( location );

            // 注册监听器 locationListener ,第 2 3 个参数可以控制接收 gps 消息的频度以节省电力。第 2 个参数为毫秒,

            // 表示调用 listener 的周期,第 3 个参数为米 , 表示位置移动指定距离后就调用 listener

            locationManager .requestLocationUpdates( provider , 2000, 10,

                            locationListener );

        }

        // 判断是否开启 GPS ,若未开启,打开 GPS 设置界面

        private void openGPS() {       

            if ( locationManager .isProviderEnabled(android.location.LocationManager. GPS_PROVIDER )

            || locationManager .isProviderEnabled(android.location.LocationManager. NETWORK_PROVIDER )

            ) {

                Toast.makeText ( this , " 位置源已设置! " , Toast. LENGTH_SHORT ).show();

                return ;

            }

            Toast.makeText ( this , " 位置源未设置! " , Toast. LENGTH_SHORT ).show();

            // 转至 GPS 设置界面

            Intent intent = new Intent(Settings. ACTION_SECURITY_SETTINGS );

            startActivityForResult(intent,0);

        }

        // 获取 Location Provider

        private void getProvider(){

        // 构建位置查询条件

            Criteria criteria = new Criteria();

            // 查询精度:高

            criteria.setAccuracy(Criteria. ACCURACY_FINE );

             // 是否查询海拨:否

            criteria.setAltitudeRequired( false );

            // 是否查询方位角 :

            criteria.setBearingRequired( false );

            // 是否允许付费:是

            criteria.setCostAllowed( true );

            // 电量要求:低

            criteria.setPowerRequirement(Criteria. POWER_LOW );

            // 返回最合适的符合条件的 provider ,第 2 个参数为 true 说明 , 如果只有一个 provider 是有效的 , 则返回当前 provider

            provider = locationManager .getBestProvider(criteria, true );  

        }

      // Gps 消息监听器

        private final LocationListener locationListener = new LocationListener() {

        // 位置发生改变后调用

            public void onLocationChanged(Location location) {

            updateWithNewLocation(location);

            }

            // provider 被用户关闭后调用

            public void onProviderDisabled(String provider){

            updateWithNewLocation( null );

            }

             // provider 被用户开启后调用

            public void onProviderEnabled(String provider){ }

            // provider 状态变化时调用

            public void onStatusChanged(String provider, int status,

            Bundle extras){ }

        };

        // Gps 监听器调用,处理位置信息

        private void updateWithNewLocation(Location location) {

            String latLongString;

            TextView myLocationText = (TextView)findViewById(R.id. text );

            if (location != null ) {

            double lat = location.getLatitude();

            double lng = location.getLongitude();

            latLongString = " 纬度 :" + lat + "/n 经度 :" + lng;

            } else {

            latLongString = " 无法获取地理信息 " ;

            }

            myLocationText.setText( " 您当前的位置是 :/n" +

            latLongString+ "/n" +getAddressbyGeoPoint(location));

        }

    // 获取地址信息

    private List<Address> getAddressbyGeoPoint(Location location) {

    List<Address> result = null ;

    // 先将 Location 转换为 GeoPoint

    // GeoPoint gp =getGeoByLocation(location);

    try {

    if (location != null ) {

    // 获取 Geocoder ,通过 Geocoder 就可以拿到地址信息

    Geocoder gc = new Geocoder( this , Locale.getDefault ());

    result= gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

    }

    } catch (Exception e) {

    e.printStackTrace();

    }

    return result;

    }

    }

    如果,要想在模拟器中看到效果,还需要在DDMS的Emulator Control面板中进行一些设置。如果你看不到Emulator Control面板,可以从window->Show view->Other…中打开它:

     

    Emulator Control面板中,你可以找到一个Location Controls的地方:

     

    你可以在Longitude和Latitude中输入一个虚拟的经纬度,然后点击Send,可以把模拟器的位置修改为指定位置。此时运行程序,即可看到如图所示的信息:

     

    有时候,你可能出现“无法获取地理信息”的错误。这可能是没有开启“启用GPS卫星”选项。不要奇怪,在模拟器中,“使用无线网络”是无效的,使用该选项无法获取地理信息,因为模拟器中根本没有sim卡,也就无法通过基站来定位了。

    如果在真机上就不同了。如果机器没有内置GPS模块,那么启用GPS卫星选项反而无法进行定位,具体情况只有多试几次才能明白。

    @font-face { font-family: "宋体"; }@font-face { font-family: "Cambria"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 10pt; font-size: 12pt; font-family: "Times New Roman"; }div.Section1 { page: Section1; }

    Geocoder需要访问internet,在真机上调试请打开wifi或者3G网络。


  • 相关阅读:
    好记性不如烂笔头,要经常把学习的东西记录下来
    liunx git 已经设置了ssh key 还是需要帐号和密码的解决方法。
    解决composer ssl required for SSL/TLS 证书配置
    composer SSL: Handshake timed out 解决办法
    小程序解密报错:IV passed is 24 bytes long which is longer than the 16 expected by se
    PHP面向对象试题(基础部分)
    php 单例模式
    在Thinkphp里面加入验证码插件
    phpstorm 修改默认注释
    用PHP去掉文件头的Unicode签名(BOM)
  • 原文地址:https://www.cnblogs.com/encounter/p/2188508.html
Copyright © 2020-2023  润新知