高德地图导航依赖

2019-08-10  本文已影响0人  禄子_c79b

获取密钥在build.gradle下点右边


2018022814390440.png

不用依赖3D地图
compile 'com.amap.api:navi-3dmap:latest.integration'因为这里面已经包括了3dmap了

import android.Manifest;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;

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.core.SuggestionCity;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

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

public class SearchActivity extends AppCompatActivity implements PoiSearch.OnPoiSearchListener{
private EditText mEt_keyword;
private RecyclerView listView;

private static final int REQUEST_PERMISSION_LOCATION = 0;
private String keyWord = "";// 要输入的poi搜索关键字
private PoiResult poiResult; // poi返回的结果
private int currentPage = 0;// 当前页面,从0开始计数
private PoiSearch.Query query;// Poi查询条件类
private PoiSearch       poiSearch;// POI搜索

private List<LocationAddressInfo> mData = new ArrayList<>();
private LocationListAdapter listAdapter;


@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    mEt_keyword = (EditText) findViewById(R.id.et_keyword);

    listView = (RecyclerView) findViewById(R.id.irc_listview);
    listView.setLayoutManager(new LinearLayoutManager(this));

    //设置定位权限
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_PERMISSION_LOCATION);

    } else {
        //监听EditText输入
        initListener();
    }

}


private void initListener() {
    mEt_keyword.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            keyWord = String.valueOf(charSequence);
            if ("".equals(keyWord)) {
                Toast.makeText(SearchActivity.this,"请输入搜索关键字",Toast.LENGTH_SHORT).show();
                return;
            } else {
                doSearchQuery(keyWord);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
}

/**
 * 开始进行poi搜索
 */
protected void doSearchQuery(String key) {
    currentPage = 0;
    //不输入城市名称有些地方搜索不到
    // 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
    query = new PoiSearch.Query(key, "", "");
    // 设置每页最多返回多少条poiitem
    query.setPageSize(10);
    // 设置查询页码
    query.setPageNum(currentPage);

    //构造 PoiSearch 对象,并设置监听
    poiSearch = new PoiSearch(this, query);
    poiSearch.setOnPoiSearchListener(this);
    //调用 PoiSearch 的 searchPOIAsyn() 方法发送请求。
    poiSearch.searchPOIAsyn();
}

/**
 * POI信息查询回调方法
 */
@Override
public void onPoiSearched(PoiResult result, int rCode) {
    //rCode 为1000 时成功,其他为失败
    if (rCode == AMapException.CODE_AMAP_SUCCESS) {
        // 解析result   获取搜索poi的结果
        if (result != null && result.getQuery() != null) {
            if (result.getQuery().equals(query)) {  // 是否是同一条
                poiResult = result;
                ArrayList<LocationAddressInfo> data = new ArrayList<LocationAddressInfo>();//自己创建的数据集合
                // 取得第一页的poiitem数据,页数从数字0开始
                //poiResult.getPois()可以获取到PoiItem列表
                List<PoiItem> poiItems = poiResult.getPois();

                //若当前城市查询不到所需POI信息,可以通过result.getSearchSuggestionCitys()获取当前Poi搜索的建议城市
                List<SuggestionCity> suggestionCities = poiResult.getSearchSuggestionCitys();
                //如果搜索关键字明显为误输入,则可通过result.getSearchSuggestionKeywords()方法得到搜索关键词建议。
                List<String> suggestionKeywords =  poiResult.getSearchSuggestionKeywords();

                //解析获取到的PoiItem列表
                for(PoiItem item : poiItems){
                    //获取经纬度对象
                    LatLonPoint llp = item.getLatLonPoint();
                    double lon = llp.getLongitude();
                    double lat = llp.getLatitude();
                    //返回POI的名称
                    String title = item.getTitle();
                    //返回POI的地址
                    String text = item.getSnippet();
                    data.add(new LocationAddressInfo(String.valueOf(lon), String.valueOf(lat), title, text));
                }
                listAdapter = new LocationListAdapter(this, data);
                listView.setAdapter(listAdapter);
            }
        } else {
            Toast.makeText(SearchActivity.this,"无搜索结果",Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(SearchActivity.this,"错误码"+rCode,Toast.LENGTH_SHORT).show();
    }

}
/**
 * POI信息查询回调方法
 */
@Override
public void onPoiItemSearched(PoiItem item, int rCode) {

}

}

版权声明:本文为CSDN博主「shan_zwq」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/shanshan_1117/article/details/82791014

导航,路径规划:https://github.com/OrangeHao/AmapLike

上一篇下一篇

猜你喜欢

热点阅读