国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android百度地圖定位后獲取周邊位置的實現代碼

2020-04-11 10:53:58
字體:
來源:轉載
供稿:網友

本文實例講解Android百度地圖定位后獲取周邊位置的實現代碼,分享給大家供大家參考,具體內容如下

效果圖:

具體代碼:

1.布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"       android:orientation="vertical"       android:layout_width="match_parent"       android:layout_height="match_parent">  <RelativeLayout      android:layout_width="match_parent"      android:layout_height="@dimen/height_top_bar"      android:background="@color/common_top_bar_dark"      android:gravity="center_vertical">    <Button        android:id="@+id/btn_location_back"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:drawableLeft="@drawable/back"        android:text="@string/top_back"        style="@style/btn_title_bar"        android:layout_alignParentLeft="true"        android:onClick="back"        />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="@string/location_message"        style="@style/txt_titlebar_message"/>    <Button        android:id="@+id/btn_location_ok"        android:layout_width="52dp"        android:layout_height="match_parent"        android:layout_alignParentRight="true"        android:background="@drawable/common_tab_bg"        android:text="@string/txt_queding"        style="@style/btn_title_bar"/>  </RelativeLayout>  <com.baidu.mapapi.map.MapView      android:layout_weight="2"      android:id="@+id/mapview_location"      android:layout_width="fill_parent"      android:layout_height="match_parent"      android:clickable="true" />  <ListView      android:layout_weight="3"      android:id="@+id/lv_location_nearby"      android:layout_width="match_parent"      android:layout_height="match_parent"/></LinearLayout>

布局文件就是上面是一個百度地圖的mapview,下面是一個顯示周邊位置的ListView,很簡單。

1、自動定位

我們先看一下根據自己的地理位置實現定位
1.首先初始化要用到的組件

 /**   * 初始化組件   */  private void initView() {    btnLocationBack = (Button) findViewById(R.id.btn_location_back);    btnLocationBack.setOnClickListener(this);    btnLocationOk = (Button) findViewById(R.id.btn_location_ok);    btnLocationOk.setOnClickListener(this);    mapViewLocation = (MapView) findViewById(R.id.mapview_location);    lvLocNear = (ListView) findViewById(R.id.lv_location_nearby);    nearList = new ArrayList<PoiInfo>();    adapter = new LocNearAddressAdapter(context, nearList, isSelected);    lvLocNear.setAdapter(adapter);  }

2.初始化LocationClient類,該類需要在主線程中聲明

public LocationClient mLocationClient = null;public BDLocationListener myListener = new MyLocationListener();public void onCreate() {  mLocationClient = new LocationClient(getApplicationContext());   //聲明LocationClient類  mLocationClient.registerLocationListener( myListener );  //注冊監聽函數}

3.配置定位SDK參數
設置定位參數包括:定位模式(高精度定位模式,低功耗定位模式和僅用設備定位模式),返回坐標類型,是否打開GPS,是否返回地址信息、位置語義化信息、POI信息等等。
LocationClientOption類,該類用來設置定位SDK的定位方式

private void initLocation(){    LocationClientOption option = new LocationClientOption();    option.setLocationMode(LocationMode.Hight_Accuracy);//可選,默認高精度,設置定位模式,高精度,低功耗,僅設備    option.setCoorType("bd09ll");//可選,默認gcj02,設置返回的定位結果坐標系    int span=1000;    option.setScanSpan(span);//可選,默認0,即僅定位一次,設置發起定位請求的間隔需要大于等于1000ms才是有效的    option.setIsNeedAddress(true);//可選,設置是否需要地址信息,默認不需要    option.setOpenGps(true);//可選,默認false,設置是否使用gps    option.setLocationNotify(true);//可選,默認false,設置是否當gps有效時按照1S1次頻率輸出GPS結果    option.setIsNeedLocationDescribe(true);//可選,默認false,設置是否需要位置語義化結果,可以在BDLocation.getLocationDescribe里得到,結果類似于“在北京天安門附近”    option.setIsNeedLocationPoiList(true);//可選,默認false,設置是否需要POI結果,可以在BDLocation.getPoiList里得到option.setIgnoreKillProcess(false);//可選,默認false,定位SDK內部是一個SERVICE,并放到了獨立進程,設置是否在stop的時候殺死這個進程,默認殺死    option.SetIgnoreCacheException(false);//可選,默認false,設置是否收集CRASH信息,默認收集option.setEnableSimulateGps(false);//可選,默認false,設置是否需要過濾gps仿真結果,默認需要    mLocationClient.setLocOption(option);  }

4.實現BDLocationListener接口

 /**   * 監聽函數,有新位置的時候,格式化成字符串,輸出到屏幕中   */  public class MyLocationListenner implements BDLocationListener {    @Override    public void onReceiveLocation(BDLocation location) {      if (location == null) {        return;      }      Log.d("map", "On location change received:" + location);      Log.d("map", "addr:" + location.getAddrStr());      if (progressDialog != null) {        progressDialog.dismiss();      }      if (lastLocation != null) {        if (lastLocation.getLatitude() == location.getLatitude() && lastLocation.getLongitude() == location.getLongitude()) {          Log.d("map", "same location, skip refresh");          // mMapView.refresh(); //need this refresh?          return;        }      }      lastLocation = location;      mBaiduMap.clear();      mCurrentLantitude = lastLocation.getLatitude();      mCurrentLongitude = lastLocation.getLongitude();      Log.e(">>>>>>>", mCurrentLantitude + "," + mCurrentLongitude);      LatLng llA = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());      CoordinateConverter converter = new CoordinateConverter();      converter.coord(llA);      converter.from(CoordinateConverter.CoordType.COMMON);      LatLng convertLatLng = converter.convert();      OverlayOptions ooA = new MarkerOptions().position(convertLatLng).icon(BitmapDescriptorFactory          .fromResource(R.drawable.icon_marka))          .zIndex(4).draggable(true);      mCurrentMarker = (Marker) mBaiduMap.addOverlay(ooA);      MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(convertLatLng, 16.0f);      mBaiduMap.animateMapStatus(u);      new Thread(new Runnable() {        @Override        public void run() {          searchNeayBy();        }      }).start();    }    public void onReceivePoi(BDLocation poiLocation) {      if (poiLocation == null) {        return;      }    }  }

這里接受到的BDLocation中包含好多參數,相信總有一個對你有用的。

2、根據經緯度定位

這種方法不需要自動定位,就是根據經緯度來顯示地圖上的位置

 /*  * 顯示經緯度的位置  * */  private void showMap(double latitude, double longtitude, String address) {//    sendButton.setVisibility(View.GONE);    LatLng llA = new LatLng(latitude, longtitude);    CoordinateConverter converter = new CoordinateConverter();    converter.coord(llA);    converter.from(CoordinateConverter.CoordType.COMMON);    LatLng convertLatLng = converter.convert();    OverlayOptions ooA = new MarkerOptions().position(convertLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_marka))        .zIndex(4).draggable(true);    markerA = (Marker) (mBaiduMap.addOverlay(ooA));    u = MapStatusUpdateFactory.newLatLngZoom(convertLatLng, 16.0f);    mBaiduMap.animateMapStatus(u);    new Thread(new Runnable() {      @Override      public void run() {        searchNeayBy();      }    }).start();  }

3、獲取周邊地理位置

最后看一下怎么獲取周邊的地理位置,這里需要用到SDK中的一個類PoiNearbySearchOption,我們可以看一下類參考:

  • PoiNearbySearchOption keyword(java.lang.String key)
  • 檢索關鍵字
  • PoiNearbySearchOption location(LatLng location)
  • 檢索位置
  • PoiNearbySearchOption pageCapacity(int pageCapacity)
  • 設置每頁容量,默認為每頁10條
  • PoiNearbySearchOption pageNum(int pageNum)
  • 分頁編號
  • PoiNearbySearchOption radius(int radius)
  • 設置檢索的半徑范圍
  • PoiNearbySearchOption sortType(PoiSortType sortType)
  • 搜索結果排序規則,可選,默認

這里是它的一些方法,我們可以看到我們只需要設置一下關鍵字、周邊位置半徑、檢索位置、排序規則、分頁號、每頁數量等。然后我們實現OnGetPoiSearchResultListener這個接口,獲取周邊地理位置結果。

/**   * 搜索周邊地理位置   */  private void searchNeayBy() {    PoiNearbySearchOption option = new PoiNearbySearchOption();    option.keyword("寫字樓");    option.sortType(PoiSortType.distance_from_near_to_far);    option.location(new LatLng(mCurrentLantitude, mCurrentLongitude));    if (radius != 0) {      option.radius(radius);    } else {      option.radius(1000);    }    option.pageCapacity(20);    mPoiSearch.searchNearby(option);  }/** 接受周邊地理位置結果* @param poiResult*/  @Override  public void onGetPoiResult(PoiResult poiResult) {    if (poiResult != null) {      if (poiResult.getAllPoi()!=null&&poiResult.getAllPoi().size()>0){        nearList.addAll(poiResult.getAllPoi());        if (nearList != null && nearList.size() > 0) {          for (int i = 0; i < nearList.size(); i++) {            isSelected.put(i, false);          }        }        Message msg = new Message();        msg.what = 0;        handler.sendMessage(msg);      }    }  }

獲取完數據之后更新適配器顯示周邊位置就OK了,最后再實現一個小小的功能,就是點擊列表中的每個位置,顯示位置的小圖標根據位置的改變而改變

 /**     * 周邊地理位置列表點擊事件     */    lvLocNear.setOnItemClickListener(new AdapterView.OnItemClickListener() {      @Override      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {        adapter.setSelected(i);        adapter.notifyDataSetChanged();        PoiInfo ad = (PoiInfo) adapter.getItem(i);        u = MapStatusUpdateFactory.newLatLng(ad.location);        mBaiduMap.animateMapStatus(u);        if (!isLoc) {          mCurrentMarker.setPosition(ad.location);        } else {          markerA.setPosition(ad.location);        }      }    });

好了,很簡單有用的一個小功能,會給用戶帶來很好的體驗效果。

希望大家會喜歡這篇文章。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 聂拉木县| 巴南区| 湖南省| 长寿区| 枣强县| 定日县| 定州市| 东莞市| 长垣县| 广平县| 积石山| 尼玛县| 友谊县| 安龙县| 大渡口区| 武威市| 高淳县| 嘉善县| 株洲县| 宜都市| 平凉市| 那坡县| 长宁县| 织金县| 庆阳市| 图木舒克市| 泰州市| 肇州县| 阿拉尔市| 于田县| 闽侯县| 正阳县| 祁门县| 黔江区| 景洪市| 绥化市| 凤山市| 黄冈市| 炎陵县| 台南县| 聊城市|