com.tsroad.map.poisearch.PoiAroundSearchActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.tsroad.map.poisearch.PoiAroundSearchActivity.java

Source

package com.tsroad.map.poisearch;

import java.util.List;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.InfoWindowAdapter;
import com.amap.api.maps.AMap.OnInfoWindowClickListener;
import com.amap.api.maps.AMap.OnMapClickListener;
import com.amap.api.maps.AMap.OnMarkerClickListener;
import com.amap.api.maps.SupportMapFragment;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.overlay.PoiOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.core.SuggestionCity;
import com.amap.api.services.poisearch.Cinema;
import com.amap.api.services.poisearch.Dining;
import com.amap.api.services.poisearch.Hotel;
import com.amap.api.services.poisearch.PoiItemDetail;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener;
import com.amap.api.services.poisearch.PoiSearch.SearchBound;
import com.amap.api.services.poisearch.Scenic;
import com.tsroad.map.R;
import com.tsroad.map.util.ToastUtil;

/**
% @authors Keung Charteris & T.s.road CZQ
% @version 1.0 ($Revision$)
% @addr. GUET, Gui Lin, 540001,  P.R.China
% @contact : cztsiang@gmail.com
% @date Copyright(c)  2016-2020,  All rights reserved.
% This is an open access code distributed under the Creative Commons Attribution License, which permits 
% unrestricted use, distribution, and reproduction in any medium, provided the original work is properly cited. 
*/

/**
 * AMapV2??poisearch?
 */
public class PoiAroundSearchActivity extends FragmentActivity
        implements OnMarkerClickListener, InfoWindowAdapter, OnItemSelectedListener, OnPoiSearchListener,
        OnMapClickListener, OnInfoWindowClickListener, OnClickListener {
    private AMap aMap;
    private ProgressDialog progDialog = null;// ??
    private Spinner selectDeep;// 
    private String[] itemDeep = { "", "?", "", "" };
    private Spinner selectType;// ?
    private String[] itemTypes = { "poi", "", "", "" };
    private String deepType = "";// poi?
    private int searchType = 0;// ?
    private int tsearchType = 0;// ??
    private PoiResult poiResult; // poi
    private int currentPage = 0;// ??0
    private PoiSearch.Query query;// Poi?
    private LatLonPoint lp = new LatLonPoint(39.908127, 116.375257);// ?
    private Marker locationMarker; // 
    private PoiSearch poiSearch;
    private PoiOverlay poiOverlay;// poi
    private List<PoiItem> poiItems;// poi?
    private Marker detailMarker;// Marker
    private Button nextButton;// 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.poiaroundsearch_activity);
        /*
         * ?;
         * ?, 
         * ??
         * */
        //Demo????
        //        MapsInitializer.sdcardDir =OffLineMapUtils.getSdCacheDir(this);
        init();
    }

    /**
     * ?AMap
     */
    private void init() {
        if (aMap == null) {
            aMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            setUpMap();
            setSelectType();
            Button locationButton = (Button) findViewById(R.id.locationButton);
            locationButton.setOnClickListener(this);
            Button searchButton = (Button) findViewById(R.id.searchButton);
            searchButton.setOnClickListener(this);
            nextButton = (Button) findViewById(R.id.nextButton);
            nextButton.setOnClickListener(this);
            nextButton.setClickable(false);// ??
            locationMarker = aMap.addMarker(
                    new MarkerOptions().anchor(0.5f, 1).icon(BitmapDescriptorFactory.fromResource(R.drawable.point))
                            .position(new LatLng(lp.getLatitude(), lp.getLongitude()))
                            .title("?"));
            locationMarker.showInfoWindow();

        }
    }

    /**
     * 
     */
    private void setUpMap() {
        selectDeep = (Spinner) findViewById(R.id.spinnerdeep);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
                itemDeep);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        selectDeep.setAdapter(adapter);
        selectDeep.setOnItemSelectedListener(this);// spinner?
        aMap.setOnMarkerClickListener(this);// marker?
        aMap.setInfoWindowAdapter(this);// infowindow?

    }

    /**
     * 
     */
    private void setSelectType() {
        selectType = (Spinner) findViewById(R.id.searchType);// ?
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
                itemTypes);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        selectType.setAdapter(adapter);
        selectType.setOnItemSelectedListener(this);// spinner?
        aMap.setOnMarkerClickListener(this);// marker?
        aMap.setInfoWindowAdapter(this);// infowindow?
    }

    /**
     * ?
     */
    private void registerListener() {
        aMap.setOnMapClickListener(PoiAroundSearchActivity.this);
        aMap.setOnMarkerClickListener(PoiAroundSearchActivity.this);
        aMap.setOnInfoWindowClickListener(this);
        aMap.setInfoWindowAdapter(PoiAroundSearchActivity.this);
    }

    /**
     * 
     */
    private void showProgressDialog() {
        if (progDialog == null)
            progDialog = new ProgressDialog(this);
        progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progDialog.setIndeterminate(false);
        progDialog.setCancelable(false);
        progDialog.setMessage("?");
        progDialog.show();
    }

    /**
     * ??
     */
    private void dissmissProgressDialog() {
        if (progDialog != null) {
            progDialog.dismiss();
        }
    }

    /**
     * poi?
     */
    protected void doSearchQuery() {
        showProgressDialog();// 
        aMap.setOnMapClickListener(null);// poi?
        currentPage = 0;
        query = new PoiSearch.Query("", deepType, "");// ???poi??poi?
        query.setPageSize(10);// ??poiitem
        query.setPageNum(currentPage);// 

        searchType = tsearchType;

        switch (searchType) {
        case 0: {// poi
            query.setLimitDiscount(false);
            query.setLimitGroupbuy(false);
        }
            break;
        case 1: {// 
            query.setLimitGroupbuy(true);
            query.setLimitDiscount(false);
        }
            break;
        case 2: {// 
            query.setLimitGroupbuy(false);
            query.setLimitDiscount(true);
        }
            break;
        case 3: {// 
            query.setLimitGroupbuy(true);
            query.setLimitDiscount(true);
        }
            break;
        }

        if (lp != null) {
            poiSearch = new PoiSearch(this, query);
            poiSearch.setOnPoiSearchListener(this);
            poiSearch.setBound(new SearchBound(lp, 2000, true));//
            // ?lp2000
            /*
             * List<LatLonPoint> list = new ArrayList<LatLonPoint>();
             * list.add(lp);
             * list.add(AMapUtil.convertToLatLonPoint(Constants.BEIJING));
             * poiSearch.setBound(new SearchBound(list));// poi?
             */
            poiSearch.searchPOIAsyn();// ?
        }
    }

    /**
     * poi?
     */
    public void nextSearch() {
        if (query != null && poiSearch != null && poiResult != null) {
            if (poiResult.getPageCount() - 1 > currentPage) {
                currentPage++;

                query.setPageNum(currentPage);// ?
                poiSearch.searchPOIAsyn();
            } else {
                ToastUtil.show(PoiAroundSearchActivity.this, R.string.no_result);
            }
        }
    }

    /**
     * ?poi
     * 
     * @param poiId
     */
    public void doSearchPoiDetail(String poiId) {
        if (poiSearch != null && poiId != null) {
            poiSearch.searchPOIDetailAsyn(poiId);
        }
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        if (poiOverlay != null && poiItems != null && poiItems.size() > 0) {
            detailMarker = marker;
            doSearchPoiDetail(poiItems.get(poiOverlay.getPoiIndex(marker)).getPoiId());
        }
        return false;
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    /**
     * poi?????
     */
    private void showSuggestCity(List<SuggestionCity> cities) {
        String infomation = "??\n";
        for (int i = 0; i < cities.size(); i++) {
            infomation += "??:" + cities.get(i).getCityName() + "?:"
                    + cities.get(i).getCityCode() + "?:" + cities.get(i).getAdCode() + "\n";
        }
        ToastUtil.show(PoiAroundSearchActivity.this, infomation);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (parent == selectDeep) {
            deepType = itemDeep[position];

        } else if (parent == selectType) {
            tsearchType = position;
        }
        nextButton.setClickable(false);// ?????
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        if (parent == selectDeep) {
            deepType = "";
        } else if (parent == selectType) {
            tsearchType = 0;
        }
        nextButton.setClickable(false);// ?????
    }

    /**
     * POI
     */
    @Override
    public void onPoiItemDetailSearched(PoiItemDetail result, int rCode) {
        dissmissProgressDialog();// ???
        if (rCode == 0) {
            if (result != null) {// ?poi
                if (detailMarker != null) {
                    StringBuffer sb = new StringBuffer(result.getSnippet());
                    if ((result.getGroupbuys() != null && result.getGroupbuys().size() > 0)
                            || (result.getDiscounts() != null && result.getDiscounts().size() > 0)) {

                        if (result.getGroupbuys() != null && result.getGroupbuys().size() > 0) {// ???
                            sb.append("\n" + result.getGroupbuys().get(0).getDetail());
                        }
                        if (result.getDiscounts() != null && result.getDiscounts().size() > 0) {// ???
                            sb.append("\n" + result.getDiscounts().get(0).getDetail());
                        }
                    } else {

                        sb = new StringBuffer("?" + result.getSnippet() + "\n?" + result.getTel()
                                + "\n" + result.getTypeDes());
                    }
                    // poi???
                    if (result.getDeepType() != null) {
                        //???????
                        //sb = getDeepInfo(result, sb);
                        detailMarker.setSnippet(sb.toString());
                    } else {
                        ToastUtil.show(PoiAroundSearchActivity.this, "Poi?");
                    }
                }

            } else {
                ToastUtil.show(PoiAroundSearchActivity.this, R.string.no_result);
            }
        } else if (rCode == 27) {
            ToastUtil.show(PoiAroundSearchActivity.this, R.string.error_network);
        } else if (rCode == 32) {
            ToastUtil.show(PoiAroundSearchActivity.this, R.string.error_key);
        } else {
            ToastUtil.show(PoiAroundSearchActivity.this, getString(R.string.error_other) + rCode);
        }
    }

    /**
     * POI??
     */
    private StringBuffer getDeepInfo(PoiItemDetail result, StringBuffer sbuBuffer) {
        switch (result.getDeepType()) {
        // ??
        case DINING:
            if (result.getDining() != null) {
                Dining dining = result.getDining();
                sbuBuffer.append("\n?" + dining.getTag() + "\n" + dining.getRecommend()
                        + "\n??" + dining.getDeepsrc());
            }
            break;
        // ?
        case HOTEL:
            if (result.getHotel() != null) {
                Hotel hotel = result.getHotel();
                sbuBuffer.append("\n?" + hotel.getLowestPrice() + "\n?" + hotel.getHealthRating()
                        + "\n??" + hotel.getDeepsrc());
            }
            break;
        // ?
        case SCENIC:
            if (result.getScenic() != null) {
                Scenic scenic = result.getScenic();
                sbuBuffer.append("\n" + scenic.getPrice() + "\n??" + scenic.getRecommend()
                        + "\n??" + scenic.getDeepsrc());
            }
            break;
        // ?
        case CINEMA:
            if (result.getCinema() != null) {
                Cinema cinema = result.getCinema();
                sbuBuffer.append("\n?" + cinema.getParking() + "\n" + cinema.getIntro()
                        + "\n??" + cinema.getDeepsrc());
            }
            break;
        default:
            break;
        }
        return sbuBuffer;
    }

    /**
     * POI?
     */
    @Override
    public void onPoiSearched(PoiResult result, int rCode) {
        dissmissProgressDialog();// ???
        if (rCode == 0) {
            if (result != null && result.getQuery() != null) {// ?poi
                if (result.getQuery().equals(query)) {// ???
                    poiResult = result;
                    poiItems = poiResult.getPois();// ?poiitem?0
                    List<SuggestionCity> suggestionCities = poiResult.getSearchSuggestionCitys();// ??poiitem????
                    if (poiItems != null && poiItems.size() > 0) {
                        aMap.clear();// ??
                        poiOverlay = new PoiOverlay(aMap, poiItems);
                        poiOverlay.removeFromMap();
                        poiOverlay.addToMap();
                        poiOverlay.zoomToSpan();

                        nextButton.setClickable(true);// ?
                    } else if (suggestionCities != null && suggestionCities.size() > 0) {
                        showSuggestCity(suggestionCities);
                    } else {
                        ToastUtil.show(PoiAroundSearchActivity.this, R.string.no_result);
                    }
                }
            } else {
                ToastUtil.show(PoiAroundSearchActivity.this, R.string.no_result);
            }
        } else if (rCode == 27) {
            ToastUtil.show(PoiAroundSearchActivity.this, R.string.error_network);
        } else if (rCode == 32) {
            ToastUtil.show(PoiAroundSearchActivity.this, R.string.error_key);
        } else {
            ToastUtil.show(PoiAroundSearchActivity.this, getString(R.string.error_other) + rCode);
        }
    }

    @Override
    public void onMapClick(LatLng latng) {
        locationMarker = aMap.addMarker(
                new MarkerOptions().anchor(0.5f, 1).icon(BitmapDescriptorFactory.fromResource(R.drawable.point))
                        .position(latng).title(""));
        locationMarker.showInfoWindow();
    }

    @Override
    public void onInfoWindowClick(Marker marker) {
        locationMarker.hideInfoWindow();
        lp = new LatLonPoint(locationMarker.getPosition().latitude, locationMarker.getPosition().longitude);
        locationMarker.destroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        /**
         * 
         */
        case R.id.locationButton:
            aMap.clear();// ?marker
            registerListener();
            break;
        /**
         * ?
         */
        case R.id.searchButton:
            doSearchQuery();
            break;
        /**
         * 
         */
        case R.id.nextButton:
            nextSearch();
            break;
        default:
            break;
        }
    }
}