android 使用LocationClient获取精度,纬度坐标

     

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());
}


}




你可能感兴趣的:(android 使用LocationClient获取精度,纬度坐标)