Commit cdb591b9 authored by fengruiyu's avatar fengruiyu

搜索页面的搜索功能已完成(但是回调还没有处理)

搜索的历史记录的展示和保存到本地已完成(小细节未处理)
parent ae99582d
package com.mints.street.adapter
import android.content.Context
import android.text.TextUtils
import androidx.databinding.ViewDataBinding
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import com.blankj.utilcode.util.SPUtils
import com.fry.base.adapter.AbstractVLayoutBaseAdapter
import com.fry.base.adapter.BindingViewHolder
import com.fry.base.recycler_view.VHMAdapter
......@@ -13,6 +13,7 @@ import com.mints.street.BR
import com.mints.street.R
import com.mints.street.databinding.ItemHistoryRecordAdapterBinding
import com.mints.street.main.home.SearchMapActivity
import me.goldze.mvvmhabit.utils.SPUtils
/**
* Created by 冯瑞雨 on 2021/7/14.
......@@ -36,15 +37,19 @@ class HistoryRecordAdapter(val context: Context, var name: String? = null) :
holder.binding.listView.adapter = adapter
}
private fun getHistoryData(): List<String> {
private fun getHistoryData(): List<String>? {
val instance = SPUtils.getInstance(SearchMapActivity.HISTORY_NAME)
val string = instance.getString("name")
val string = instance.decodeString("name")
if (TextUtils.isEmpty(string)){
return null
}
return Gson().fromJson<List<String>>(string, List::class.java)
}
fun addHistoryData(list:List<String> ) {
val instance = SPUtils.getInstance(SearchMapActivity.HISTORY_NAME)
instance.put("name", Gson().toJson(list).toString())
instance.encode("name", Gson().toJson(list).toString())
}
override fun getItemCount() = 1
......
package com.mints.street.bean
/**
* Created by 冯瑞雨 on 2021/7/15.
*/
class PositioningBean {
//纬度
var latitude:Double?=0.0
//经度
var longitude:Double?=0.0
// 国家码
var countryCode:String?=null
//国家名称
var country:String?=null
//获取省份
var province:String?=null
//获取城市
var city :String?=null
//获取区
var district :String?=null
//获取镇信息
var town :String?=null
//获取街道信息
var street :String?=null
// 地址信息
var addrStr :String?=null
}
\ No newline at end of file
......@@ -20,12 +20,15 @@ import com.baidu.mapapi.search.core.RecommendStopInfo
import com.baidu.mapapi.search.recommendstop.RecommendStopSearch
import com.baidu.mapapi.search.recommendstop.RecommendStopSearchOption
import com.fry.base.base.BaseFragment
import com.google.gson.Gson
import com.mints.street.BR
import com.mints.street.R
import com.mints.street.bean.PositioningBean
import com.mints.street.databinding.FragmentHomeBinding
import me.goldze.mvvmhabit.base.AppManager
import me.goldze.mvvmhabit.utils.KLog
import me.goldze.mvvmhabit.utils.RxUtils
import me.goldze.mvvmhabit.utils.SPUtils
/**
* Created by 冯瑞雨 on 2021/7/2.
......@@ -100,21 +103,21 @@ class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>(), SensorE
mBaiduMap?.setMapStatus(mapStatusUpdate)
mBaiduMap?.setOnMapStatusChangeListener(object : BaiduMap.OnMapStatusChangeListener {
override fun onMapStatusChangeStart(status: MapStatus?) {
KLog.a("百度map", "移动开始")
//KLog.a("百度map", "移动开始")
}
override fun onMapStatusChangeStart(status: MapStatus?, reason: Int) {
KLog.a("百度map", "移动开始,两个参数")
// KLog.a("百度map", "移动开始,两个参数")
}
override fun onMapStatusChange(status: MapStatus?) {
KLog.a("百度map", "移动改变")
//KLog.a("百度map", "移动改变")
}
override fun onMapStatusChangeFinish(status: MapStatus?) {
KLog.a("百度map", "移动结束")
// KLog.a("百度map", "移动结束")
mRecommendStopSearch?.requestRecommendStop(
RecommendStopSearchOption().location(
center
......@@ -181,7 +184,11 @@ class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>(), SensorE
option.isOpenGps = true
// 设置坐标类型
option.setCoorType("bd09ll")
option.setScanSpan(1000)
//可选,默认0,即仅定位一次,设置发起连续定位请求的间隔需要大于等于1000ms才是有效的
option.setScanSpan(100000)//十秒一次
option.setIsNeedAddress(true) // 可选,设置是否需要地址信息,默认不需要
option.setIsNeedLocationDescribe(true) // 可选,设置是否需要地址描述
mLocClient?.locOption = option
mLocClient?.start()
}
......@@ -240,8 +247,28 @@ class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>(), SensorE
/**
* 定位SDK监听函数
*/
var sp:SPUtils?=null
var positioningBean:PositioningBean?=null
inner class MyLocationListener : BDLocationListener {
override fun onReceiveLocation(location: BDLocation) {
if (sp==null){
sp = SPUtils.getInstance(SPUtils.POSITIONING_MAP)
}
if (positioningBean == null){
positioningBean = PositioningBean()
}
positioningBean?.latitude = location.latitude
positioningBean?.longitude = location.longitude
positioningBean?.countryCode = location.countryCode
positioningBean?.country = location.country
positioningBean?.province = location.province
positioningBean?.city = location.city
positioningBean?.district = location.district
positioningBean?.town = location.town
positioningBean?.street = location.street
positioningBean?.addrStr = location.addrStr
sp?.encode(SPUtils.POSITIONING_MAP,Gson().toJson(positioningBean).toString())
// MapView 销毁后不在处理新接收的位置
mBaiduMap?.apply {
mCurrentLat = location.latitude
......
package com.mints.street.main.home
import me.goldze.mvvmhabit.base.ItemViewModel
/**
* Created by 冯瑞雨 on 2021/7/15.
*/
class SearchAfterItem(viewModel: SearchMapViewModel,val name:String?, val address:String?) :
ItemViewModel<SearchMapViewModel>(viewModel) {
}
\ No newline at end of file
......@@ -2,9 +2,15 @@ package com.mints.street.main.home
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Observer
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.VirtualLayoutManager
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 com.fry.base.base.BaseActivity
import com.fry.base.utils.ResourceUtils
import com.mints.street.BR
......@@ -13,11 +19,13 @@ import com.mints.street.adapter.HistoryRecordAdapter
import com.mints.street.adapter.PopularSceneAdapter
import com.mints.street.databinding.ActivitySearchMapBinding
import me.goldze.mvvmhabit.base.AppManager
import me.goldze.mvvmhabit.utils.KLog
/**
* Created by 冯瑞雨 on 2021/7/13.
*/
class SearchMapActivity : BaseActivity<ActivitySearchMapBinding, SearchMapViewModel>() {
class SearchMapActivity : BaseActivity<ActivitySearchMapBinding, SearchMapViewModel>(),
OnGetSuggestionResultListener {
companion object {
const val HISTORY_NAME = "history_name"
fun startSearchMapActivity() {
......@@ -31,12 +39,16 @@ class SearchMapActivity : BaseActivity<ActivitySearchMapBinding, SearchMapViewMo
private var mAdapter: DelegateAdapter? = null
private var index = 0;//0=境内 1=境外
private var historyRecordAdapter: HistoryRecordAdapter? = null
private var popularSceneAdapter: PopularSceneAdapter? = null
//检索模块
private var mSuggestionSearch: SuggestionSearch? = null
override fun initContentView(savedInstanceState: Bundle?) = R.layout.activity_search_map
override fun initVariableId() = BR.viewModel
private var historyRecordAdapter: HistoryRecordAdapter? = null
private var popularSceneAdapter: PopularSceneAdapter? = null
override fun initData() {
super.initData()
......@@ -47,14 +59,41 @@ class SearchMapActivity : BaseActivity<ActivitySearchMapBinding, SearchMapViewMo
binding.recyList.layoutManager = vLayout
binding.recyList.adapter = mAdapter
historyRecordAdapter = HistoryRecordAdapter(this);
// 初始化建议搜索模块,注册建议搜索事件监听
mSuggestionSearch = SuggestionSearch.newInstance()
mSuggestionSearch?.setOnGetSuggestionResultListener(this)
binding.et.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable?) {
if (TextUtils.isEmpty(s)) {
viewModel.searchAfterOneItems.clear()
return
}
// 使用建议搜索服务获取建议列表,结果在onSuggestionResult()中更新
mSuggestionSearch?.requestSuggestion(
SuggestionSearchOption()
.keyword(s.toString()) // 关键字
.city(viewModel.positioningBean?.city ?: "北京")
) // 城市
}
})
historyRecordAdapter = HistoryRecordAdapter(this)
popularSceneAdapter = PopularSceneAdapter(this, listOf("1", "12"))
mAdapter?.addAdapter(historyRecordAdapter)
mAdapter?.addAdapter(popularSceneAdapter)
//境内
binding.territory.setOnClickListener {
territory()
......@@ -63,6 +102,7 @@ class SearchMapActivity : BaseActivity<ActivitySearchMapBinding, SearchMapViewMo
binding.abroad.setOnClickListener {
abroad()
}
}
private fun update() {
......@@ -100,4 +140,24 @@ class SearchMapActivity : BaseActivity<ActivitySearchMapBinding, SearchMapViewMo
binding.abroad.solid = ResourceUtils.getColor(R.color.white)
update()
}
/**
* 获取在线建议搜索结果,得到requestSuggestion返回的搜索结果
*
* @param suggestionResult Sug检索结果
*/
override fun onGetSuggestionResult(suggestionResult: SuggestionResult?) {
if (suggestionResult == null || suggestionResult.allSuggestions == null) {
return
}
viewModel.searchAfterOneItems.clear()
if(TextUtils.isEmpty(binding.et.text)){
return
}
for (info in suggestionResult.allSuggestions) {
viewModel.searchAfterOneItems.add(SearchAfterItem(viewModel
,info.key,info.address))
}
}
}
\ No newline at end of file
package com.mints.street.main.home
import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.SPUtils
import android.text.TextUtils
import android.view.View
import androidx.databinding.ObservableArrayList
import com.google.gson.Gson
import com.mints.street.BR
import com.mints.street.R
import com.mints.street.bean.PositioningBean
import me.goldze.mvvmhabit.base.BaseViewModel
import me.goldze.mvvmhabit.utils.SPUtils
import me.tatarka.bindingcollectionadapter2.ItemBinding
/**
* Created by 冯瑞雨 on 2021/7/13.
......@@ -12,4 +18,20 @@ import me.goldze.mvvmhabit.base.BaseViewModel
class SearchMapViewModel(application: Application) : BaseViewModel(application) {
val searchAfterItemBinding = ItemBinding.of<SearchAfterItem>(BR.viewModel, R.layout.item_search_after)
val searchAfterOneItems = ObservableArrayList<SearchAfterItem>()
//定位位置
val positioningBean: PositioningBean? =getpositioningMap()
private fun getpositioningMap():PositioningBean?{
SPUtils.getInstance(SPUtils.POSITIONING_MAP)
.decodeString(SPUtils.POSITIONING_MAP)?.apply {
if (!TextUtils.isEmpty(this)){
return Gson().fromJson(this,PositioningBean::class.java)
}
}
return null
}
}
\ No newline at end of file
......@@ -74,7 +74,7 @@ class PanoramicActivity : BaseActivity<ActivityPanoramicBinding, PanoramicViewMo
"请在AndoridManifest.xml中输入正确的授权Key,并检查您的网络连接是否正常!error: $iError"
)
} else {
KLog.a("BMapManager", "key认证成功")
KLog.i("BMapManager", "key认证成功")
}
}
}
......
package com.mints.street.webview
import android.os.Bundle
import com.fry.base.base.BaseActivity
import com.mints.street.BR
import com.mints.street.R
import com.mints.street.databinding.ActivityMintsWebViewBinding
/**
* Created by 冯瑞雨 on 2021/7/15.
*/
class MintsWebViewActivity:BaseActivity<ActivityMintsWebViewBinding,MintsWebViewModel>() {
override fun initContentView(savedInstanceState: Bundle?) = R.layout.activity_mints_web_view
override fun initVariableId() = BR.viewModel
}
\ No newline at end of file
package com.mints.street.webview
import android.app.Application
import me.goldze.mvvmhabit.base.BaseViewModel
/**
* Created by 冯瑞雨 on 2021/7/15.
*/
class MintsWebViewModel(application: Application):BaseViewModel(application) {
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.mints.street.webview.MintsWebViewModel" />
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.fry.base.ui.widget.CommonTitleBar
android:id="@+id/ctb_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ctb_theme="dark"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</layout>
\ No newline at end of file
......@@ -66,6 +66,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_bg" />
<EditText
android:id="@+id/et"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:layout_width="match_parent"
......@@ -89,6 +90,19 @@
android:layout_height="wrap_content"
app:ctb_theme="dark"
app:layout_constraintTop_toTopOf="parent" />
<com.fry.base.ui.widget.CustomRecycleView
android:background="@color/white"
android:id="@+id/recy_after_search"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/et"
app:itemBinding="@{viewModel.searchAfterItemBinding}"
app:items="@{viewModel.searchAfterOneItems}"
android:visibility="@{viewModel.searchAfterOneItems.size() == 0 ? view.GONE:view.VISIBLE}"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
......@@ -98,5 +112,8 @@
<variable
name="viewModel"
type="com.mints.street.main.home.SearchMapViewModel" />
<variable
name="view"
type="android.view.View" />
</data>
</layout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@mipmap/map_history_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@{viewModel.name}"
android:textColor="@color/color_555"
android:textSize="14sp"
android:maxLines="1"
android:singleLine="true"
app:layout_constraintBottom_toTopOf="@id/tv_address"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/image"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_address"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:singleLine="true"
android:text="@{viewModel.address}"
android:textColor="@color/color_555"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/image"
app:layout_constraintTop_toBottomOf="@id/tv_name" />
<View
android:layout_width="0dp"
android:layout_height="0.5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/image"
android:background="@color/color_555"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<data>
<variable
name="viewModel"
type="com.mints.street.main.home.SearchAfterItem" />
</data>
</layout>
\ No newline at end of file
......@@ -21,6 +21,7 @@ public final class SPUtils {
private static Map<String, SPUtils> sSPMap = new HashMap<>();
private static final String DEFAULT_KEY = "spUtils";
public static final String POSITIONING_MAP = "positioningMap";
private MMKV mv;
public static void init(Application application){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment