百度地圖定位基礎

來源:互聯網
上載者:User

最近使用百度地圖,查看了官方的說明,然後做了一個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{@Overrideprotected 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");//返回的定位結果是百度經緯度,預設值gcj02option.setScanSpan(5000);//設定發起請求的時間間隔為5000msoption.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);//設置底部佈局的buttonint 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);}@Overridepublic 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);     } @Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.local, menu);return true;}@Overrideprotected void onStop() {super.onStop();if (mLocationClient != null) {mLocationClient.stop();mLocationClient = null;}}public class MyLocationListener implements BDLocationListener{//接收非同步返回的定位結果,參數是BDLocation型別參數@Overridepublic 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型別參數@Overridepublic 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;//dppublic int buttonHeight = 50;//dppublic boolean clickNotify = false;}

介面如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.