Android using Baidu Map API: City POI search-Get all results

Source: Internet
Author: User


This article mainly explains how to search through the Baidu Map API to get all the poi in a city. It is necessary to emphasize the word "all" in order to attract attention, because in the search for POI, the default returns only one page of 10 search results, then how to get all the search results? In fact, the Baidu Map API is to provide a relevant method, but I found that a considerable number of netizens are asking this question, so it is necessary to explain the demonstration.



What do you call "City Poi search" first? It with us in the previous article ([011] Baidu Map API Poi Search-find points of interest around you, such as supermarkets, restaurants, ATM ... What is the difference between the POI search that is learned in the (Android)?



The previous article called the Map API "Peripheral POI Search" service, that is, to retrieve the number of meters around the poi, and this chapter is called the Map API "City POI Search" service, that is, to retrieve all the poi in a city. If you look at these two articles, you will find that only the method that is invoked is different, and that the search results are handled in the same way that the search results are handled exactly the same.



A complete example of a city poi search is given below, and it explains how to get all the search results.



1) Layout file Res/layout/poi_city_search.xml



<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >  
    <com.baidu.mapapi.MapView android:id="@+id/map_View"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
    />  
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/map_View"
        android:layout_alignLeft="@id/map_View"
        android:layout_alignRight="@id/map_View"
        android:background="@null"
        android:padding="0dip"
        >  
        <EditText android:id="@+id/keyword_edittext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="20" />  
        <Button android:id="@+id/query_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:text="search" />  
    </LinearLayout>  
</RelativeLayout>


2) inherits the Com.baidu.mapapi.MapActivity activity class


package com.liufeng.baidumap;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.PoiOverlay;
public class PoiSearchInCityActivity extends MapActivity {
//Define map engine management class
private BMapManager mapManager;
//Define search service class
private MKSearch mMKSearch;
private MapView mapView;
private MapController mapController;
private EditText keyWordEditText;
private Button queryButton;
private static StringBuilder sb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.poi_city_search);
//Initialize mapactivity
mapManager = new BMapManager(getApplication());
//The first parameter of init method needs to be filled in the API key applied
mapManager.init("285B415EBAB2A92293E85502150ADA7F03C777C4", null);
super.initMapActivity(mapManager);
mapView = (MapView) findViewById(R.id.map_View);
//Set map mode to traffic map
mapView.setTraffic(true);
//Set enable built-in zoom control
mapView.setBuiltInZoomControls(true);
//Get the map controller object to control MapView
mapController = mapView.getController();
//Set the default zoom level of the map
mapController.setZoom(10);
//Set the number of POIs returned per page. The default value is 10. The value range is 1-50
MKSearch.setPoiPageCapacity(10);
//Initialize mksearch
mMKSearch = new MKSearch();
mMKSearch.init(mapManager, new MySearchListener());
keyWordEditText = (EditText) findViewById(R.id.keyword_edittext);
queryButton = (Button) findViewById(R.id.query_button);
queryButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Contents in previous SB are cleared before each search
sb = new StringBuilder();
String keyWord = keyWordEditText.getText().toString().trim();
//Search Wal Mart in Guiyang
Mkksearch.poisearchincity ("Guiyang", keyword);
}
};
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
protected void onDestroy() {
if (mapManager != null) {
//This method needs to be called before the program exits
mapManager.destroy();
mapManager = null;
}
super.onDestroy();
}
@Override
protected void onPause() {
if (mapManager != null) {
//Terminate Baidu map API
mapManager.stop();
}
super.onPause();
}
@Override
protected void onResume() {
if (mapManager != null) {
//Open Baidu map API
mapManager.start();
}
super.onResume();
}
* *
*Implement mksearchlistener interface for asynchronous search service
*
* @author liufeng
* /
public class MySearchListener implements MKSearchListener {
* *
*Search address information results based on latitude and longitude
*
*@ param result search results
*@ param ierror error number (0 means correct return)
* /
@Override
public void onGetAddrResult(MKAddrInfo result, int iError) {
}
* *
*Driving route search results
*
*@ param result search results
*@ param ierror error number (0 means correct return)
* /
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult result, int iError) {
}
* *
*POI search results (range search, city POI search, peripheral search)
*
*@ param result search results
*@ param type returns the result type (11,12,21: POI list 7: city list)
*@ param ierror error number (0 means correct return)
* /
@Override
public void onGetPoiResult(MKPoiResult result, int type, int iError) {
if (result == null) {
Return;
}
//Clear all existing covers on the map
mapView.getOverlays().clear();
//Poioverlay is the overlay provided by Baidu map API to display poi
PoiOverlay poioverlay = new PoiOverlay(PoiSearchInCityActivity.this, mapView);
//Set POI data searched
poioverlay.setData(result.getAllPoi());
//Show poioverlay on the map (mark the searched interest points on the map)
mapView.getOverlays().add(poioverlay);
if(result.getNumPois() > 0) {
//Set the geographic coordinates of one of the search results as the center of the map
MKPoiInfo poiInfo = result.getPoi(0);
mapController.setCenter(poiInfo.pt);
}
Sb. Append ("found in total"). Append (result. Getnumpois()). Append ("POI / N");
//POI returned by traversing the current page (only 10 are returned by default)
for (MKPoiInfo poiInfo : result.getAllPoi()) {
Sb. Append ("Name:. Append (poiinfo. Name). Append (" / N ");
//Sb. Append ("address:. Append (poiinfo. Address). Append (" / N ");
//Sb. Append ("longitude:. Append (poiinfo. Pt. Getlongitudee6() / 1000000.0f). Append (" / N ");
//Sb. Append ("latitude:. Append (poiinfo. Pt. Getlatitudee6() / 1000000.0f). Append (" / N ");
}
//Display POI found by current page through alertdialog
new AlertDialog.Builder(PoiSearchInCityActivity.this)
. settitle ("POI information searched")
.setMessage(sb.toString())
. setpositivebutton ("off", new dialoginterface. Onclicklistener(){
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}).create().show();
}
* *
*Bus transfer route search results
*
*@ param result search results
*@ param ierror error number (0 means correct return)
* /
@Override
public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {
}
* *
*Pedestrian route search results
*
*@ param result search results
*@ param ierror error number (0 means correct return)
* /
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult result, int iError) {
}
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.