您好,关于android.location.Location 定位实例化的问题!

我在维护一个老的项目,以前定位的时候用的是百度定位,但是现在更新了sdk。定位的时候需要得到 android.location.Location location,作为一个方法的参数。请问有什么办法能实例化一个 android.location.Location 实例?(精度和纬度等信息从百度定位中能得到)

紫衣仙女
浏览 669回答 2
2回答

繁花不似锦

一、LocationManagerLocationMangager,位置管理器。要想操作定位相关设备,必须先定义个LocationManager。我们可以通过如下代码创建LocationManger对象。LocationManger locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);二、LocationListenerLocationListener,位置监听,监听位置变化,监听设备开关与状态。private LocationListener locationListener=new LocationListener() {/*** 位置信息变化时触发*/public void onLocationChanged(Location location) {updateView(location);Log.i(TAG, "时间:"+location.getTime());Log.i(TAG, "经度:"+location.getLongitude());Log.i(TAG, "纬度:"+location.getLatitude());Log.i(TAG, "海拔:"+location.getAltitude());}/*** GPS状态变化时触发*/public void onStatusChanged(String provider, int status, Bundle extras) {switch (status) {//GPS状态为可见时case LocationProvider.AVAILABLE:Log.i(TAG, "当前GPS状态为可见状态");break;//GPS状态为服务区外时case LocationProvider.OUT_OF_SERVICE:Log.i(TAG, "当前GPS状态为服务区外状态");break;//GPS状态为暂停服务时case LocationProvider.TEMPORARILY_UNAVAILABLE:Log.i(TAG, "当前GPS状态为暂停服务状态");break;}}/*** GPS开启时触发*/public void onProviderEnabled(String provider) {Location location=lm.getLastKnownLocation(provider);updateView(location);}/*** GPS禁用时触发*/public void onProviderDisabled(String provider) {updateView(null);}};三、LocationLocation,位置信息,通过Location可以获取时间、经纬度、海拔等位置信息。上面采用locationListener里面的onLocationChanged()来获取location,下面讲述如何主动获取location。Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);system.out.println("时间:"+location.getTime());system.out.println("经度:"+location.getLongitude());注意:Location location=new Location(LocationManager.GPS_PROVIDER)方式获取的location的各个参数值都是为0。四、GpsStatus.ListenerGpsStatus.Listener ,GPS状态监听,包括GPS启动、停止、第一次定位、卫星变化等事件。//状态监听GpsStatus.Listener listener = new GpsStatus.Listener() {public void onGpsStatusChanged(int event) {switch (event) {//第一次定位case GpsStatus.GPS_EVENT_FIRST_FIX:Log.i(TAG, "第一次定位");break;//卫星状态改变case GpsStatus.GPS_EVENT_SATELLITE_STATUS:Log.i(TAG, "卫星状态改变");//获取当前状态GpsStatus gpsStatus=lm.getGpsStatus(null);//获取卫星颗数的默认最大值int maxSatellites = gpsStatus.getMaxSatellites();//创建一个迭代器保存所有卫星Iterator<GpsSatellite> iters = gpsStatus.getSatellites().iterator();int count = 0;while (iters.hasNext() && count <= maxSatellites) {GpsSatellite s = iters.next();count++;}System.out.println("搜索到:"+count+"颗卫星");break;//定位启动case GpsStatus.GPS_EVENT_STARTED:Log.i(TAG, "定位启动");break;//定位结束case GpsStatus.GPS_EVENT_STOPPED:Log.i(TAG, "定位结束");break;}};};//绑定监听状态lm.addGpsStatusListener(listener);五、GpsStatusGpsStatus,GPS状态信息,上面在卫星状态变化时,我们就用到了GpsStatus。//实例化GpsStatus gpsStatus = locationManager.getGpsStatus(null); // 获取当前状态//获取默认最大卫星数int maxSatellites = gpsStatus.getMaxSatellites();//获取第一次定位时间(启动到第一次定位)int costTime=gpsStatus.getTimeToFirstFix();//获取卫星Iterable<GpsSatellite> iterable=gpsStatus.getSatellites();//一般再次转换成IteratorIterator<GpsSatellite> itrator=iterable.iterator();六、GpsSatelliteGpsSatellite,定位卫星,包含卫星的方位、高度、伪随机噪声码、信噪比等信息。//获取卫星Iterable<GpsSatellite> iterable=gpsStatus.getSatellites();//再次转换成IteratorIterator<GpsSatellite> itrator=iterable.iterator();//通过遍历重新整理为ArrayListArrayList<GpsSatellite> satelliteList=new ArrayList<GpsSatellite>();int count=0;int maxSatellites=gpsStatus.getMaxSatellites();while (itrator.hasNext() && count <= maxSatellites) {GpsSatellite satellite = itrator.next();satelliteList.add(satellite);count++;}System.out.println("总共搜索到"+count+"颗卫星");//输出卫星信息for(int i=0;i<satelliteList.size();i++){//卫星的方位角,浮点型数据System.out.println(satelliteList.get(i).getAzimuth());//卫星的高度,浮点型数据System.out.println(satelliteList.get(i).getElevation());//卫星的伪随机噪声码,整形数据System.out.println(satelliteList.get(i).getPrn());//卫星的信噪比,浮点型数据System.out.println(satelliteList.get(i).getSnr());//卫星是否有年历表,布尔型数据System.out.println(satelliteList.get(i).hasAlmanac());//卫星是否有星历表,布尔型数据System.out.println(satelliteList.get(i).hasEphemeris());//卫星是否被用于近期的GPS修正计算System.out.println(satelliteList.get(i).hasAlmanac());}为了便于理解,接下来模拟一个案例,如何在程序代码中使用GPS获取位置信息。第一步:新建一个Android工程项目,命名为mygps,目录结构如下第二步:修改main.xml布局文件,修改内容如下:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><EditText android:layout_width="fill_parent"android:layout_height="wrap_content"android:cursorVisible="false"android:editable="false"android:id="@+id/editText"/></LinearLayout>第三步:实用Adnroid平台的GPS设备,需要添加上权限<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>第四步:修改核心组件activity,修改内容如下package com.ljq.activity;import java.util.Iterator;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.location.Criteria;import android.location.GpsSatellite;import android.location.GpsStatus;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.location.LocationProvider;import android.os.Bundle;import android.provider.Settings;import android.util.Log;import android.widget.EditText;import android.widget.Toast;public class GpsActivity extends Activity {private EditText editText;private LocationManager lm;private static final String TAG="GpsActivity"; @Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();lm.removeUpdates(locationListener);}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);editText=(EditText)findViewById(R.id.editText);lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);//判断GPS是否正常启动if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){Toast.makeText(this, "请开启GPS导航...", Toast.LENGTH_SHORT).show();//返回开启GPS导航设置界面Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);startActivityForResult(intent,0);return;}//为获取地理位置信息时设置查询条件String bestProvider = lm.getBestProvider(getCriteria(), true);//获取位置信息//如果不设置查询要求,getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDERLocation location= lm.getLastKnownLocation(bestProvider);updateView(location);//监听状态lm.addGpsStatusListener(listener);//绑定监听,有4个参数//参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种//参数2,位置信息更新周期,单位毫秒//参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息//参数4,监听//备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新// 1秒更新一次,或最小位移变化超过1米更新一次;//注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);然后执行handler.sendMessage(),更新位置lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);}//位置监听private LocationListener locationListener=new LocationListener() {/*** 位置信息变化时触发*/public void onLocationChanged(Location location) {updateView(location);Log.i(TAG, "时间:"+location.getTime());Log.i(TAG, "经度:"+location.getLongitude());Log.i(TAG, "纬度:"+location.getLatitude());Log.i(TAG, "海拔:"+location.getAltitude());}/*** GPS状态变化时触发*/public void onStatusChanged(String provider, int status, Bundle extras) {switch (status) {//GPS状态为可见时case LocationProvider.AVAILABLE:Log.i(TAG, "当前GPS状态为可见状态");break;//GPS状态为服务区外时case LocationProvider.OUT_OF_SERVICE:Log.i(TAG, "当前GPS状态为服务区外状态");break;//GPS状态为暂停服务时case LocationProvider.TEMPORARILY_UNAVAILABLE:Log.i(TAG, "当前GPS状态为暂停服务状态");break;}}/*** GPS开启时触发*/public void onProviderEnabled(String provider) {Location location=lm.getLastKnownLocation(provider);updateView(location);}/*** GPS禁用时触发*/public void onProviderDisabled(String provider) {updateView(null);}};

小唯快跑啊

[java] view plain copypackage com.baidu.mapapi.demo;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.location.Criteria;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.provider.Settings;import android.widget.TextView;import android.widget.Toast;
打开App,查看更多内容
随时随地看视频慕课网APP