1.地点检索输入提示服务(也被称为POI热词建议检索、在线建议检索、Suggestion POI search),简称Sug检索,是指根据关键词查询在线建议词。
2.实现Sug检索的步骤
(1)在activity_main布局文件中实现界面的设计
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<AutoCompleteTextView
android:id="@+id/editaddress"
android:layout_width="match_parent"
android:background="@null"
android:completionThreshold="2"
android:hint="请输入地址"
android:padding="15dp"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="220dp">
<com.baidu.mapapi.map.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.baidu.mapapi.map.MapView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:paddingBottom="20dp"
android:src="@mipmap/dingwei2" />
<ImageView
android:id="@+id/sa_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:src="@mipmap/icon_location" />
</RelativeLayout>
<TextView
android:id="@+id/jieguo"
android:layout_width="match_parent"
android:text="输入位置附近的位置"
android:padding="15dp"
android:layout_height="match_parent" />
</LinearLayout>
界面设计如下
(2)在item.xml文件中设计热词提示框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/dianji"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:src="@mipmap/icon_dingwei"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:paddingLeft="10dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:text="name"
android:textSize="15sp"
android:textColor="#000"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/address"
android:layout_width="match_parent"
android:text="address"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f2f2f2"/>
</LinearLayout>
设计如下:
(3)在Main_Acticity.java中的相应操作
初始化相应的控件,Sug检索实例和监听器
public void setInit() {
mMapView = findViewById(R.id.mapview);
mBaiduMap = mMapView.getMap();
mSuggestionSearch = SuggestionSearch.newInstance();
mSuggestionSearch.setOnGetSuggestionResultListener(listener);
mMapView.showZoomControls(false);
mSaLocation = findViewById(R.id.sa_location);
mSaLocation.setOnClickListener(this);
mEditAddress = findViewById(R.id.editaddress);
mJieguo = findViewById(R.id.jieguo);
}
创建Sug检索监听器
OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
public void onGetSuggestionResult(SuggestionResult res) {
if (res == null || res.getAllSuggestions() == null) {//未找到相关结果
return;
} else {//获取在线建议检索结果
List<SuggestionResult.SuggestionInfo> resl = res.getAllSuggestions();
stringlist.clear();
stringlist2.clear();
for (int i = 0; i < resl.size(); i++) {
stringlist.add(resl.get(i).key);
stringlist2.add(resl.get(i).city+resl.get(i).district+resl.get(i).key);
latLng = resl.get(i).pt;
}
mJieguo.setText("" + resl);
AutoEditTextAdapter adapter = new AutoEditTextAdapter(stringlist,stringlist2, MainActivity.this);
mEditAddress.setAdapter(adapter);
adapter.setOnItemClickListener(new AutoEditTextAdapter.OnItemClickListener() {
@Override
public void onClick(int position) {
Toast.makeText(MainActivity.this,""+position,Toast.LENGTH_SHORT).show();
mEditAddress.setText(stringlist.get(position));
mEditAddress.dismissDropDown();
}
@Override
public void onLongClick(int position) {
}
});
}
}
};
对输入框的监听,发起检索,显示相关的热词
public void setEdit(final String city) {
//点击就自动提示
mEditAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditAddress.showDropDown();
}
});
mEditAddress.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) {
mSuggestionSearch.requestSuggestion((new SuggestionSearchOption())
.keyword(mEditAddress.getText().toString())
.city(city));
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
Main_Activity.java的完整代码
package com.example.global_1;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements View.OnClickListener {
private MapView mMapView;
private BaiduMap mBaiduMap;
private ImageView mSaLocation;
private LocationClient mLocationClient;
private LatLng latLng;
private boolean isFirstLoc = true;//是否是首次定位
public BDLocationListener myListener = new MyLocationListener();
private MyLocationData locData;
private double mLatitude;
private double mLongitude;
private TextView mJieguo;
private AutoCompleteTextView mEditAddress;
private List<String> stringlist = new ArrayList<>();
private List<String> stringlist2 = new ArrayList<>();
private SuggestionSearch mSuggestionSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
SDKInitializer.initialize(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setInit();
initMap();
}
public void setInit() {
mMapView = findViewById(R.id.mapview);
mBaiduMap = mMapView.getMap();
mSuggestionSearch = SuggestionSearch.newInstance();
mSuggestionSearch.setOnGetSuggestionResultListener(listener);
mMapView.showZoomControls(false);
mSaLocation = findViewById(R.id.sa_location);
mSaLocation.setOnClickListener(this);
mEditAddress = findViewById(R.id.editaddress);
mJieguo = findViewById(R.id.jieguo);
}
/**
* 监听输入框的输入
*/
public void setEdit(final String city) {
//点击就自动提示
mEditAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditAddress.showDropDown();
}
});
mEditAddress.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) {
mSuggestionSearch.requestSuggestion((new SuggestionSearchOption())
.keyword(mEditAddress.getText().toString())
.city(city));
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
public void onGetSuggestionResult(SuggestionResult res) {
if (res == null || res.getAllSuggestions() == null) {//未找到相关结果
return;
} else {//获取在线建议检索结果
List<SuggestionResult.SuggestionInfo> resl = res.getAllSuggestions();
stringlist.clear();
stringlist2.clear();
for (int i = 0; i < resl.size(); i++) {
stringlist.add(resl.get(i).key);
stringlist2.add(resl.get(i).city+resl.get(i).district+resl.get(i).key);
latLng = resl.get(i).pt;
}
mJieguo.setText("" + resl);
AutoEditTextAdapter adapter = new AutoEditTextAdapter(stringlist,stringlist2, MainActivity.this);
mEditAddress.setAdapter(adapter);
adapter.setOnItemClickListener(new AutoEditTextAdapter.OnItemClickListener() {
@Override
public void onClick(int position) {
Toast.makeText(MainActivity.this,""+position,Toast.LENGTH_SHORT).show();
mEditAddress.setText(stringlist.get(position));
mEditAddress.dismissDropDown();
}
@Override
public void onLongClick(int position) {
}
});
}
}
};
/**
* 定位
*/
private void initMap() {
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);//默认显示普通地图
mBaiduMap.setMyLocationEnabled(true);// 开启定位图层
mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类
initLocation();//配置定位SDK参数
mLocationClient.registerLocationListener(myListener); //注册监听函数
mLocationClient.start();//开启定位
mLocationClient.requestLocation();//图片点击事件,回到定位点
}
/**
* 配置定位SDK参数
*/
private void initLocation() {
LocationClientOption option = new LocationClientOption();
option.setCoorType("bd09ll");//可选,coorType - 取值有3个: 返回国测局经纬度坐标系:gcj02 返回百度墨卡托坐标系 :bd09 返回百度经纬度坐标系 :bd09ll
Log.e("获取地址信息设置", option.getAddrType());//获取地址信息设置
option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true); // 是否打开gps进行定位
option.setLocationNotify(true);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果
option.setScanSpan(1000);//可选,设置的扫描间隔,单位是毫秒,设置发起定位请求的间隔需要大于等于1000ms才是有效的
Log.e("获取设置的Prod字段值", option.getProdName());
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setNeedDeviceDirect(true);//在网络定位时,是否需要设备方向- true:需要 ; false:不需要。默认为false
option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation
// .getLocationDescribe里得到,结果类似于“在北京天安门附近”
option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要
mLocationClient.setLocOption(option);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.sa_location:
//把定位点再次显现出来
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(mapStatusUpdate);
break;
}
}
//实现BDLocationListener接口,BDLocationListener为结果监听接口,异步获取定位结果
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(final BDLocation location) {
latLng = new LatLng(location.getLatitude(), location.getLongitude());
// 构造定位数据
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
.direction(100)// 此处设置开发者获取到的方向信息,顺时针0-360
.latitude(mLatitude)
.longitude(mLongitude).build();
mBaiduMap.setMyLocationData(locData);// 设置定位数据
setEdit(location.getCity());
if (isFirstLoc) {
isFirstLoc = false;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(latLng).zoom(18.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
Toast.makeText(MainActivity.this, location.getAddrStr(), Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
Toast.makeText(MainActivity.this, location.getAddrStr(), Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
Toast.makeText(MainActivity.this, location.getAddrStr(), Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeServerError) {//服务器错误
Toast.makeText(MainActivity.this, "服务器错误,请检查", Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {//网络错误
Toast.makeText(MainActivity.this, "网络错误,请检查", Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {//手机模式错误
Toast.makeText(MainActivity.this, "手机模式错误,请检查是否飞行", Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
}
(4)用Android studio的模拟机运行的效果如下
模拟机的GPS没打开,无法显示效果,你们可以参考下面链接,连接手机测试
https://blog.csdn.net/m0_37108482/article/details/80841689