技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁 > 教程 > 服務(wù)器類 >

Android高德地圖poi檢索仿微信發(fā)送位置實(shí)例代碼

來源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2017-10-31 06:00┆點(diǎn)擊:

  以了解一下。

  最近項(xiàng)目需求把發(fā)送定位模塊改成類似微信發(fā)送位置給好友的效果,我使用了高德地圖實(shí)現(xiàn)了一個(gè)demo,效果圖如下:

Android高德地圖poi檢索仿微信發(fā)送位置實(shí)例代碼 三聯(lián)

  從主界面中我們可以看到中心標(biāo)記上面顯示的就是我們定位的地址,下面是一個(gè)listview列表,第一條item的數(shù)據(jù)就是我們定位得到的地址,下面其余的都是我們根據(jù)定位得到的經(jīng)緯度通過poi周邊搜索得到的地址。我們進(jìn)行了如下操作:

  我們點(diǎn)擊列表的item,中心標(biāo)記會移動到該item對象的地址上面去。

  我們手動移動地圖的時(shí)候,中心標(biāo)記的地址會發(fā)生相應(yīng)的變化并且下面的列表也會發(fā)生相應(yīng)的變化。

  根據(jù)關(guān)鍵字poi搜索得到的列表,然后點(diǎn)擊item主界面立馬進(jìn)行更新操作。

  這里貼出主要代碼,首先我們進(jìn)行地圖地位初始化操作:

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_map);

  mapView.onCreate(savedInstanceState);// 此方法必須重寫

  if (aMap == null) {

  aMap = mapView.getMap();

  // 自定義系統(tǒng)定位小藍(lán)點(diǎn)

  MyLocationStyle myLocationStyle = new MyLocationStyle();

  // 設(shè)置小藍(lán)點(diǎn)的圖標(biāo)

  myLocationStyle.myLocationIcon(BitmapDescriptorFactory.

  fromResource(R.mipmap.ic_location_marker));// 設(shè)置小藍(lán)點(diǎn)的圖標(biāo)

  myLocationStyle.strokeColor(0x7F0070D9);// 設(shè)置圓形的邊框顏色

  myLocationStyle.radiusFillColor(0x130070D9);// 設(shè)置圓形的填充顏色

  // myLocationStyle.anchor(int,int)//設(shè)置小藍(lán)點(diǎn)的錨點(diǎn)

  myLocationStyle.strokeWidth(1.0f);// 設(shè)置圓形的邊框粗細(xì)

  aMap.setMyLocationStyle(myLocationStyle);

  aMap.setLocationSource(this);// 設(shè)置定位監(jiān)聽(1)

  aMap.setOnCameraChangeListener(this);//手動移動地圖監(jiān)聽 (2)

  aMap.getUiSettings().setMyLocationButtonEnabled(true);// 設(shè)置默認(rèn)定位按鈕是否顯示

  //設(shè)置為true表示顯示定位層并可觸發(fā)定位,false表示隱藏定位層并不可觸發(fā)定位,默認(rèn)是false

  aMap.setMyLocationEnabled(true);

  aMap.moveCamera(CameraUpdateFactory.zoomTo(17.5f));

  }

  //------------------------------------------添加中心標(biāo)記

  mMarkerOptions = new MarkerOptions();

  mMarkerOptions.draggable(false);//可拖放性

  mMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_tips_nearby));

  mCenterMarker = aMap.addMarker(mMarkerOptions);

  ViewTreeObserver vto = mapView.getViewTreeObserver();

  vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

  @Override

  public void onGlobalLayout() {

  mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

  mCenterMarker.setPositionByPixels(mapView.getWidth() >> 1, mapView.getHeight() >> 1);

  mCenterMarker.showInfoWindow();

  }

  });

  //---------------------------------------------初始化正反編碼類 (3)

  mGeocoderSearch = new GeocodeSearch(this);

  mGeocoderSearch.setOnGeocodeSearchListener(this);

  }

  我們注意重點(diǎn)關(guān)注在上面的三個(gè)監(jiān)聽回調(diào),1處是定位監(jiān)聽,有以下兩個(gè)回調(diào)方法:

  //-----------------地圖定位回調(diào)

  //激活定位

  @Override

  public void activate(OnLocationChangedListener onLocationChangedListener) {

  mListener = onLocationChangedListener;

  if (mlocationClient == null) {

  mlocationClient = new AMapLocationClient(this);

  mLocationOption = new AMapLocationClientOption();

  //設(shè)置定位監(jiān)聽

  mlocationClient.setLocationListener(this);(4)

  //設(shè)置為高精度定位模式

  mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);

  //設(shè)置定位參數(shù)

  mlocationClient.setLocationOption(mLocationOption);

  // 此方法為每隔固定時(shí)間會發(fā)起一次定位請求,為了減少電量消耗或網(wǎng)絡(luò)流量消耗,

  // 注意設(shè)置合適的定位時(shí)間的間隔(最小間隔支持為2000ms),并且在合適時(shí)間調(diào)用stopLocation()

  // 方法來取消定位請求

  // 在定位結(jié)束后,在合適的生命周期調(diào)用onDestroy()方法

  // 在單次定位情況下,定位無論成功與否,都無需調(diào)用stopLocation()方法移除請求,定位sdk內(nèi)部會移除

  mlocationClient.startLocation();

  }

  }

  //停止定位

  @Override

  public void deactivate() {

  mListener = null;

  if (mlocationClient != null) {

  mlocationClient.stopLocation();

  mlocationClient.onDestroy();

  }

  mlocationClient = null;

  }