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

Java tutorial

Introduction

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

Source

package com.tsroad.map.poisearch;

import java.util.ArrayList;
import java.util.List;
import android.app.ProgressDialog;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.InfoWindowAdapter;
import com.amap.api.maps.AMap.OnMarkerClickListener;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.SupportMapFragment;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.NaviPara;
import com.amap.api.maps.overlay.PoiOverlay;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.core.SuggestionCity;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.Inputtips.InputtipsListener;
import com.amap.api.services.help.Tip;
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.tsroad.map.R;
import com.tsroad.map.util.AMapUtil;
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 PoiKeywordSearchActivity extends FragmentActivity
        implements OnMarkerClickListener, InfoWindowAdapter, TextWatcher, OnPoiSearchListener, OnClickListener {
    private AMap aMap;
    private AutoCompleteTextView searchText;// ?
    private String keyWord = "";// ?poi?
    private ProgressDialog progDialog = null;// ??
    private EditText editCity;// ????
    private PoiResult poiResult; // poi
    private int currentPage = 0;// ??0
    private PoiSearch.Query query;// Poi?
    private PoiSearch poiSearch;// POI?

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

    /**
     * ?AMap
     */
    private void init() {
        if (aMap == null) {
            aMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            setUpMap();
        }
    }

    /**
     * ??
     */
    private void setUpMap() {
        Button searButton = (Button) findViewById(R.id.searchButton);
        searButton.setOnClickListener(this);
        Button nextButton = (Button) findViewById(R.id.nextButton);
        nextButton.setOnClickListener(this);
        searchText = (AutoCompleteTextView) findViewById(R.id.keyWord);
        searchText.addTextChangedListener(this);// ?
        editCity = (EditText) findViewById(R.id.city);
        aMap.setOnMarkerClickListener(this);// marker?
        aMap.setInfoWindowAdapter(this);// infowindow?
    }

    /**
     * ?
     */
    public void searchButton() {
        keyWord = AMapUtil.checkEditText(searchText);
        if ("".equals(keyWord)) {
            ToastUtil.show(PoiKeywordSearchActivity.this, "?");
            return;
        } else {
            doSearchQuery();
        }
    }

    /**
     * 
     */
    public void nextButton() {
        if (query != null && poiSearch != null && poiResult != null) {
            if (poiResult.getPageCount() - 1 > currentPage) {
                currentPage++;
                query.setPageNum(currentPage);// ?
                poiSearch.searchPOIAsyn();
            } else {
                ToastUtil.show(PoiKeywordSearchActivity.this, R.string.no_result);
            }
        }
    }

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

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

    /**
     * poi?
     */
    protected void doSearchQuery() {
        showProgressDialog();// 
        currentPage = 0;
        query = new PoiSearch.Query(keyWord, "", editCity.getText().toString());// ???poi??poi?
        query.setPageSize(10);// ??poiitem
        query.setPageNum(currentPage);// 

        poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        marker.showInfoWindow();
        return false;
    }

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

    @Override
    public View getInfoWindow(final Marker marker) {
        View view = getLayoutInflater().inflate(R.layout.poikeywordsearch_uri, null);
        TextView title = (TextView) view.findViewById(R.id.title);
        title.setText(marker.getTitle());

        TextView snippet = (TextView) view.findViewById(R.id.snippet);
        snippet.setText(marker.getSnippet());
        ImageButton button = (ImageButton) view.findViewById(R.id.start_amap_app);
        // app
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startAMapNavi(marker);
            }
        });
        return view;
    }

    /**
     * ??app?
     */
    public void startAMapNavi(Marker marker) {
        //?
        NaviPara naviPara = new NaviPara();
        //?
        naviPara.setTargetPoint(marker.getPosition());
        //??
        naviPara.setNaviStyle(NaviPara.DRIVING_AVOID_CONGESTION);
        try {
            //
            AMapUtils.openAMapNavi(naviPara, getApplicationContext());
        } catch (com.amap.api.maps.AMapException e) {
            //?
            AMapUtils.getLatestAMapApp(getApplicationContext());
        }

    }

    /**
     * ??app??
     */
    public String getApplicationName() {
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo = null;
        try {
            packageManager = getApplicationContext().getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInfo = null;
        }
        String applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
        return applicationName;
    }

    /**
     * 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(PoiKeywordSearchActivity.this, infomation);

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @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();
        Inputtips inputTips = new Inputtips(PoiKeywordSearchActivity.this, new InputtipsListener() {

            @Override
            public void onGetInputtips(List<Tip> tipList, int rCode) {
                if (rCode == 0) {// 
                    List<String> listString = new ArrayList<String>();
                    for (int i = 0; i < tipList.size(); i++) {
                        listString.add(tipList.get(i).getName());
                    }
                    ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(getApplicationContext(),
                            R.layout.route_inputs, listString);
                    searchText.setAdapter(aAdapter);
                    aAdapter.notifyDataSetChanged();
                }
            }
        });
        try {
            inputTips.requestInputtips(newText, editCity.getText().toString());// ??????

        } catch (AMapException e) {
            e.printStackTrace();
        }
    }

    /**
     * POI
     */
    @Override
    public void onPoiItemDetailSearched(PoiItemDetail arg0, int rCode) {

    }

    /**
     * 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
                    List<PoiItem> poiItems = poiResult.getPois();// ?poiitem?0
                    List<SuggestionCity> suggestionCities = poiResult.getSearchSuggestionCitys();// ??poiitem????

                    if (poiItems != null && poiItems.size() > 0) {
                        aMap.clear();// ??
                        PoiOverlay poiOverlay = new PoiOverlay(aMap, poiItems);
                        poiOverlay.removeFromMap();
                        poiOverlay.addToMap();
                        poiOverlay.zoomToSpan();
                    } else if (suggestionCities != null && suggestionCities.size() > 0) {
                        showSuggestCity(suggestionCities);
                    } else {
                        ToastUtil.show(PoiKeywordSearchActivity.this, R.string.no_result);
                    }
                }
            } else {
                ToastUtil.show(PoiKeywordSearchActivity.this, R.string.no_result);
            }
        } else if (rCode == 27) {
            ToastUtil.show(PoiKeywordSearchActivity.this, R.string.error_network);
        } else if (rCode == 32) {
            ToastUtil.show(PoiKeywordSearchActivity.this, R.string.error_key);
        } else {
            ToastUtil.show(PoiKeywordSearchActivity.this, getString(R.string.error_other) + rCode);
        }

    }

    /**
     * Button
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        /**
         * ?
         */
        case R.id.searchButton:
            searchButton();
            break;
        /**
         * 
         */
        case R.id.nextButton:
            nextButton();
            break;
        default:
            break;
        }
    }
}