百度地图定位基础 (六)

2014-11-24 11:17:34 · 作者: · 浏览: 9
nClick(View v) {
switch (v.getId()) {
case 11:
//发起定位请求。请求过程是异步的,定位结果在上面的监听函数onReceiveLocation中获取。
if (mLocationClient != null && mLocationClient.isStarted()) {
mLocationClient.requestLocation();
}else {
Log.e("LocSDK3", "locClient is null or not started");
}
break;
case 12:
//发起POI查询请求。请求过程是异步的,定位结果在上面的监听函数onReceivePoi中获取
if (mLocationClient != null && mLocationClient.isStarted()) {
mLocationClient.requestPoi();
}
break;
case 13:
if (!clickNotify) {
clickNotify = true;
//位置提醒最多提醒3次,3次过后将不再提醒。 假如需要再次提醒,或者要修改提醒点坐标,都可通过函数SetNotifyLocation()来实现
//位置提醒相关代码
mNotifyer = new NotifyLister();
mNotifyer.SetNotifyLocation(42.03249652949337,113.3129895882556,3000,"gps");//4个参数代表要位置提醒的点的坐标,具体含义依次为:纬度,经度,距离范围,坐标系类型(gcj02,gps,bd09,bd09ll)
mLocationClient.registerNotify(mNotifyer);
}else {
clickNotify = false;
//取消位置提醒
mLocationClient.removeNotifyEvent(mNotifyer);
}
break;
case 14:
/*
* 发起离线定位请求。请求过程是异步的,定位结果在上面的监听函数onReceiveLocation中获取。
* getLocTypte = BDLocation.TypteOfflineLocation || BDLocation.TypeOfflineLocationFail
* 表示是离线定位请求返回的定位结果
*/
if (mLocationClient != null && mLocationClient.isStarted()) {
mLocationClient.requestOfflineLocation();
}
break;
}
}

//获取屏幕的宽度,高度和密度以及dp / px
public void getDisplayMetrics() {
DisplayMetrics dm = new DisplayMetrics();
dm = getApplicationContext().getResources().getDisplayMetrics();
Screen_width = dm.widthPixels;
Screen_height = dm.heightPixels;
scale = getResources().getDisplayMetrics().density;
}

public int dip2px(float dpValue) {
return (int)(dpValue * scale + 0.5f);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.local, menu);
return true;
}

@Override
protected void onStop() {
super.onStop();
if (mLocationClient != null) {
mLocationClient.stop();
mLocationClient = null;
}
}

public class MyLocationListener implements BDLocationListener{

//接收异步返回的定位结果,参数是BDLocation类型参数
@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("\nlontitude: ");
sb.append(location.getLongitude());
sb.append("\nradius: ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation) {
sb.append("\nspedd: ");
sb.append(location.getSpeed());
sb.append("\nsatellite: ");
sb.append(location.getSatelliteNumber());
}else if(location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\naddr: ");
sb.append(location.getAddrStr());
}else if(location.getLocType() == BDLocation.TypeOffLineLocation || location.getLocType() == BDLocation.TypeOffLineLocationNetworkFail){

}
if (contentTextView != null) {
contentTextView.setText(sb.toString());
}
}

//接收异步返回的POI查询结果,参数是BDLocation类型参数
@Override
public void onReceivePoi(BDLocation arg0) {

}
}

//BDNotifyListener实现
public class NotifyLister extends BDNotifyListener{