MyApplication

/**
 * 软件应用类
 * 
 * @author yang_qingan
 * 
 */
public class MyApplication extends Application {

public LocationClient mLocationClient;
public MyLocationListener mMyLocationListener;


public static double nlatitude = 0;


public static double nlontitude = 0;

public static Boolean havelocation = false;

private myLocation myLocation;

    protected static MyApplication application;
    public static String cityName="",cityId="";
    
    public void onCreate() {
super.onCreate();
application = this;
EaseUI.getInstance().init(application);

mLocationClient = new LocationClient(this.getApplicationContext());
mMyLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mMyLocationListener);
initLocation();
mLocationClient.start();
    }


    /**
     * 获得应用实例
     * 
     * @return
     */
    public static MyApplication getApplication() {
return application;
    }


    /**
     * 获得屏幕尺寸
     * 
     * @return
     */
    public Point getScreenSize() {
WindowManager windowManager = (WindowManager) MyApplication
.getApplication().getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point p = new Point();
display.getSize(p);
return p;
    }


    public float getDenstiy() {
WindowManager windowManager = (WindowManager) MyApplication
.getApplication().getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics metric = new DisplayMetrics();
display.getMetrics(metric);
return metric.density;
    }


    public int getDpi() {
WindowManager windowManager = (WindowManager) MyApplication
.getApplication().getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics metric = new DisplayMetrics();
display.getMetrics(metric);
return metric.densityDpi;
    }


    /**
     * DisplayMetrics metric = new DisplayMetrics();
     * getWindowManager().getDefaultDisplay().getMetrics(metric); int width =
     * metric.widthPixels; // 屏幕宽度(像素) int height = metric.heightPixels; //
     * 屏幕高度(像素) float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5) int
     * densityDpi = metric.densityDpi; // 屏幕密度DPI(120 / 160 / 240)
     */


    /*public static BitmapUtils getBitmapUtils() {
//synchronized (application) {
   //if (bitmapUtils == null) {
    BitmapUtils bitmapUtils = new BitmapUtils(application);
// bitmapUtils.configDefaultLoadingImage(R.drawable.load_default);
bitmapUtils.configDefaultBitmapMaxSize(BitmapCommonUtils
.getScreenSize(application));
  // }
//}
return bitmapUtils;
    }*/
    
public void getmyLocation(myLocation myLocation) {
this.myLocation = myLocation;
if (!havelocation) {
// mLocationClient.start();
myLocation.result(0, 0, false);
} else {
myLocation.result(nlatitude, nlontitude, true);
}
myLocation = null;
}
    
/**
* 初始化百度地图
*/
private void initLocation() {
LocationMode tempMode = LocationMode.Hight_Accuracy;
String tempcoor = "gcj02";
LocationClientOption option = new LocationClientOption();
option.setLocationMode(tempMode);// 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType(tempcoor);// 可选,默认gcj02,设置返回的定位结果坐标系,
int span = 10000;
option.setScanSpan(span);// 可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setIsNeedAddress(true);// 可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);// 可选,默认false,设置是否使用gps
option.setLocationNotify(true);// 可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
option.setIgnoreKillProcess(true);// 可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
option.setEnableSimulateGps(false);// 可选,默认false,设置是否需要过滤gps仿真结果,默认需要
option.setIsNeedLocationDescribe(true);// 可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
option.setIsNeedLocationPoiList(true);// 可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
mLocationClient.setLocOption(option);
}


/**
* 百度地图回调

* @author Administrator

*/
public interface myLocation {


void result(double nlatitude, double nlontitude, boolean success);


}


/**
* 百度地图

* @author Administrator

*/
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// Receive Location
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());
System.out.println("定位结果" + sb.toString());

if (location.getLocType() == BDLocation.TypeGpsLocation
|| location.getLocType() == BDLocation.TypeNetWorkLocation
|| location.getLocType() == BDLocation.TypeOffLineLocation
|| location.getLocType() == BDLocation.TypeCacheLocation) {// 定位成功
nlatitude = location.getLatitude();
nlontitude = location.getLongitude();
mLocationClient.stop();// 停止定位
// if(myLocation!=null){
// myLocation.result(nlatitude,nlontitude,true);
havelocation = true;
// }
} else {
havelocation = false;
// if(myLocation!=null){
// myLocation.result(nlatitude,nlontitude,false);
// }
}
// myLocation = null;
}
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值