查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧!
最近应用百度地图,查看了官方的说明,然后做了一个Demo,作为入门,如果看了我之前的代码,有一个习气就是应用代码写布局,感觉这样比较快,习气而已。
源码如下:
package com.zhangjie.local; import android.os.Bundle; import android.os.Vibrator; import android.app.Activity; import android.app.Service; import android.util.DisplayMetrics; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.location.BDNotifyListener;//假如用到位置提示功能,须要import该类 public class Local extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getDisplayMetrics(); initLayout(); setContentView(Parent); myListener = new MyLocationListener(); mLocationClient = new LocationClient(getApplicationContext()); mLocationClient.registerLocationListener(myListener); //设置定位参数包含:定位模式(单次定位,准时定位),返回坐标类型,是否打开GPS等等 option = new LocationClientOption(); option.setOpenGps(true); option.setAddrType("all");//返回定位结果包含地址信息 option.setCoorType("bd0911");//返回的定位结果是百度经纬度,默认值gcj02 option.setScanSpan(5000);//设置发起请求的时间间隔为5000ms option.disableCache(true);//制止开启缓存定位 option.setPoiNumber(5);//最多返回POI个数 option.setPoiDistance(1000);//poi查询距离 option.setPoiExtraInfo(true);//是否须要POI的电话和地址等详细信息 mLocationClient.setLocOption(option); mLocationClient.start(); } /** * 初始化布局 */ public void initLayout(){ Parent = new RelativeLayout(this); bottomLayout = new LinearLayout(this); bottomLayout.setId(10); contentTextView = new TextView(this); contentTextView.setText(R.string.content); localButton = new Button(this); localButton.setText(R.string.localrequest); localButton.setId(11); localButton.setOnClickListener(this); poiButton = new Button(this); poiButton.setText(R.string.poirequest); poiButton.setId(12); poiButton.setOnClickListener(this); notifyButton = new Button(this); notifyButton.setText(R.string.notify); notifyButton.setId(13); notifyButton.setOnClickListener(this); offlineButton = new Button(this); offlineButton.setText(R.string.offine); offlineButton.setId(14); offlineButton.setOnClickListener(this); //設置底部佈局的button int disten = (Screen_width - dip2px(buttonWidth) * 4) / 5; LinearLayout.LayoutParams buttonInBottomLayoutParams = new LinearLayout.LayoutParams(dip2px(buttonWidth), dip2px(buttonHeight)); buttonInBottomLayoutParams.leftMargin = disten; bottomLayout.addView(localButton, buttonInBottomLayoutParams); bottomLayout.addView(poiButton, buttonInBottomLayoutParams); bottomLayout.addView(notifyButton, buttonInBottomLayoutParams); bottomLayout.addView(offlineButton, buttonInBottomLayoutParams); RelativeLayout.LayoutParams bottomInParentLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); bottomInParentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); Parent.addView(bottomLayout, bottomInParentLayoutParams); //設置contentTextView佈局 RelativeLayout.LayoutParams contentInParentLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); contentInParentLayoutParams.addRule(RelativeLayout.ABOVE, 10); Parent.addView(contentTextView, contentInParentLayoutParams); } @Override public void onClick(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{ public void onNotify(BDLocationListener mListener, float distance){ if (mVibrator == null) { mVibrator = (Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE); } mVibrator.vibrate(1000);//振动提示已到设定位置附近 } } public LocationClient mLocationClient = null; LocationClientOption option; public BDLocationListener myListener; public NotifyLister mNotifyer; public Vibrator mVibrator; private TextView contentTextView; private Button localButton; private Button poiButton; private Button notifyButton; private Button offlineButton; private RelativeLayout Parent; private LinearLayout bottomLayout; public int Screen_width; public int Screen_height; public float scale; public int buttonWidth = 130;//dp public int buttonHeight = 50;//dp public boolean clickNotify = false; }
界面如下:
文章结束给大家分享下程序员的一些笑话语录:
问:你觉得让你女朋友(或者任何一个女的)从你和李彦宏之间选一个,你觉得她会选谁?
答:因为李艳红这种败类,所以我没女友!