高德地图--获取poi数据

来源:互联网 发布:软件代理加盟 编辑:程序博客网 时间:2024/04/29 05:40

根据输入的关键字进行Poi搜索,点击搜索到的结果列表中的一项,返回显示和定位页,定位并显示该地址的详细信息;


/** 售后地图* Created by wangchm on 2017/4/25 0031.* */public class SaleMapActivity extends AppCompatActivity implements GeocodeSearch.OnGeocodeSearchListener {    private static final String TAG = "SaleMapActivity";    @BindView(R.id.iv_mapBack)    ImageView ivMapBack;    @BindView(R.id.afterSaleMap)    MapView afterSaleMap;    @BindView(R.id.iv_saleSear)    ImageView ivSaleSear;    @BindView(R.id.rl_sale_search)    RelativeLayout rlSaleSearch;    @BindView(R.id.tv_pointName)    TextView tvPointName;    @BindView(R.id.tv_distance)    TextView tvDistance;    @BindView(R.id.iv_location)    ImageView ivLocation;    @BindView(R.id.tv_location)    TextView tvLocation;    @BindView(R.id.ll_location)    LinearLayout llLocation;    @BindView(R.id.iv_phone)    ImageView ivPhone;    @BindView(R.id.tv_phone)    TextView tvPhone;    @BindView(R.id.ll_phone)    LinearLayout llPhone;    @BindView(R.id.map_bottom)    LinearLayout mapBottom;    AMap aMap;    //声明mLocationOption对象    public AMapLocationClientOption mLocationOption = null;    //声明AMapLocationClient类对象    public AMapLocationClient mLocationClient = null;    private Marker geoMarker;    private Marker regeoMarker;    private Double lat;    private Double lon;    private String mCity = "";    private String areaInfo="";    private String adCode = "";    Context mContext;    private GeocodeSearch geocoderSearch;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_sale_map);        ButterKnife.bind(this);        afterSaleMap.onCreate(savedInstanceState);        mContext = SaleMapActivity.this;        //初始化定位        mLocationClient = new AMapLocationClient(getApplicationContext());        //设置定位回调监听        mLocationClient.setLocationListener(mLocationListener);        initAmap();    }    /**     * * 初始化AMap对象     */    private void initAmap() {        if (aMap == null) {            aMap = afterSaleMap.getMap();        }        setUpMap();    }    private void setUpMap() {        //初始化定位参数        mLocationOption = new AMapLocationClientOption();        //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);        //设置是否返回地址信息(默认返回地址信息)        mLocationOption.setNeedAddress(true);        //设置是否只定位一次,默认为false        mLocationOption.setOnceLocation(true);        //设置是否强制刷新WIFI,默认为强制刷新        mLocationOption.setWifiActiveScan(true);        //设置是否允许模拟位置,默认为false,不允许模拟位置        mLocationOption.setMockEnable(false);        //设置定位间隔,单位毫秒,默认为2000ms//        mLocationOption.setInterval(2000);        //给定位客户端对象设置定位参数        mLocationClient.setLocationOption(mLocationOption);        //启动定位        mLocationClient.startLocation();        //重新定位时移除红气泡,再重新初始化        geoMarker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)                .icon(BitmapDescriptorFactory                        .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));//        geoMarker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)//                .icon(BitmapDescriptorFactory.fromResource(R.drawable.cur_position)));//        regeoMarker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)//                .icon(BitmapDescriptorFactory//                        .defaultMarker(BitmapDescriptorFactory.HUE_RED)));        regeoMarker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)                .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_map_search)));    }    //声明定位回调监听器    public AMapLocationListener mLocationListener = new AMapLocationListener() {        @Override        public void onLocationChanged(AMapLocation amapLocation) {            if (amapLocation != null) {                if (amapLocation.getErrorCode() == 0) {                    //定位成功回调信息,设置相关消息                    amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表                    amapLocation.getLatitude();//获取纬度                    amapLocation.getLongitude();//获取经度                    amapLocation.getAccuracy();//获取精度信息                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");                    Date date = new Date(amapLocation.getTime());                    df.format(date);//定位时间                    amapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。                    amapLocation.getCountry();//国家信息                    amapLocation.getProvince();//省信息                    mCity = amapLocation.getCity();//城市信息                    amapLocation.getDistrict();//城区信息                    amapLocation.getStreet();//街道信息                    amapLocation.getStreetNum();//街道门牌号信息                    amapLocation.getCityCode();//城市编码                    adCode = amapLocation.getAdCode();//地区编码                    amapLocation.getAoiName();//获取当前定位点的AOI信息                    lat = amapLocation.getLatitude();                    lon = amapLocation.getLongitude();                    areaInfo = amapLocation.getProvince()+" "+amapLocation.getCity() +" "+amapLocation.getDistrict();                    Log.v("pcw", "lat : " + lat + " lon : " + lon+" adCode: "+adCode+","+areaInfo);                    Log.v("pcw", "amapLocation="+amapLocation.toString());//                    etEAddr.setText(amapLocation.getAoiName());                    tvPointName.setText(amapLocation.getAoiName());                    tvLocation.setText(amapLocation.getAddress());                    // 设置当前地图显示为当前位置                    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 16));                    MarkerOptions markerOptions = new MarkerOptions();                    markerOptions.position(new LatLng(lat, lon));                    markerOptions.title("当前位置");                    markerOptions.visible(true);//                    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.location_marker));//                    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.cur_position);                    //设置当前位置的marker图标                    ImageView imageView = new ImageView(SaleMapActivity.this);                    imageView.setImageResource(R.drawable.cur_position);                    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromView(imageView);                    markerOptions.icon(bitmapDescriptor);                    aMap.addMarker(markerOptions);//                    mLocationListener.onLocationChanged(amapLocation);                } else {                    //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。                    Log.e("AmapError", "location Error, ErrCode:"                            + amapLocation.getErrorCode() + ", errInfo:"                            + amapLocation.getErrorInfo());                    Toast.makeText(mContext,amapLocation.getErrorInfo(),Toast.LENGTH_LONG).show();                }            }        }    };    @Override    protected void onActivityResult(int requestCode,int resultCode,Intent data){        LogUtil.d("SaleMapActivity","sdjflksdfjdlskf");        if(requestCode == 1){            if(resultCode == RESULT_FIRST_USER){                LogUtil.d("SaleMapActivity","onActivityResult======RESULT_FIRST_USER=====");                Bundle bundle1 = data.getBundleExtra("poibundle");                PoiItem item = (PoiItem) bundle1.get("poi");                LatLng latLng= convertToLatLng(item.getLatLonPoint());                aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,15));                regeoMarker.setPosition(latLng);                tvPointName.setText(item.getTitle());                String addr = item.getProvinceName()+item.getCityName()+item.getAdName()+item.getSnippet();                tvLocation.setText(addr);                tvPhone.setText(item.getTel());                LatLng curLatLng = new LatLng(lat,lon);                float distance = AMapUtils.calculateLineDistance(curLatLng,latLng);                DecimalFormat format = new DecimalFormat("0.00");                tvDistance.setText("距离"+format.format(distance/1000)+"公里");            }        }    }    @OnClick({R.id.iv_mapBack, R.id.rl_sale_search, R.id.ll_phone})    public void onClick(View view) {        switch (view.getId()) {            case R.id.iv_mapBack:                finish();                break;            case R.id.rl_sale_search:                Intent searchIntent = new Intent(SaleMapActivity.this, SearchSaleActivity.class);                startActivityForResult(searchIntent,1);//                startActivity(searchIntent);                break;            case R.id.ll_phone:                break;        }    }    public LatLng convertToLatLng(LatLonPoint point){        return new LatLng(point.getLatitude(),point.getLongitude());    }}


/** 售后网点搜索* Created by wangchm on 2017/4/25 0031.* */public class SearchSaleActivity extends AppCompatActivity {    private static final String TAG = "SearchSaleActivity";    @BindView(R.id.salesearch_edit)    TextField salesearchEdit;    @BindView(R.id.tv_saleCancel)    TextView tvSaleCancel;    @BindView(R.id.lvResult)    ListView lvResult;    GeocodeSearch geocodeSearch;    Context mContext;    SearchMapAdapter adapter;    List<GeocodeAddress> addrList;    PoiSearch poiSearch;    SearchPoiMapAdapter poiAdapter;    ArrayList<PoiItem> poiItems;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_search_sale);        ButterKnife.bind(this);        mContext = SearchSaleActivity.this;        addrList = new ArrayList<GeocodeAddress>();        salesearchEdit.addTextChangedListener(new MyTextChangeListener());        lvResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                Bundle bundle1 = new Bundle();                bundle1.putParcelable("poi",poiItems.get(position));                Intent backIntent = new Intent(mContext,SaleMapActivity.class);                backIntent.putExtra("poibundle",bundle1);                setResult(RESULT_FIRST_USER,backIntent);                finish();            }        });    }    int currentPage = 1;    //文本框的改变监听    class MyTextChangeListener implements TextWatcher{        @Override        public void beforeTextChanged(CharSequence s, int start, int count, int after) {        }        @Override        public void onTextChanged(CharSequence s, int start, int before, int count) {            String newText = s.toString().trim();            //获取Poi数据            //构造 PoiSearch.Query 对象,通过 PoiSearch.Query(String query, String ctgr, String city) 设置搜索条件。            //keyWord表示搜索字符串,第二个参数表示POI搜索类型,二者选填其一,            //cityCode表示POI搜索区域,可以是城市编码也可以是城市名称,也可以传空字符串,空字符串代表全国在全国范围内进行搜索            PoiSearch.Query query = new PoiSearch.Query(newText,"","0532");            query.setPageSize(10);            query.setPageNum(currentPage);            //构造 PoiSearch 对象,并设置监听。            poiSearch = new PoiSearch(mContext,query);            poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {                @Override                public void onPoiSearched(PoiResult poiResult, int i) {                    //通过回调接口 onPoiSearched 解析返回的结果,将查询到的 POI 以绘制点的方式显示在地图上。                    if(i == 1000){                        if(poiResult != null && poiResult.getPois() != null && poiResult.getPois().size() != 0){                            poiItems = poiResult.getPois();                            poiAdapter = new SearchPoiMapAdapter(mContext,poiItems);                            lvResult.setAdapter(poiAdapter);                        }                    }                }                @Override                public void onPoiItemSearched(PoiItem poiItem, int i) {                }            });            //调用 PoiSearch 的 searchPOIAsyn() 方法发送请求。            poiSearch.searchPOIAsyn();        }        @Override        public void afterTextChanged(Editable s) {        }    }    @OnClick(R.id.tv_saleCancel)    public void onClick() {    }}

0 0
原创粉丝点击