高德地图——搜索

Posted 码农000

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高德地图——搜索相关的知识,希望对你有一定的参考价值。

这里使用的高德搜索SDK为: AMap_Search_V3.30_20160616.jar
地图SDK提供了一系列的接口来方便用户获取高德LBS数据,目前主要支持的检索有:
• 兴趣点搜索:
1. 关键字搜索
2. 周边搜索
3. 指定区域搜索(与关键字搜索相同,这里不再介绍)
4. 团购/优惠搜索(这里不做介绍)
5. POI深度信息(这里不做介绍)
• 输入提示搜索
• 路径规划
• 公交线路查询
• 坐标/地址查询

注意:
这里仅仅进行简单的介绍,简单的记录一下学习笔记!
希望对大家能够起到一个抛砖引玉的作用

关键字搜索

  1. 搜索条件设置
    您需要通过 PoiSearch.Query(String query, String ctgr, String city) 设置搜索条件
    参数“ query” 为搜索的关键字,“ ctgr” 为搜索类型(类型参照表从相关下载处获取),“ city”为搜索城市,
    关键字、类型至少输入一个,搜索城市必须输入,
    通过 Query.setPageSize(int) 设置查询每页的结果数目;
    通过 Query.setPageNum(int) 设置查询第几页
  2. 发送请求和接收数据
    使用 PoiSearch.searchPOIAsyn() 搜索 POI,
    使用 PoiSearch.setOnPoiSearchListener()方法设置监听器,
    在 PoiSearch.OnPoiSearchListener 接口回调方法
    onPoiSearched(PoiResult poiResult,int rCode)中处理返回结果,当指定搜索城市时,若没有返回 POI 结果,则会返回包含关键字的建议城市名称

int currentPage = 0;
// 第一个参数表示搜索字符串,第二个参数表示POI搜索类型,二选其一
// 第三个参数表示POI搜索区域的编码,必设
        query = new PoiSearch.Query("kfc", "餐饮", "北京");
// 设置每页最多返回多少条poiitem
        query.setPageSize(10);
// 设置查第一页
        query.setPageNum(currentPage);
        PoiSearch poiSearch = new PoiSearch(this, query);
//设置搜索完成后的回调
poiSearch.setOnPoiSearchListener(this);
//进行异步查询
poiSearch.searchPOIAsyn();

    @Override
    public void onPoiItemSearched(PoiItem arg0, int arg1) {

    }

    public void onPoiSearched(PoiResult result, int rCode) {
            if (result != null && result.getQuery() != null) {
                if (result.getQuery().equals(query)) {
                    poiResult = result;
                    List<PoiItem> poiItems = poiResult.getPois();
                    if (poiItems != null && poiItems.size() > 0) {
    // 清理之前的图标,将查询结果显示在地图上
                        aMap.clear();
                        PoiOverlay poiOverlay = new PoiOverlay(aMap, poiItems);
                        poiOverlay.removeFromMap();
                        poiOverlay.addToMap();
                        poiOverlay.zoomToSpan();
                    }else{
                        // 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
                        List<SuggestionCity> suggestionCities = poiResult
                                .getSearchSuggestionCitys();
                    }
                }
            }

完整代码如下:

package com.hang.helloamap;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.overlay.PoiOverlay;
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 com.amap.api.services.poisearch.PoiSearch.Query;

public class BasePoiSearchActivity extends Activity implements
        PoiSearch.OnPoiSearchListener {
    MapView mapView;
    AMap aMap;
    Query query = null;
    PoiResult poiResult = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            mapView=new MapView(this);
            setContentView(mapView);
        mapView.onCreate(savedInstanceState);

        aMap = mapView.getMap();

        int currentPage = 0;
        // 第一个参数表示搜索字符串,第二个参数表示POI搜索类型,二选其一
        // 第三个参数表示POI搜索区域的编码,必设
        query = new PoiSearch.Query("kfc", "餐饮", "北京");
        query.setPageSize(10);// 设置每页最多返回多少条poiitem
        query.setPageNum(currentPage);// 设置查第一页
        PoiSearch poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();

    }

    @Override
    public void onPoiItemSearched(PoiItem arg0, int arg1) {

    }

    public void onPoiSearched(PoiResult result, int rCode) {
            if (result != null && result.getQuery() != null) {
                if (result.getQuery().equals(query)) {
                    poiResult = result;
                    List<PoiItem> poiItems = poiResult.getPois();
                    if (poiItems != null && poiItems.size() > 0) {
                        aMap.clear();// 清理之前的图标
                        PoiOverlay poiOverlay = new PoiOverlay(aMap, poiItems);
                        poiOverlay.removeFromMap();
                        poiOverlay.addToMap();
                        poiOverlay.zoomToSpan();
                    }else{
                        // 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
                        List<SuggestionCity> suggestionCities = poiResult
                                .getSearchSuggestionCitys();
                    }
                }
            }
    }

    @Override
    protected void onResume() {

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

    @Override
    protected void onPause() {

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

    @Override
    protected void onDestroy() {

        super.onDestroy();
        mapView.onDestroy();
    }
}

这里写图片描述

周边搜索

在关键字基础上通过PoiSearch.SearchBound(LatLonPoint center, int
radiusInMeters) 设置周边圆形搜索范围,参数 “ center” 为搜索的中心点坐标,“ radiusInMeters” 为周边搜索的范围

为了更好的演示效果,这里多添加一点东西

package com.hang.helloamap;

import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CircleOptions;
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.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.poisearch.PoiSearch.Query;
import com.amap.api.services.poisearch.PoiSearch.SearchBound;
import com.pansoft.oilgas.gaodenavigation.R;

public class PoiBoundSearchActivity extends Activity implements
        PoiSearch.OnPoiSearchListener {
    MapView mapView;
    AMap aMap;

    Query query = null;
    PoiResult poiResult = null;
    Marker locationMarker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapView = new MapView(this);
        setContentView(mapView);

        mapView.onCreate(savedInstanceState);

        aMap = mapView.getMap();

        int currentPage = 0;
// 第一个参数表示搜索字符串,第二个参数表示POI搜索类型二选其一
// 第三个参数表示POI搜索区域的编码,必设
        query = new PoiSearch.Query("", "餐厅", "北京");

        query.setPageSize(10);// 设置每页最多返回多少条poiitem
        query.setPageNum(currentPage);// 设置查第一页
        PoiSearch poiSearch = new PoiSearch(this, query);

        locationMarker = aMap.addMarker(new MarkerOptions()
                .anchor(0.5f, 1)
                .icon(BitmapDescriptorFactory
                    .fromResource(R.drawable.ic_launcher))
                .position(new LatLng(39.908127, 116.375257))
                .title("西单为中心点,查其周边"));
/*定义marker 图标的锚点(anchor),锚点是marker 图标接触地图平面的点,图标的左顶点为(0,0)点,右底点为(1,1)点,默认为(0.5,1.0)展示一下搜索中心位置,以及一些文本信息*/
        locationMarker.showInfoWindow();
//在要搜索的范围中添加一个白色背景&&黑色边线,凸显搜索范围
        {
            CircleOptions circleOptions = new CircleOptions();
            circleOptions.center(locationMarker.getPosition());
            circleOptions.radius(200);
            circleOptions.fillColor(Color.WHITE);
            circleOptions.strokeColor(Color.BLACK);
            circleOptions.strokeWidth(10);
            aMap.addCircle(circleOptions);
        }
//设置搜索的范围
        poiSearch.setBound(new SearchBound(
                new LatLonPoint(locationMarker.getPosition().latitude,
                        locationMarker.getPosition().longitude), 200));

        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();

    }

    @Override
    public void onPoiItemSearched(PoiItem arg0, int arg1) {

    }

    public void onPoiSearched(PoiResult result, int rCode) {
        // 搜索POI的结果
        if (result != null && result.getQuery() != null) {
            // 是否是同一条
            if (result.getQuery().equals(query)) {
                poiResult = result;
                // 取得搜索到的poiitems有多少页
                int resultPages = poiResult.getPageCount();
                // 取得第一页的poiitem数据,页数从数字0开始
                List<PoiItem> poiItems = poiResult.getPois();
                if (poiItems != null && poiItems.size() > 0) {
                    // aMap.clear();// 清理之前的图标
                    PoiOverlay poiOverlay = new PoiOverlay(aMap, poiItems);
                    poiOverlay.removeFromMap();
                    poiOverlay.addToMap();
                    poiOverlay.zoomToSpan();
                } else {
                    // 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
                    List<SuggestionCity> suggestionCities = poiResult
                            .getSearchSuggestionCitys();
                }
            }
        }
    }

    @Override
    protected void onResume() {

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

    @Override
    protected void onPause() {

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

    @Override
    protected void onDestroy() {

        super.onDestroy();
        mapView.onDestroy();
    }
}

这里写图片描述

输入提示搜索

使用一个简单的例子来进行说明
使用一个简单的布局,获取一个AutoCompleteTextView的实例

<?xml version="1.0" encoding="utf-8"?>
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/autocomplete"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
package com.hang.helloamap;

import java.util.ArrayList;

public class PoiInputInfoActivity extends Activity implements TextWatcher {
    AutoCompleteTextView autoCompleteTextView;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_poi_input_info);

        autoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.autocomplete);
        autoCompleteTextView.addTextChangedListener(this);
        autoCompleteTextView.setThreshold(1);

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(s.toString())) {
            Inputtips inputTips = new Inputtips(this, 
//搜索结果的处理
new InputtipsListener() {
                @Override
                public void onGetInputtips(final List<Tip> tipList, int rCode) {
                    if(tipList!=null&&tipList.size()>0){

                    List<String> listString = new ArrayList<>();
                    for (int i = 0; i < tipList.size(); i++) {
                        listString.add(tipList.get(i).getName());
                    }

                    adapter = new ArrayAdapter<String>(
                            PoiInputInfoActivity.this,
                            android.R.layout.simple_dropdown_item_1line);
                    autoCompleteTextView.setAdapter(adapter);
                    adapter.addAll(listString);


                    }
                }
            });
            try {
// 发送输入提示请求
// 第一个参数表示提示关键字,第二个参数默认代表全国,也可以为城市区号
                inputTips.requestInputtips(s.toString(), "");
            } catch (AMapException e) {
                e.printStackTrace();
            }
        }
    }

}

这里写图片描述

步行路径规划

步行路径规划可以根据起终点和步行路线的数据,使用WalkRouteOverlay 画出步行路线图层,包括起终点和转弯点,另外也可以自定义起终点和步行转弯点的图标
1.设置搜索条件
通过 WalkRouteQuery(RouteSearch.FromAndTo ft, int mode) 设置搜索条件,参数ft,路径的起终点;mode,计算路径的模式,可选,默认为最快捷
2.发送请求和接收数据
根据给出的起终点坐标和路径的模式(模式类型参照参考手册类 RouteSearch),使用类 RouteSearch 的calculateWalkRouteAsyn(WalkRouteQuery query)
方法进行步行规划路径计算,在 RouteSearch.OnRouteSearchListener 接口回
调方法 void onWalkRouteSearched(WalkRouteResult walkRouteResult,int rCode) 处理步行规划路径结果

做一个Demo,体验一下效果

package com.pansoft.oilgas.gaodenavigation;

import android.app.Activity;
import android.os.Bundle;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.overlay.WalkRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;

public class QueryPoiActivity extends Activity implements
        RouteSearch.OnRouteSearchListener{
    MapView mapView;
    AMap aMap;
    RouteSearch routeSearch;
    WalkRouteResult walkRouteResult;
    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {

    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult result, int rCode) {
        if(rCode == 1000){
            if(result != null && result.getPaths() != null && result.getPaths().size() > 0){
                walkRouteResult = result;
                WalkPath walkPath = walkRouteResult.getPaths().get(0);
                aMap.clear();//清理之前的图标
                WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(
                        this, aMap, walkPath,walkRouteResult.getStartPos(),
                        walkRouteResult.getTargetPos());
                walkRouteOverlay.removeFromMap();
                walkRouteOverlay.addToMap();
                walkRouteOverlay.zoomToSpan();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapView=new MapView(this);
        setContentView(mapView);
        mapView.onCreate(savedInstanceState);

        aMap = mapView.getMap();

        routeSearch = new RouteSearch(this);
        routeSearch.setRouteSearchListener(this);
        //千佛山
        LatLonPoint startPoint=new LatLonPoint(36.6302087907,117.0290366152);
        //大明湖
        LatLonPoint endPoint=new LatLonPoint(36.6634645884,117.0115754929);

        RouteSearch.FromAndTo fromAndTo=new RouteSearch.FromAndTo(startPoint,endPoint);
        int walkMode=RouteSearch.WalkDefault;
        RouteSearch.WalkRouteQuery query = new RouteSearch.WalkRouteQuery(fromAndTo, walkMode);
        routeSearch.calculateWalkRouteAsyn(query);

    }

    @Override
    protected void onResume() {

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

    @Override
    protected void onPause() {

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

    @Override
    protected void onDestroy() {

        super.onDestroy();
        mapView.onDestroy();
    }
}

这里写图片描述

公交路径规划

这个和步行路径规范只有很少的差别,写一个Demo感受一下

package com.pansoft.oilgas.gaodenavigation;

import android.app.Activity;
import android.os.Bundle;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.overlay.BusRouteOverlay;
import com.amap.api.maps.overlay.WalkRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusPath;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;

public class QueryPoiActivity extends Activity implements
        RouteSearch.OnRouteSearchListener {
    MapView mapView;
    AMap aMap;
    RouteSearch routeSearch;
    BusRouteResult busRouteResult;

    @Override
    public void onBusRouteSearched(BusRouteResult result, int rCode) {
        if (rCode == 1000) {
            if (result != null && result.getPaths() != null && result.getPaths().size() > 0) {
                busRouteResult = result;
                BusPath busPath = busRouteResult.getPaths().get(0);
                aMap.clear();//清理之前的图标
                BusRouteOverlay routeOverlay = new BusRouteOverlay(
                        this, aMap, busPath, busRouteResult.getStartPos(),
                        busRouteResult.getTargetPos());
                routeOverlay.removeFromMap();
                routeOverlay.addToMap();
                routeOverlay.zoomToSpan();
            }
        }
    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {

    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult result, int rCode) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapView = new MapView(this);
        setContentView(mapView);
        mapView.onCreate(savedInstanceState);

        aMap = mapView.getMap();

        routeSearch = new RouteSearch(this);
        routeSearch.setRouteSearchListener(this);


        //千佛山
        LatLonPoint startPoint = new LatLonPoint(36.6302087907, 117.0290366152);
        //大明湖
        LatLonPoint endPoint = new LatLonPoint(36.6634645884, 117.0115754929);


        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(startPoint, endPoint);

        int busMode = RouteSearch.BusLeaseChange;
        String city = "济南";
        int calculateNight = 0;

/* 第一个参数表示路径规划的起点和终点,第二个参数表示公交查询模式 第三个参数表示公交查询城市区号,第四个参数表示是否计算夜班车, 0表示不计算*/
        RouteSearch.BusRouteQuery query = new RouteSearch.BusRouteQuery(fromAndTo, busMode, city, calculateNight);
        routeSearch.calculateBusRouteAsyn(query);


    }

    @Override
    protected void onResume() {

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

    @Override
    protected void onPause() {

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

    @Override
    protected void onDestroy() {

        super.onDestroy();
        mapView.onDestroy();
    }
}

这里写图片描述

驾车路径规划

驾车路径规划可以根据起终点和驾车路线的数据,使用 DrivingRouteOverlay 画出驾车路线图层,包括起终点和转弯点,另外也可以自定义起终点和驾车转弯点的图标
1.设置搜索条件
目前驾车路径规划支持途经点、避让区域以及支持选择一条避让道路,通过
DriveRouteQuery(RouteSearch.FromAndTo ft, int mode, List
passedByPoints, List avoidpolygons, String avoidRoad) 设置搜索条件
• 参数 ft,路径的起终点;
• mode,计算路径的模式,可选,默认为速度优先;
• passedByPoints,途经点,可选;
• avoidpolygons,避让区域,可选;
2.发送请求和接收数据
使用类 RouteSearch 的 calculateDriveRouteAsyn(DriveRouteQuery query)
方法进行驾车规划路径计算,在 RouteSearch.OnRouteSearchListener 接口回
调方法 void onDriveRouteSearched(DriveRouteResult driveRouteResult,int
rCode) 处理驾车路径规划结果

不罗嗦了,写一个Demo感受一下

package com.pansoft.oilgas.gaodenavigation;

import android.app.Activity;
import android.os.Bundle;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.overlay.DrivingRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;

public class QueryPoiActivity extends Activity implements
        RouteSearch.OnRouteSearchListener {
    MapView mapView;
    AMap aMap;
    RouteSearch routeSearch;
    DriveRouteResult driveRouteResult;

    @Override
    public void onBusRouteSearched(BusRouteResult result, int rCode) {
    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult result, int rCode) {
        if(rCode == 1000){
            if(result != null && result.getPaths() != null && result.getPaths().size() > 0){
                driveRouteResult = result;
                DrivePath drivePath = driveRouteResult.getPaths().get(0);
                aMap.clear();//清理之前的图标
                DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
                        this, aMap, drivePath,driveRouteResult.getStartPos(),
                        driveRouteResult.getTargetPos());
                drivingRouteOverlay.removeFromMap();
                drivingRouteOverlay.addToMap();
                drivingRouteOverlay.zoomToSpan();
            }
        }}

    @Override
    public void onWalkRouteSearched(WalkRouteResult result, int rCode) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapView = new MapView(this);
        setContentView(mapView);
        mapView.onCreate(savedInstanceState);

        aMap = mapView.getMap();

        routeSearch = new RouteSearch(this);
        routeSearch.setRouteSearchListener(this);


        //千佛山
        LatLonPoint startPoint = new LatLonPoint(36.6302087907, 117.0290366152);
        //大明湖
        LatLonPoint endPoint = new LatLonPoint(36.6634645884, 117.0115754929);


        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(startPoint, endPoint);
        int drivingMode = RouteSearch.DrivingDefault;
        // 第一个参数表示路径规划的起点和终点,第二个参数表示驾车模式
// 第三个参数表示途经点,第四个参数表示避让区域,第五个参数表示避让道路
        RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, drivingMode, null, null, "");
        routeSearch.calculateDriveRouteAsyn(query);

    }

    @Override
    protected void onResume() {

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

    @Override
    protected void onPause() {

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

    @Override
    protected void onDestroy() {

        super.onDestroy();
        mapView.onDestroy();
    }
}

这里写图片描述

公交线路查询

您可以根据线路名称、途经站点、线路ID查询公交线路信息,根据线路名称和ID可以通过如下方法查询:
1.设置搜索条件
根据 BusLineSearch(Context act, new BusQuery(String query,
BusLineQuery.SearchType ctgr, String city)) 创建一个 BusLineSearch 对象,并设置查询条件,查询类型参数 ctgr 此处设置为 :
BusLineQuery.SearchType.BY_LINE_NAME(名称查询)或
BusLineQuery.SearchType. BY_LINE_ID(ID查询)
2.发送请求和接收数据
使用 BusLineSearch.searchBusLineAsyn() 搜索公交线路,在
BusLineSearch.OnBusLineSearchListener 的接口回调方法
onBusLineSearched(BusLineResult busLineResult, int rCode) 处理返回结果,
当根据线路名称搜索无结果时,会自动匹配关键字为途经点名称进行搜索

一个小Demo,看一下效果吧

package com.pansoft.oilgas.gaodenavigation;

import android.app.Activity;
import android.os.Bundle;

import com.amap.api.services.busline.BusLineItem;
import com.amap.api.services.busline.BusLineQuery;
import com.amap.api.services.busline.BusLineResult;
import com.amap.api.services.busline.BusLineSearch;

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

public class MainActivity extends Activity implements
        BusLineSearch.OnBusLineSearchListener {
    BusLineQuery busLineQuery;
    BusLineResult busLineResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        busLineQuery = new BusLineQuery("116", BusLineQuery.SearchType.BY_LINE_NAME, "济南");
        busLineQuery.setPageSize(10);
        busLineQuery.setPageNumber(0);
        BusLineSearch busLineSearch = new BusLineSearch(this, busLineQuery);
        busLineSearch.setOnBusLineSearchListener(this);
        busLineSearch.searchBusLineAsyn();
    }

    public void onBusLineSearched(BusLineResult result, int rCode) {
        if (rCode == 1000) {
            if (result != null && result.getQuery() != null &&
                    result.getQuery().equals(busLineQuery)) {
                if (result.getQuery().getCategory() == BusLineQuery.SearchType.BY_LINE_NAME) {
                    if (result.getPageCount() > 0 && result.getBusLines() != null
                            && result.getBusLines().size() > 0) {
                        busLineResult = result;
                        List<BusLineItem> lineItems = result.getBusLines();
                        ArrayList<BusLineItem> arrayList = new ArrayList<>();
                        arrayList.addAll(lineItems);

                        BusLineDialogFragment busLineDialogFragment = new BusLineDialogFragment();

                        Bundle bundle = new Bundle();
                        bundle.putParcelableArrayList("data", arrayList);
                        busLineDialogFragment.setArguments(bundle);
                        busLineDialogFragment.show(getFragmentManager(), "xxxx");


                    }
                }
            }
        }
    }

}
package com.pansoft.oilgas.gaodenavigation;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Parcel;

import com.amap.api.services.busline.BusLineItem;
import com.amap.api.services.busline.BusLineQuery;
import com.amap.api.services.busline.BusLineResult;
import com.amap.api.services.busline.BusLineSearch;

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

public class BusLineDialogFragment extends DialogFragment {

    private String[] busArray;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayList<BusLineItem> busLineResultList = getArguments().getParcelableArrayList("data");
        busArray = new String[busLineResultList.size()];

        for (int j = 0; j < busLineResultList.size(); j++) {
            busArray[j] = busLineResultList.get(j).getBusLineName();
        }

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setItems(busArray, null);
        builder.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dismiss();
            }
        });
        builder.setTitle("查询结果");


        return builder.show();


    }
}

不熟悉DialogFragment的同学,请参考:
AlterDialog在DialogFragment中的使用

这里写图片描述

前面那个例子挺无聊的,下面做一个有点实际应用价值的——查询经过某一个站点的所有公交线路

如果通过途经站点查询可以使用如下方法:
1.设置搜索条件
根据 BusStationSearch(Context act, new BusQuery(String query, String
city)) 创建一个 BusStationSearch 对象,并设置查询条件
2.发送请求和接收数据
通过设置 busStationSearch.setOnBusLineSearchListener(this)监听,使用
BusStationSearch.searchBusStationAsyn() 搜索结果,并通过
OnBusStationSearchListener.onBusStationSearched() 返回数据,结果返回车站ID、车站经纬度坐标、车站区域编码、车站名称、经过此公交站的公交路线等信息

下面咱们查询一下途经大济南齐鲁软件园的公交线路

package com.pansoft.oilgas.gaodenavigation;

import android.app.Activity;
import android.os.Bundle;

import com.amap.api.services.busline.BusStationItem;
import com.amap.api.services.busline.BusStationQuery;
import com.amap.api.services.busline.BusStationResult;
import com.amap.api.services.busline.BusStationSearch;

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

public class MainActivity extends Activity implements
        BusStationSearch.OnBusStationSearchListener {

    BusStationQuery busStationQuery;
    BusStationSearch busStationSearch;
    BusStationResult busStationResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        busStationQuery = new BusStationQuery("齐鲁软件园", "济南");
        busStationSearch = new BusStationSearch(this, busStationQuery);
        busStationSearch.setOnBusStationSearchListener(this);
        busStationSearch.searchBusStationAsyn();
    }

    @Override
    public void onBusStationSearched(BusStationResult result, int rCode) {
        if (rCode == 1000) {
            if (result != null && result.getQuery() != null &&
                    result.getQuery().equals(busStationQuery)) {
                Android 高德地图的定位,周边搜索

高德怎么定位一个地方搜附近的影院

手机高德地图导航原理

手机高德地图怎么导航 高德地图怎么用

高德地图 怎样看乡镇的范围

在pc端集成地图功能