1. 程式人生 > >高德定位+poi搜尋

高德定位+poi搜尋

package com.amap.map2d.demo.location;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.map2d.demo.R;
import com.amap.map2d.demo.adapter.CustomLocationAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * AMapV2地圖中介紹自定義定位小藍點+搜尋周邊商務住宅
 */
public class CustomLocationActivity extends Activity implements LocationSource,
        AMapLocationListener {
    private AMap aMap;
    private MapView mapView;
    private OnLocationChangedListener mListener;
    private AMapLocationClient mlocationClient;
    private AMapLocationClientOption mLocationOption;
    private RadioGroup mGPSModeGroup;

    private TextView mLocationErrText;
    private static final int STROKE_COLOR = Color.argb(180, 3, 145, 255);
    private static final int FILL_COLOR = Color.argb(10, 0, 0, 180);
    private ListView lv_listaddress;
    private CustomLocationAdapter madapter;
    private List list;
    private PoiSearch.Query query;
    private int currentPage = 0;
    private LatLonPoint lp;//當前位置
    private PoiSearch poiSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("onCreate", "onCreate++++++");
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 不顯示程式的標題欄
        setContentView(R.layout.locationsource_activity);
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 此方法必須重寫
        init();
        initlv();
    }

    private void initlv() {
        list = new ArrayList<>();
        lv_listaddress = (ListView) findViewById(R.id.lv_listaddress);

    }

    /**
     * 初始化
     */
    private void init() {
        Log.e("init", "init++++++");

        if (aMap == null) {
            aMap = mapView.getMap();
            setUpMap();
        }

        mLocationErrText = (TextView) findViewById(R.id.location_errInfo_text);
        mLocationErrText.setVisibility(View.GONE);
    }

    /**
     * 設定一些amap的屬性
     */
    private void setUpMap() {
        Log.e("setUpMap", "setUpMap++++++");
        aMap.setLocationSource(this);// 設定定位監聽
        aMap.getUiSettings().setMyLocationButtonEnabled(true);// 設定預設定位按鈕是否顯示
        aMap.setMyLocationEnabled(true);// 設定為true表示顯示定位層並可觸發定位,false表示隱藏定位層並不可觸發定位,預設是false
        setupLocationStyle();
    }

    private void setupLocationStyle() {
        Log.e("setupLocationStyle", "setupLocationStyle++++++");

        // 自定義系統定位藍點
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        // 自定義定位藍點圖示
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.
                fromResource(R.drawable.gps_point));
        // 自定義精度範圍的圓形邊框顏色
        myLocationStyle.strokeColor(STROKE_COLOR);
        //自定義精度範圍的圓形邊框寬度
        myLocationStyle.strokeWidth(5);
        // 設定圓形的填充顏色
        myLocationStyle.radiusFillColor(FILL_COLOR);
        // 將自定義的 myLocationStyle 物件新增到地圖上
        aMap.setMyLocationStyle(myLocationStyle);
    }

    /**
     * 方法必須重寫
     */
    @Override
    protected void onResume() {
        Log.e("onResume", "onResume++++++");

        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必須重寫
     */
    @Override
    protected void onPause() {
        Log.e("onPause", "onPause++++++");

        super.onPause();
        mapView.onPause();
        deactivate();
    }

    /**
     * 方法必須重寫
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Log.e("onSaveInstanceState", "onSaveInstanceState++++++");

        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必須重寫
     */
    @Override
    protected void onDestroy() {
        Log.e("onDestroy", "onDestroy++++++");

        super.onDestroy();
        mapView.onDestroy();
        if (null != mlocationClient) {
            mlocationClient.onDestroy();
        }
    }

    /**
     * 定位成功後回撥函式
     */
    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        Log.e("onLocationChanged", "onLocationChanged++++++");

        if (mListener != null && amapLocation != null) {
            if (amapLocation != null
                    && amapLocation.getErrorCode() == 0) {
                mLocationErrText.setVisibility(View.GONE);
                mListener.onLocationChanged(amapLocation);// 顯示系統小藍點
                Log.e("current+address", amapLocation.getAddress());
                Log.e("current+address", amapLocation.getPoiName());
                //自定義小藍點外部圓的範圍
                aMap.moveCamera(CameraUpdateFactory.zoomTo(30));
                queryPoi(amapLocation);
//                aMap.moveCamera(CameraUpdateFactory.zoomTo(18));
                Log.e("經緯度", "經度" + amapLocation.getLatitude() + "緯度=====" + amapLocation.getLongitude());
            } else {
                String errText = "定位失敗," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo();
                Log.e("AmapErr", errText);
                mLocationErrText.setVisibility(View.VISIBLE);
                mLocationErrText.setText(errText);
            }
        }
    }

    /**
     * 查詢附近地址
     */
    private void queryPoi(AMapLocation amapLocation) {

        String keyword = "";
        query = new PoiSearch.Query(keyword, "商務住宅", amapLocation.getProvince());// 第一個引數表示搜尋字串,第二個引數表示poi搜尋型別,第三個引數表示poi搜尋區域(空字串代表全國)
        query.setPageSize(100);// 設定每頁最多返回多少條poiitem
        query.setPageNum(currentPage);// 設定查第一頁
        lp = new LatLonPoint(amapLocation.getLatitude(), amapLocation.getLongitude());
        if (lp != null) {
            poiSearch = new PoiSearch(this, query);
            poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
                @Override
                public void onPoiSearched(PoiResult poiResult, int i) {
                    if (i == AMapException.CODE_AMAP_SUCCESS) {
                        if (poiResult != null && poiResult.getQuery() != null) {// 搜尋poi的結果
                            if (poiResult.getQuery().equals(query)) {// 是否是同一條
                                list = poiResult.getPois();
                                if (list != null && list.size() > 0) {
                                    if (madapter == null) {
                                        madapter = new CustomLocationAdapter(CustomLocationActivity.this, list);
                                        lv_listaddress.setAdapter(madapter);
                                    }
                                    madapter.notifyDataSetChanged();
                                }
                            }
                        }
                    } else {
                        Log.e("error", "失敗");
                    }
                }

                @Override
                public void onPoiItemSearched(PoiItem poiItem, int i) {

                }
            });
            poiSearch.setBound(new PoiSearch.SearchBound(lp, 3000, true));//
            // 設定搜尋區域為以lp點為圓心,其周圍3000米範圍
            poiSearch.searchPOIAsyn();// 非同步搜尋
        }
    }

    /**
     * 啟用定位
     */
    @Override
    public void activate(OnLocationChangedListener listener) {
        Log.e("activate", "activate++++++");

        mListener = listener;
        if (mlocationClient == null) {
            mlocationClient = new AMapLocationClient(this);
            mLocationOption = new AMapLocationClientOption();
            //設定定位監聽
            mlocationClient.setLocationListener(this);

            //設定定位模式為高精度模式,Battery_Saving 為低功耗模式,Device_Sensors是僅裝置模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //設定是否犯規地址資訊)預設返回)
            mLocationOption.setNeedAddress(true);
            //設定是否只定位一次
            mLocationOption.setOnceLocation(true);
            if (mLocationOption.isOnceLocationLatest()) {
                //設定setOnceLocationLatest(boolean b)介面為true,啟動定位時SDK會返回最近3s內精度最高的一次定位結果。
                //如果設定其為true,setOnceLocation(boolean b)介面也會被設定為true,反之不會。
                mLocationOption.setOnceLocationLatest(true);
            }
            //設定是否強制重新整理wify,預設為強制
            mLocationOption.setWifiActiveScan(true);
            //設定是否允許模擬位置 ,預設為FALSE不允許
            mLocationOption.setMockEnable(false);
            //設定定位間隔 單位毫秒,預設為2000ms
            mLocationOption.setInterval(2000);
            //給定位客戶端設定引數
            mlocationClient.setLocationOption(mLocationOption);
            // 此方法為每隔固定時間會發起一次定位請求,為了減少電量消耗或網路流量消耗,
            // 注意設定合適的定位時間的間隔(最小間隔支援為2000ms),並且在合適時間呼叫stopLocation()方法來取消定位請求
            // 在定位結束後,在合適的生命週期呼叫onDestroy()方法
            // 在單次定位情況下,定位無論成功與否,都無需呼叫stopLocation()方法移除請求,定位sdk內部會移除
            mlocationClient.startLocation();
        }
    }

    /**
     * 停止定位
     */
    @Override
    public void deactivate() {
        Log.e("deactivate", "deactivate++++++");

        mListener = null;
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;
    }


}