根据百度地图sdk官方文档使用sug检索,没有返回结果。suggestionResult.error打印出来是RESULT_NOT_FOUND。
下面给出代码。求求大佬解答
权限获取
<!-- 这个权限用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!-- 这个权限用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<!-- 访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET"/>
Activity代码
private SuggestionSearch suggestionSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(this.getApplication());
setContentView(R.layout.activity_location);
init();
initLocationData();
}
private void init(){
backButton = findViewById(R.id.back_button_L);
locationAddressTV = findViewById(R.id.location_address);
locationCityTV = findViewById(R.id.location_city);
searchAddressET = findViewById(R.id.search_address_ET);
recyclerView = findViewById(R.id.suggestion_address_recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
suggestionAddressAdapter = new SuggestionAddressAdapter(list);
recyclerView.setAdapter(suggestionAddressAdapter);
backButton.setOnClickListener(this);
locationCityTV.setOnClickListener(this);
locationAddressTV.setOnClickListener(this);
searchAddressET.addTextChangedListener(new TextWatcher() {
@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 city = locationCityTV.getText().toString();
if (s.length()<=0){
return;
}
if (!city.equals("")){
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_SHORT).show();
suggestionSearch.requestSuggestion(new SuggestionSearchOption().keyword(s.toString()).city(city));
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void initLocationData(){
locationClient = new LocationClient(getApplicationContext());
locationClient.registerLocationListener(myLocationListener);
option = new LocationClientOption();
option.setIsNeedAddress(true);
option.setNeedNewVersionRgc(true);
locationClient.setLocOption(option);
locationClient.start();
suggestionSearch = SuggestionSearch.newInstance();
suggestionSearch.setOnGetSuggestionResultListener(listener);
}
OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
@Override
public void onGetSuggestionResult(SuggestionResult suggestionResult) {
if (suggestionResult == null || suggestionResult.getAllSuggestions() == null) {
Toast.makeText(getApplicationContext(), "wrong2"+suggestionResult.error,Toast.LENGTH_SHORT).show();
return;
}
list.clear();
Toast.makeText(getApplicationContext(), "work"+suggestionResult.getAllSuggestions().get(0).toString(),Toast.LENGTH_SHORT).show();
for (SuggestionResult.SuggestionInfo suggestionInfo:suggestionResult.getAllSuggestions()) {
list.add(suggestionInfo.toString());
}
suggestionAddressAdapter.setSuggestionAddresses(list);
suggestionAddressAdapter.notifyDataSetChanged();
}
};