android 使用LocationClient获取精度,纬度坐标
2014-02-28 19:37
429 查看
LocationClient类
用来发起定位,添加取消监听
LocationClientOption类
用来设置定位方式,包括是否启用缓存,使用gps,时间间隔等。
BDLocation类
定位结果的封装,包含坐标和错误码等信息。
BDLocationListener接口类
获取定位结果
BDNotifyListener 类
作用:位置提醒接口类,用于设定位置提醒点,以及实现监听函数。这个应该是适用于地图。
1.这是获取经纬度是调用的一个公共方法,实现精度纬度的获取
/**
* 获取精纬度
*
* @return
*/
public static void locate(Application application) {
// 定位服务
LocationClient mLocClient = ((Location)application).mLocationClient;
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开gps
// option.setCoorType(mCoorEdit.getText().toString()); // 设置坐标类型
option.setServiceName("com.baidu.location.service_v2.9");
option.setPoiExtraInfo(true);
option.setAddrType("all");
option.setScanSpan(300); // 设置定位模式,小于1秒则一次定位;大于等于1秒则定时定位
if (true) {
option.setPriority(LocationClientOption.NetWorkFirst); // 设置网络优先
} else {
option.setPriority(LocationClientOption.GpsFirst); // 不设置,默认是gps优先
}
option.setPoiNumber(10);
option.disableCache(true);
mLocClient.setLocOption(option);
mLocClient.start();//打开定位
if (mLocClient != null && mLocClient.isStarted()) {
mLocClient.requestLocation();
}
}
2,继承Application的Location类()
就是值得这个类中的一个mLocationClient变量
package com.example.asmack.location;
import com.baidu.location.*;
import android.app.Application;
import android.util.Log;
import android.widget.TextView;
import android.os.Process;
import android.os.Vibrator;
public class Location extends Application {
// 记录更新弹出框出现的次数
public LocationClient mLocationClient = null;
public MyLocationListenner myListener;
public static String TAG = "LocTestDemo";
@Override
public void onCreate() {
mLocationClient = new LocationClient(this);
myListener = new MyLocationListenner(this, mLocationClient);
mLocationClient.registerLocationListener(myListener);
super.onCreate();
Log.d(TAG, "... Application onCreate... pid=" + Process.myPid());
}
}
3,继承BDLocationListener抽象方法的类MyLocationListenner
该类中需要继承两个方法
/**
* 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
*/
public class MyLocationListenner implements BDLocationListener {
private Context context;
private LocationClient locationClient;
public MyLocationListenner(Context context, LocationClient locationClient) {
this.context = context;
this.locationClient = locationClient;
}
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return;
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation) {
sb.append("\nspeed : ");
sb.append(location.getSpeed());
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
// sb.append("\n省:");
// sb.append(location.getProvince());
// sb.append("\n市:");
// sb.append(location.getCity());
// sb.append("\n区/县:");
// sb.append(location.getDistrict());
sb.append("\naddr : ");
sb.append(location.getAddrStr());
}
String longitude = location.getLongitude() + "";
String latitude = location.getLatitude() + "";
String address = location.getAddrStr();
System.out.println("MyLocationListener location " + "精度 = " + longitude
+ "纬度 = " + latitude + "address = " + address);
}
public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null) {
return;
}
StringBuffer sb = new StringBuffer(256);
sb.append("Poi time : ");
sb.append(poiLocation.getTime());
sb.append("\nerror code : ");
sb.append(poiLocation.getLocType());
sb.append("\nlatitude : ");
sb.append(poiLocation.getLatitude());
sb.append("\nlontitude : ");
sb.append(poiLocation.getLongitude());
sb.append("\nradius : ");
sb.append(poiLocation.getRadius());
if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
sb.append("\naddr : ");
sb.append(poiLocation.getAddrStr());
}
if (poiLocation.hasPoi()) {
sb.append("\nPoi:");
sb.append(poiLocation.getPoi());
} else {
sb.append("noPoi information");
}
Log.d("Location", sb.toString());
}
}
用来发起定位,添加取消监听
LocationClientOption类
用来设置定位方式,包括是否启用缓存,使用gps,时间间隔等。
BDLocation类
定位结果的封装,包含坐标和错误码等信息。
BDLocationListener接口类
获取定位结果
BDNotifyListener 类
作用:位置提醒接口类,用于设定位置提醒点,以及实现监听函数。这个应该是适用于地图。
1.这是获取经纬度是调用的一个公共方法,实现精度纬度的获取
/**
* 获取精纬度
*
* @return
*/
public static void locate(Application application) {
// 定位服务
LocationClient mLocClient = ((Location)application).mLocationClient;
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开gps
// option.setCoorType(mCoorEdit.getText().toString()); // 设置坐标类型
option.setServiceName("com.baidu.location.service_v2.9");
option.setPoiExtraInfo(true);
option.setAddrType("all");
option.setScanSpan(300); // 设置定位模式,小于1秒则一次定位;大于等于1秒则定时定位
if (true) {
option.setPriority(LocationClientOption.NetWorkFirst); // 设置网络优先
} else {
option.setPriority(LocationClientOption.GpsFirst); // 不设置,默认是gps优先
}
option.setPoiNumber(10);
option.disableCache(true);
mLocClient.setLocOption(option);
mLocClient.start();//打开定位
if (mLocClient != null && mLocClient.isStarted()) {
mLocClient.requestLocation();
}
}
2,继承Application的Location类()
就是值得这个类中的一个mLocationClient变量
package com.example.asmack.location;
import com.baidu.location.*;
import android.app.Application;
import android.util.Log;
import android.widget.TextView;
import android.os.Process;
import android.os.Vibrator;
public class Location extends Application {
// 记录更新弹出框出现的次数
public LocationClient mLocationClient = null;
public MyLocationListenner myListener;
public static String TAG = "LocTestDemo";
@Override
public void onCreate() {
mLocationClient = new LocationClient(this);
myListener = new MyLocationListenner(this, mLocationClient);
mLocationClient.registerLocationListener(myListener);
super.onCreate();
Log.d(TAG, "... Application onCreate... pid=" + Process.myPid());
}
}
3,继承BDLocationListener抽象方法的类MyLocationListenner
该类中需要继承两个方法
/**
* 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
*/
public class MyLocationListenner implements BDLocationListener {
private Context context;
private LocationClient locationClient;
public MyLocationListenner(Context context, LocationClient locationClient) {
this.context = context;
this.locationClient = locationClient;
}
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return;
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation) {
sb.append("\nspeed : ");
sb.append(location.getSpeed());
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
// sb.append("\n省:");
// sb.append(location.getProvince());
// sb.append("\n市:");
// sb.append(location.getCity());
// sb.append("\n区/县:");
// sb.append(location.getDistrict());
sb.append("\naddr : ");
sb.append(location.getAddrStr());
}
String longitude = location.getLongitude() + "";
String latitude = location.getLatitude() + "";
String address = location.getAddrStr();
System.out.println("MyLocationListener location " + "精度 = " + longitude
+ "纬度 = " + latitude + "address = " + address);
}
public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null) {
return;
}
StringBuffer sb = new StringBuffer(256);
sb.append("Poi time : ");
sb.append(poiLocation.getTime());
sb.append("\nerror code : ");
sb.append(poiLocation.getLocType());
sb.append("\nlatitude : ");
sb.append(poiLocation.getLatitude());
sb.append("\nlontitude : ");
sb.append(poiLocation.getLongitude());
sb.append("\nradius : ");
sb.append(poiLocation.getRadius());
if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
sb.append("\naddr : ");
sb.append(poiLocation.getAddrStr());
}
if (poiLocation.hasPoi()) {
sb.append("\nPoi:");
sb.append(poiLocation.getPoi());
} else {
sb.append("noPoi information");
}
Log.d("Location", sb.toString());
}
}
相关文章推荐
- android 使用LocationClient获取精度,纬度坐标
- Android Path和PathMeasure类的使用之获取圆弧上的坐标值
- Android——使用百度API获取经度纬度以及所在城市
- ios 利用自带的CoreLocation.framework 实现获取手机当前处于的精度和纬度 并计算两点之间的间距(特别使用于团购,附近商户之类的项目)
- Android中获取应用程序(包)的大小-----PackageManager的使用(二)
- Android中获取正在运行的服务-------ActivityManager.RunningServiceInfo的使用
- Android——使用contentprovider获取内容
- Android 使用WebView 时获取数据出现编码不正确的解决方法
- 飞鸽传书(飞秋) android使用udp 深入剖析 -- (1.如何获取好友列表)
- Android中获取应用程序(包)的信息-----PackageManager的使用(一)
- Android 使用CursorLoader获取手机短信
- Android 属性自定义及使用获取浅析
- android菜鸟学习笔记31----Android使用百度地图API(二)获取地理位置及地图控制器的简单使用
- getLocationInWindow getLocationOnScreen 获取Android坐标
- Android 获取地理位置的经度和纬度
- Android Map Api 使用和开发(3)浮动搜索框 ,通过地址名称获取经纬度和详细地址并定位
- PC端使用opencv获取webcam,通过socket把Mat图像传输到android手机端
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- android MVP模式使用retroflt获取数据加载到recyclerview
- Android中获取应用程序(包)的信息-----PackageManager的使用