移动GIS开发:第一步安卓地图定位功能的实现

一.目标
获取当前手机定位坐标信息(WGS84坐标系)
二.实现过程
1.在布局界面添加一个TextView

  TextView
        android:id="@+id/location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="正在初始化..."

2.定义组件并通过id找到上述组件

private TextView text;  //定义用于显示LocationProvider的TextView组件
text = (TextView) findViewById(R.id.location);  //获取显示Location信息的TextView组件

3.获取系统的LocationManager对象,即位置信息管理员,从系统服务中获取

LocationManager locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);

4.从从GPS获取最新的定位信息

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

5.定义一个方法locationUpdates,用来在文本框组件中更新位置信息

    public void locationUpdates(Location location) {  //获取指定的查询信息
        //如果location不为空时
        if (location != null) {
            StringBuilder stringBuilder = new StringBuilder();        //使用StringBuilder保存数据
            //获取经度、纬度、等属性值
            stringBuilder.append("您的位置信息:\n");
            stringBuilder.append("经度:");
            stringBuilder.append(location.getLongitude());
            stringBuilder.append("\n纬度:");
            stringBuilder.append(location.getLatitude());
//            stringBuilder.append("\n精确度:");
//            stringBuilder.append(location.getAccuracy());
//            stringBuilder.append("\n高度:");
//            stringBuilder.append(location.getAltitude());
//            stringBuilder.append("\n方向:");
//            stringBuilder.append(location.getBearing());
//            stringBuilder.append("\n速度:");
//            stringBuilder.append(location.getSpeed());
//            stringBuilder.append("\n时间:");
//            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH mm ss");    //设置日期时间格式
//            stringBuilder.append(dateFormat.format(new Date(location.getTime())));
            text.setText(stringBuilder);            //显示获取的信息
        } else {
            //否则输出空信息
            text.setText("没有获取到GPS信息");
        }
    }

6.将最新的定位信息传递给创建的locationUpdates()方法中

locationUpdates(location); 

7.设置LocationManager的requestLocationUpdates(获取位置信息方法)的参数,实现在一定时间间隔进行位置更新。

   locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,      //GPS定位提供者
                1000,       //更新数据时间为1秒
                1,      //位置间隔为1米
                //位置监听器
                new LocationListener() {  //GPS定位信息发生改变时触发,用于更新位置信息

                @Override
                public void onLocationChanged(Location location) {
                    //GPS信息发生改变时,更新位置
                    locationUpdates(location);
                }

                @Override
                //位置状态发生改变时触发
                public void onStatusChanged(String provider, int status, Bundle extras) {
                }

                @Override
                //定位提供者启动时触发
                public void onProviderEnabled(String provider) {
                }

                @Override
                //定位提供者关闭时触发
                public void onProviderDisabled(String provider) {
                }
            });     

8.赋予app相应权限,在manifest中添加

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

9.在运行期间遇到的一个小bug在此记录,即中文字符乱码,在app文件夹目录里的build.gradle文件下添加

 android{compileOptions.encoding="GBK"}

三.总结
获取位置总体流程
1)从系统服务中申请一个负责管理位置的管家(LocationManager对象)
2)从gps获取最新的坐标存到location对象
3)写一个方法(locationUpdates)将坐标信息更新到目标文本框
4)通知管家(LocationManager对象)每隔多久更新一次坐标信息,精度多少,即设置LocationManager的requestLocationUpdates(获取位置信息方法)的参数。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值