|
@@ -8,8 +8,6 @@ import android.graphics.Point
|
|
|
import android.location.Location
|
|
|
import android.location.LocationRequest
|
|
|
import android.os.Bundle
|
|
|
-import android.os.Handler
|
|
|
-import android.os.Looper
|
|
|
import android.text.Editable
|
|
|
import android.text.TextWatcher
|
|
|
import android.view.LayoutInflater
|
|
@@ -17,40 +15,47 @@ import android.view.Menu
|
|
|
import android.view.View
|
|
|
import android.view.inputmethod.InputMethodManager
|
|
|
import android.widget.ArrayAdapter
|
|
|
-import android.widget.AutoCompleteTextView
|
|
|
import android.widget.CheckBox
|
|
|
import android.widget.CompoundButton
|
|
|
import android.widget.ImageView
|
|
|
import android.widget.SeekBar
|
|
|
import android.widget.SeekBar.OnSeekBarChangeListener
|
|
|
import android.widget.TextView
|
|
|
+import androidx.appcompat.widget.AppCompatAutoCompleteTextView
|
|
|
import androidx.appcompat.widget.Toolbar
|
|
|
import androidx.core.content.ContextCompat
|
|
|
-import com.amap.api.location.AMapLocationClient
|
|
|
-import com.amap.api.location.AMapLocationClientOption
|
|
|
-import com.amap.api.maps.AMap
|
|
|
-import com.amap.api.maps.CameraUpdateFactory
|
|
|
-import com.amap.api.maps.SupportMapFragment
|
|
|
-import com.amap.api.maps.model.BitmapDescriptorFactory
|
|
|
-import com.amap.api.maps.model.CameraPosition
|
|
|
-import com.amap.api.maps.model.CircleOptions
|
|
|
-import com.amap.api.maps.model.LatLng
|
|
|
-import com.amap.api.maps.model.LatLngBounds
|
|
|
-import com.amap.api.maps.model.MarkerOptions
|
|
|
-import com.amap.api.services.core.LatLonPoint
|
|
|
-import com.amap.api.services.geocoder.GeocodeResult
|
|
|
-import com.amap.api.services.geocoder.GeocodeSearch
|
|
|
-import com.amap.api.services.geocoder.RegeocodeQuery
|
|
|
-import com.amap.api.services.geocoder.RegeocodeResult
|
|
|
-import com.amap.api.services.help.Inputtips
|
|
|
-import com.amap.api.services.help.InputtipsQuery
|
|
|
-import com.amap.api.services.help.Tip
|
|
|
+import com.baidu.location.BDAbstractLocationListener
|
|
|
+import com.baidu.location.BDLocation
|
|
|
+import com.baidu.location.LocationClient
|
|
|
+import com.baidu.location.LocationClientOption
|
|
|
+import com.baidu.mapapi.map.BaiduMap
|
|
|
+import com.baidu.mapapi.map.BitmapDescriptorFactory
|
|
|
+import com.baidu.mapapi.map.CircleOptions
|
|
|
+import com.baidu.mapapi.map.MapPoi
|
|
|
+import com.baidu.mapapi.map.MapStatus
|
|
|
+import com.baidu.mapapi.map.MapStatusUpdateFactory
|
|
|
+import com.baidu.mapapi.map.MapView
|
|
|
+import com.baidu.mapapi.map.MarkerOptions
|
|
|
+import com.baidu.mapapi.map.Stroke
|
|
|
+import com.baidu.mapapi.model.LatLng
|
|
|
+import com.baidu.mapapi.model.LatLngBounds
|
|
|
+import com.baidu.mapapi.search.core.SearchResult
|
|
|
+import com.baidu.mapapi.search.geocode.GeoCodeResult
|
|
|
+import com.baidu.mapapi.search.geocode.GeoCoder
|
|
|
+import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener
|
|
|
+import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption
|
|
|
+import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult
|
|
|
+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.github.pengrad.mapscaleview.MapScaleView
|
|
|
import com.sikey.interconnect.R
|
|
|
import com.sikey.interconnect.app.DataManager.Companion.instance
|
|
|
import com.sikey.interconnect.component.helper.FenceHelper
|
|
|
import com.sikey.interconnect.component.log.Logger
|
|
|
import com.sikey.interconnect.component.network.http.model.ChildInfo
|
|
|
+import com.sikey.interconnect.constant.Constant
|
|
|
import com.sikey.interconnect.constant.FenceCategoryConstants
|
|
|
import com.sikey.interconnect.data.bean.FenceInfoBean
|
|
|
import com.sikey.interconnect.k2.utils.BitmapUtils
|
|
@@ -60,12 +65,12 @@ import com.sikey.interconnect.ui.avtivity.component.left.BabyDetailActivity
|
|
|
import com.sikey.interconnect.ui.custom.layout.DrawFenceRelativeLayout
|
|
|
import com.sikey.interconnect.ui.custom.layout.DrawFenceRelativeLayout.OnDrawPolygonListener
|
|
|
import com.sikey.interconnect.utils.PermissionUtils
|
|
|
+import com.sikey.interconnect.utils.SharedPreferenceUtil
|
|
|
import com.sikey.interconnect.utils.ToastUtils
|
|
|
import com.tbruyelle.rxpermissions2.RxPermissions
|
|
|
|
|
|
class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, OnDrawPolygonListener,
|
|
|
- AMap.OnCameraChangeListener,
|
|
|
- CompoundButton.OnCheckedChangeListener, GeocodeSearch.OnGeocodeSearchListener {
|
|
|
+ BaiduMap.OnMapStatusChangeListener, CompoundButton.OnCheckedChangeListener {
|
|
|
private var fenceFillColor = 0
|
|
|
private var latlngTemp: String? = null
|
|
|
private var mTvArea: TextView? = null
|
|
@@ -83,7 +88,13 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
private var mFenceType: Int = 1
|
|
|
|
|
|
/////////////////locate related/////////////////
|
|
|
- private var map: AMap? = null
|
|
|
+ private var mMapView: MapView? = null
|
|
|
+ private var map: BaiduMap? = null
|
|
|
+ private lateinit var locationClient: LocationClient
|
|
|
+ private lateinit var autoCompleteTextView: AppCompatAutoCompleteTextView
|
|
|
+ private lateinit var adapter: ArrayAdapter<String>
|
|
|
+ private lateinit var suggestionSearch: SuggestionSearch
|
|
|
+ private val suggestions = mutableListOf<SuggestionResult.SuggestionInfo>()
|
|
|
var scaleView: MapScaleView? = null
|
|
|
|
|
|
///////////////////////////////////////////////
|
|
@@ -94,73 +105,37 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
private var mName: TextView? = null
|
|
|
private var mBack: ImageView? = null
|
|
|
private var mRightTv: TextView? = null
|
|
|
- //var mGoogleApiClient: GoogleApiClient? = null
|
|
|
var mLastLocation: Location? = null
|
|
|
private var needLocate = false
|
|
|
//internal lateinit var mLocationCallback: LocationCallback
|
|
|
internal lateinit var mLocationRequest: LocationRequest
|
|
|
//internal var mFusedLocationClient: FusedLocationProviderClient? = null
|
|
|
- private lateinit var geocodeSearch: GeocodeSearch
|
|
|
- private lateinit var locationClient: AMapLocationClient
|
|
|
- private lateinit var autoCompleteTextView: AutoCompleteTextView
|
|
|
- private val suggestions = mutableListOf<Tip>()
|
|
|
- private lateinit var adapter: ArrayAdapter<String>
|
|
|
private var radiusReserved: Int = 100
|
|
|
-
|
|
|
private fun initMap() {
|
|
|
- val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
|
|
|
-/* mapFragment!!.getMapAsync(this)
|
|
|
- mGoogleApiClient = GoogleApiClient.Builder(this)
|
|
|
- .addConnectionCallbacks(this)
|
|
|
- .addOnConnectionFailedListener(this)
|
|
|
- .addApi(LocationServices.API)
|
|
|
- .build()*/
|
|
|
- map = mapFragment?.getMap()
|
|
|
- map!!.uiSettings.isMyLocationButtonEnabled = false
|
|
|
-
|
|
|
- map!!.setOnPOIClickListener {
|
|
|
- map!!.moveCamera(CameraUpdateFactory.newLatLngZoom(it.coordinate, 15f))
|
|
|
- drawPolygon(it.coordinate, mDotsFenceInfoBean?.circleRadius ?: DEFAULT_RADIUS, mFenceType)
|
|
|
- getGeoAdress(it.coordinate)
|
|
|
- }
|
|
|
-
|
|
|
- map!!.setOnMapClickListener { latLng ->
|
|
|
- map!!.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f))
|
|
|
- drawPolygon(latLng, mDotsFenceInfoBean?.circleRadius ?: DEFAULT_RADIUS, mFenceType)
|
|
|
- getGeoAdress(latLng)
|
|
|
- }
|
|
|
- map!!.setOnCameraChangeListener(this)
|
|
|
- val cameraPosition = map!!.cameraPosition
|
|
|
+ mMapView = findViewById(R.id.bmapView)
|
|
|
+ map = mMapView?.map
|
|
|
+ map?.uiSettings?.isRotateGesturesEnabled = false
|
|
|
+ setMapClickListener(map, mDotsFenceInfoBean, mFenceType)
|
|
|
+ map?.setOnMapStatusChangeListener(this)
|
|
|
+ val cameraPosition = map!!.mapStatus
|
|
|
// need to pass zoom and latitude
|
|
|
scaleView!!.update(cameraPosition.zoom, cameraPosition.target.latitude)
|
|
|
initData()
|
|
|
refreshMapZoom()
|
|
|
}
|
|
|
|
|
|
- private fun initLocation() {
|
|
|
- locationClient = AMapLocationClient(this)
|
|
|
- val locationOption = AMapLocationClientOption()
|
|
|
- locationOption.apply {
|
|
|
- setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy)
|
|
|
- isNeedAddress = true
|
|
|
- interval = 2000
|
|
|
- isOnceLocation = true
|
|
|
- }
|
|
|
- locationClient.setLocationOption(locationOption)
|
|
|
- locationClient.setLocationListener { location ->
|
|
|
- location?.let {
|
|
|
- if (it.errorCode == 0) {
|
|
|
- // Positioning successfully, update map position
|
|
|
- val latLng = LatLng(it.latitude, it.longitude)
|
|
|
- map?.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f))
|
|
|
- Logger.d(TAG, "Positioning successfully: ${it.latitude} ${it.longitude}")
|
|
|
- } else {
|
|
|
- // Failed to positioning
|
|
|
- Logger.e(TAG, "Failed to positioning: ${it.errorInfo}")
|
|
|
- }
|
|
|
+ private fun setMapClickListener(map: BaiduMap?, fenceInfoBean: FenceInfoBean?, fenceType: Int) {
|
|
|
+ map?.setOnMapClickListener(object : BaiduMap.OnMapClickListener {
|
|
|
+ override fun onMapClick(latLng: LatLng) {
|
|
|
+ drawPolygon(latLng, fenceInfoBean?.circleRadius ?: DEFAULT_RADIUS, fenceType)
|
|
|
+ getGeoAdress(latLng)
|
|
|
}
|
|
|
- }
|
|
|
- locationClient.startLocation()
|
|
|
+
|
|
|
+ override fun onMapPoiClick(poi: MapPoi) {
|
|
|
+ drawPolygon(poi.position, fenceInfoBean?.circleRadius ?: DEFAULT_RADIUS, fenceType)
|
|
|
+ getGeoAdress(poi.position)
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
private fun initView() {
|
|
@@ -216,41 +191,43 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
}
|
|
|
|
|
|
fun initSearchLayout() {
|
|
|
-/* if (!Places.isInitialized()) {
|
|
|
- Places.initialize(applicationContext, getString(R.string.google_maps_key))
|
|
|
- }
|
|
|
- val autocompleteFragment =
|
|
|
- supportFragmentManager.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment?
|
|
|
- autocompleteFragment!!.setPlaceFields(mutableListOf(Place.Field.NAME, Place.Field.LAT_LNG))
|
|
|
- autocompleteFragment.setOnPlaceSelectedListener(object : PlaceSelectionListener {
|
|
|
- override fun onPlaceSelected(place: Place) {
|
|
|
- place.latLng?.let { positon ->
|
|
|
- drawPolygon(
|
|
|
- positon,
|
|
|
- mDotsFenceInfoBean?.circleRadius ?: DEFAULT_RADIUS,
|
|
|
- mFenceType
|
|
|
- )
|
|
|
- mDotsFenceInfoBean?.lat = positon.latitude
|
|
|
- mDotsFenceInfoBean?.lng = positon.longitude
|
|
|
- getGeoAdress(positon)
|
|
|
- animateMapStatus(positon)
|
|
|
- }
|
|
|
- mDotsFenceInfoBean?.fenceLocation = place.address
|
|
|
- }
|
|
|
-
|
|
|
- override fun onError(status: Status) {
|
|
|
- Log.i(TAG, "An error occurred: $status")
|
|
|
- }
|
|
|
- })*/
|
|
|
autoCompleteTextView = findViewById(R.id.autocomplete_input)
|
|
|
- adapter = ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, mutableListOf())
|
|
|
+ adapter = ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, mutableListOf<String>())
|
|
|
autoCompleteTextView.setAdapter(adapter)
|
|
|
|
|
|
+ suggestionSearch = SuggestionSearch.newInstance()
|
|
|
+ suggestionSearch.setOnGetSuggestionResultListener(object : OnGetSuggestionResultListener {
|
|
|
+ override fun onGetSuggestionResult(result: SuggestionResult?) {
|
|
|
+ if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
|
|
|
+ Logger.e(TAG, "搜索建议失败: ${result?.error}")
|
|
|
+ adapter.clear()
|
|
|
+ adapter.notifyDataSetChanged()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 更新建议列表
|
|
|
+ suggestions.clear()
|
|
|
+ suggestions.addAll(result.allSuggestions?.filter { it.pt != null } ?: emptyList())
|
|
|
+ /* adapter.clear()
|
|
|
+ adapter.addAll(suggestions.map { "${it.key} (${it.city}${it.district})" })
|
|
|
+ adapter.notifyDataSetChanged()
|
|
|
+ autoCompleteTextView.showDropDown()*/
|
|
|
+
|
|
|
+ val suggestionNames = suggestions.map { "${it.key} (${it.city}${it.district})" }
|
|
|
+ adapter = ArrayAdapter(this@FenceSettingActivity, android.R.layout.simple_dropdown_item_1line, suggestionNames)
|
|
|
+ autoCompleteTextView.setAdapter(adapter)
|
|
|
+ adapter.notifyDataSetChanged()
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
// 监听输入变化
|
|
|
autoCompleteTextView.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) {
|
|
|
- if (s.isNullOrEmpty()) return
|
|
|
+ if (s.isNullOrEmpty()) {
|
|
|
+ adapter.clear()
|
|
|
+ adapter.notifyDataSetChanged()
|
|
|
+ return
|
|
|
+ }
|
|
|
fetchSuggestions(s.toString())
|
|
|
}
|
|
|
override fun afterTextChanged(s: Editable?) {}
|
|
@@ -267,37 +244,21 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private fun fetchSuggestions(keyword: String) {
|
|
|
- // 配置 Inputtips 查询
|
|
|
- val query = InputtipsQuery(keyword, null) // 第二个参数为城市,可设为具体城市或 null
|
|
|
- query.cityLimit = false // 是否限制在指定城市
|
|
|
- val inputTips = Inputtips(this, query)
|
|
|
-
|
|
|
- // 设置监听器
|
|
|
- inputTips.setInputtipsListener { tipList, rCode ->
|
|
|
- if (rCode == 1000 && tipList != null) { // 1000 表示成功
|
|
|
- suggestions.clear()
|
|
|
- tipList.removeIf { tip -> tip == null || tip.point == null }
|
|
|
- suggestions.addAll(tipList)
|
|
|
- val suggestionNames = tipList.map { it.name }
|
|
|
- adapter.clear()
|
|
|
- adapter = ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, suggestionNames)
|
|
|
- autoCompleteTextView.setAdapter(adapter)
|
|
|
- adapter.notifyDataSetChanged()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 异步执行查询
|
|
|
- inputTips.requestInputtipsAsyn()
|
|
|
+ private fun fetchSuggestions(query: String) {
|
|
|
+ val option = SuggestionSearchOption()
|
|
|
+ .keyword(query)
|
|
|
+ .city("全国")
|
|
|
+ suggestionSearch.requestSuggestion(option)
|
|
|
}
|
|
|
|
|
|
- private fun onPlaceSelected(tip: Tip) {
|
|
|
- // 获取选择的地点信息
|
|
|
- val latLonPoint = tip.point
|
|
|
- val latLng = LatLng(latLonPoint.latitude, latLonPoint.longitude)
|
|
|
- map?.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f))
|
|
|
- drawPolygon(latLng, mDotsFenceInfoBean?.circleRadius ?: DEFAULT_RADIUS, mFenceType)
|
|
|
- getGeoAdress(latLng)
|
|
|
+ private fun onPlaceSelected(suggestion: SuggestionResult.SuggestionInfo) {
|
|
|
+ // 处理选择的地点,例如更新地图中心
|
|
|
+ suggestion.pt?.let { latLng ->
|
|
|
+ animateMapStatus(latLng)
|
|
|
+ drawPolygon(latLng, mDotsFenceInfoBean?.circleRadius ?: DEFAULT_RADIUS, mFenceType)
|
|
|
+ getGeoAdress(latLng)
|
|
|
+ }
|
|
|
+ Logger.d(TAG, "Selected place: ${suggestion.key}, ${suggestion.pt}")
|
|
|
}
|
|
|
|
|
|
override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
|
|
@@ -383,7 +344,8 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
val southWest = LatLng(minX, minY)
|
|
|
val earthEast = LatLng(maxX, maxY)
|
|
|
val bounds = LatLngBounds.Builder().include(southWest).include(earthEast).build()
|
|
|
- updateMap(bounds)
|
|
|
+ val mapStatusUpdate = MapStatusUpdateFactory.newLatLngBounds(bounds, 500, 500)
|
|
|
+ map?.animateMapStatus(mapStatusUpdate)
|
|
|
}
|
|
|
|
|
|
private val boundsPoint: ArrayList<*>
|
|
@@ -433,10 +395,6 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
return arrayList
|
|
|
}
|
|
|
|
|
|
- private fun updateMap(bounds: LatLngBounds) {
|
|
|
- //map!!.setMapStatusLimits(bounds)
|
|
|
- }
|
|
|
-
|
|
|
private fun getChildLocation(beanList: List<ChildInfo>?): LatLng? {
|
|
|
if (null == beanList || beanList.size < 1) {
|
|
|
Logger.d(TAG, "beanList is empty")
|
|
@@ -460,26 +418,25 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
|
|
|
private fun drawPolygon(center: LatLng?, radiu: Int, fenceType:Int) {
|
|
|
map!!.clear()
|
|
|
- Handler(Looper.getMainLooper()).postDelayed({
|
|
|
- mDotsFenceInfoBean?.lat = center?.latitude ?: 200.0
|
|
|
- mDotsFenceInfoBean?.lng = center?.longitude ?: 200.0
|
|
|
- val circle = map!!.addCircle(
|
|
|
- CircleOptions().center(center!!)
|
|
|
- .radius(radiu.toDouble())
|
|
|
- .strokeColor(fenceFillColor)
|
|
|
- .fillColor(fenceFillColor)
|
|
|
- )
|
|
|
-
|
|
|
- val view: View = if (fenceType == 1) {
|
|
|
- LayoutInflater.from(this).inflate(R.layout.fence_safe_layout, null)
|
|
|
- } else {
|
|
|
- LayoutInflater.from(this).inflate(R.layout.fence_danger_layout, null)
|
|
|
- }
|
|
|
- val descriptor = BitmapDescriptorFactory.fromBitmap(BitmapUtils.getBitmapFromView(view))
|
|
|
- val option = MarkerOptions().position(center).icon(descriptor)
|
|
|
- .zIndex(1f).draggable(false)
|
|
|
- val marker = map!!.addMarker(option)
|
|
|
- }, 200)
|
|
|
+ mDotsFenceInfoBean?.lat = center?.latitude ?: 200.0
|
|
|
+ mDotsFenceInfoBean?.lng = center?.longitude ?: 200.0
|
|
|
+
|
|
|
+ val circleOptions = CircleOptions()
|
|
|
+ .center(center)
|
|
|
+ .radius(radiu) // 百度地图的 radius 是 Int 类型
|
|
|
+ .stroke(Stroke(5, fenceFillColor)) // 描边宽度 5 像素
|
|
|
+ .fillColor(fenceFillColor)
|
|
|
+ val circle = map?.addOverlay(circleOptions)
|
|
|
+
|
|
|
+ val view: View = if (fenceType == 1) {
|
|
|
+ LayoutInflater.from(this).inflate(R.layout.fence_safe_layout, null)
|
|
|
+ } else {
|
|
|
+ LayoutInflater.from(this).inflate(R.layout.fence_danger_layout, null)
|
|
|
+ }
|
|
|
+ val descriptor = BitmapDescriptorFactory.fromBitmap(BitmapUtils.getBitmapFromView(view))
|
|
|
+ val option = MarkerOptions().position(center).icon(descriptor)
|
|
|
+ .zIndex(1).draggable(false)
|
|
|
+ val marker = map!!.addOverlay(option)
|
|
|
}
|
|
|
|
|
|
private fun drawPolygon() {
|
|
@@ -489,7 +446,6 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
mDotsFenceInfoBean!!.lng
|
|
|
)
|
|
|
animateMapStatus(tempLatLng)
|
|
|
- map?.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(mDotsFenceInfoBean!!.lat, mDotsFenceInfoBean!!.lng), 15f))
|
|
|
drawPolygon(
|
|
|
tempLatLng,
|
|
|
mDotsFenceInfoBean?.circleRadius ?: DEFAULT_RADIUS,
|
|
@@ -572,83 +528,77 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
if (tempLatLng?.latitude == 0.0 && tempLatLng.longitude == 0.0) {
|
|
|
initLocation()
|
|
|
} else {
|
|
|
- map?.moveCamera(CameraUpdateFactory.newLatLngZoom(tempLatLng, 15f))
|
|
|
- map?.addMarker(MarkerOptions().position(tempLatLng))
|
|
|
+ if (tempLatLng != null) {
|
|
|
+ animateMapStatus(tempLatLng)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/* private fun getGeoAdress(latLng: LatLng) {
|
|
|
- val latlngStr = latLng.latitude.toString() + "," + latLng.longitude
|
|
|
- Logger.d(TAG, "getGeoAdress -- $latlngStr")
|
|
|
- if (latlngStr != null && latlngStr == latlngTemp) {
|
|
|
- return
|
|
|
+ private fun initLocation() {
|
|
|
+ LocationClient.setAgreePrivacy(true)
|
|
|
+ locationClient = LocationClient(this)
|
|
|
+ val locationOption = LocationClientOption()
|
|
|
+ locationOption.apply {
|
|
|
+ setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy)
|
|
|
+ setIsNeedAddress(true)
|
|
|
+ scanSpan = 2000
|
|
|
+ isOnceLocation = true
|
|
|
}
|
|
|
- // UrlConstants.initTrustSSL();
|
|
|
- val request = StringRequest(
|
|
|
- UrlConstants.GOOGLE_GEO_ADRESS + "?latlng=" + latlngStr + getString(R.string.param_google_maps_key),
|
|
|
- { response ->
|
|
|
- try {
|
|
|
- val jsonObject = JSONObject(response)
|
|
|
- val status = jsonObject.getString("status")
|
|
|
- if ("OK" == status) {
|
|
|
- latlngTemp = latlngStr
|
|
|
- val results = jsonObject.getString("results")
|
|
|
- val jsonArray = JSONArray(results)
|
|
|
- val length = jsonArray.length()
|
|
|
- Logger.d(TAG, "getGeoAdress -- jsonArray.length(): $length")
|
|
|
- for (i in 0 until length) {
|
|
|
- val jsonObject2 = jsonArray.getJSONObject(i)
|
|
|
- Logger.d(TAG, "jsonObject2 - $i : $jsonObject2")
|
|
|
- val formatted_address = jsonObject2.getString("formatted_address")
|
|
|
- Logger.i(TAG, "getGeoAdress -- formatted_address: $formatted_address")
|
|
|
- if (i == 0) {
|
|
|
- mDotsFenceInfoBean?.fenceLocation = formatted_address
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (e: JSONException) {
|
|
|
- e.printStackTrace()
|
|
|
+ locationClient.locOption = locationOption
|
|
|
+ locationClient.registerLocationListener(object : BDAbstractLocationListener() {
|
|
|
+ override fun onReceiveLocation(location: BDLocation?) {
|
|
|
+ location?.let {
|
|
|
+ // Positioning successfully, update map position
|
|
|
+ Logger.d(TAG, "Positioning successfully, ${it.latitude}, ${it.longitude}")
|
|
|
+ val latLng = LatLng(it.latitude, it.longitude)
|
|
|
+ animateMapStatus(latLng)
|
|
|
+ SharedPreferenceUtil.setFloatDataIntoSP(Constant.PACKAGE_NAME, Constant.KEY_LOCATION_LAT, it.latitude.toFloat())
|
|
|
+ SharedPreferenceUtil.setFloatDataIntoSP(Constant.PACKAGE_NAME, Constant.KEY_LOCATION_LNG, it.longitude.toFloat())
|
|
|
+ }?: run {
|
|
|
+ // Failed to positioning
|
|
|
+ Logger.e(TAG, "Failed to positioning")
|
|
|
}
|
|
|
- }) { error ->
|
|
|
- Logger.i(TAG, "add err==" + error.message)
|
|
|
- /* ToastUtils.showLongToast(R.string.network_exception) */
|
|
|
- }
|
|
|
- RequestManager.getInstance(this).addToRequestQueue(request)
|
|
|
- }*/
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
private fun getGeoAdress(latLng: LatLng) {
|
|
|
- val latlngStr = latLng.latitude.toString() + "," + latLng.longitude
|
|
|
- Logger.d(TAG, "getAddressFromLocation -- $latlngStr")
|
|
|
- if (latlngStr != null && latlngStr == latlngTemp) {
|
|
|
- return
|
|
|
- }
|
|
|
- Logger.d(TAG, "getAddressFromLocation")
|
|
|
- geocodeSearch = GeocodeSearch(applicationContext)
|
|
|
- geocodeSearch?.setOnGeocodeSearchListener(this)
|
|
|
- val latLonPoint = latLng.latitude.toString()?.let {
|
|
|
- latLng.longitude.toString()?.let { it1 ->
|
|
|
- LatLonPoint(
|
|
|
- it.toDouble(), it1.toDouble())
|
|
|
+ val geoCoder = GeoCoder.newInstance()
|
|
|
+ geoCoder.setOnGetGeoCodeResultListener(object : OnGetGeoCoderResultListener {
|
|
|
+ override fun onGetGeoCodeResult(result: GeoCodeResult?) {
|
|
|
+ if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
|
|
|
+ Logger.d(TAG, "地理编码失败: ${result?.error}")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ val latLng = result.location
|
|
|
+ val address = result.address
|
|
|
+ Logger.d(TAG, "地理编码结果: 地址=$address, 经纬度=$latLng")
|
|
|
}
|
|
|
- }
|
|
|
- latlngTemp = latlngStr
|
|
|
- val query = RegeocodeQuery(latLonPoint, 200f, GeocodeSearch.AMAP)
|
|
|
- geocodeSearch?.getFromLocationAsyn(query)
|
|
|
- }
|
|
|
|
|
|
- override fun onRegeocodeSearched(result: RegeocodeResult?, rCode: Int) {
|
|
|
- Logger.d(TAG, "onRegeocodeSearched")
|
|
|
- if (rCode == 1000 && result != null) {
|
|
|
- Logger.d(TAG, "onRegeocodeSearched successfully")
|
|
|
- val address = result.regeocodeAddress
|
|
|
- mDotsFenceInfoBean?.fenceLocation = address?.formatAddress
|
|
|
- }
|
|
|
- }
|
|
|
+ override fun onGetReverseGeoCodeResult(result: ReverseGeoCodeResult?) {
|
|
|
+ if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
|
|
|
+ Logger.e(TAG, "逆地理编码失败: ${result?.error}")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 获取逆地理编码结果
|
|
|
+ val address = result.address
|
|
|
+ val poiList = result.poiList
|
|
|
+ Logger.d(TAG, "逆地理编码结果: 地址=$address, 格式化地址=${result.formattedPoiAddress}, 附近POI=$poiList")
|
|
|
+ if (result.error == SearchResult.ERRORNO.NO_ERROR) {
|
|
|
+ mDotsFenceInfoBean?.fenceLocation = result.formattedPoiAddress
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
|
|
|
- override fun onGeocodeSearched(result: GeocodeResult?, rCode: Int) {
|
|
|
- // This method is not used for reverse geocoding
|
|
|
+ // 发起逆地理编码请求
|
|
|
+ latLng?.let {
|
|
|
+ val reverseGeoCodeOption = ReverseGeoCodeOption()
|
|
|
+ .location(it)
|
|
|
+ .radius(200)
|
|
|
+ .newVersion(1)
|
|
|
+ geoCoder.reverseGeoCode(reverseGeoCodeOption)
|
|
|
+ } ?: Logger.e(TAG, "无效的经纬度数据")
|
|
|
}
|
|
|
|
|
|
override fun onDrawPolygon(pointList: List<Point>) {
|
|
@@ -658,17 +608,6 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
mDrawFenceLayout!!.setShouldIntercept(false)
|
|
|
}
|
|
|
|
|
|
- override fun onStart() {
|
|
|
- //startToLocate();
|
|
|
- //mGoogleApiClient!!.connect()
|
|
|
- super.onStart()
|
|
|
- }
|
|
|
-
|
|
|
- override fun onStop() {
|
|
|
- //mGoogleApiClient!!.disconnect()
|
|
|
- super.onStop()
|
|
|
- }
|
|
|
-
|
|
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
//getMenuInflater().inflate(R.menu.menu_fence_setting, menu);
|
|
@@ -676,25 +615,25 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
- override fun onCameraChange(cameraPosition: CameraPosition) {
|
|
|
- scaleView!!.update(cameraPosition.zoom, cameraPosition.target.latitude)
|
|
|
+ override fun onMapStatusChangeStart(p0: MapStatus?) {
|
|
|
+ Logger.d(TAG, "onMapStatusChangeStart latitude:${p0!!.target.latitude}")
|
|
|
+ scaleView?.update(p0!!.zoom, p0.target.latitude)
|
|
|
}
|
|
|
|
|
|
- override fun onCameraChangeFinish(cameraPosition: CameraPosition) {
|
|
|
- scaleView!!.update(cameraPosition.zoom, cameraPosition.target.latitude)
|
|
|
+ override fun onMapStatusChangeStart(p0: MapStatus?, p1: Int) {
|
|
|
+ Logger.d(TAG, "onMapStatusChangeStart(p0: MapStatus?, p1: Int) latitude:${p0!!.target.latitude}")
|
|
|
+ scaleView?.update(p0!!.zoom, p0.target.latitude)
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
- override fun onCameraIdle() {
|
|
|
- val cameraPosition = map!!.cameraPosition
|
|
|
- scaleView!!.update(cameraPosition.zoom, cameraPosition.target.latitude)
|
|
|
+ override fun onMapStatusChange(p0: MapStatus?) {
|
|
|
+ Logger.d(TAG, "onMapStatusChange latitude:${p0!!.target.latitude}")
|
|
|
+ scaleView?.update(p0!!.zoom, p0.target.latitude)
|
|
|
}
|
|
|
|
|
|
- override fun onCameraMove() {
|
|
|
- val cameraPosition = map!!.cameraPosition
|
|
|
- scaleView!!.update(cameraPosition.zoom, cameraPosition.target.latitude)
|
|
|
+ override fun onMapStatusChangeFinish(p0: MapStatus?) {
|
|
|
+ Logger.d(TAG, "onMapStatusChangeFinish latitude:${p0!!.target.latitude}")
|
|
|
+ scaleView?.update(p0!!.zoom, p0.target.latitude)
|
|
|
}
|
|
|
-*/
|
|
|
|
|
|
/**
|
|
|
* Enables the My Location layer if the fine location permission has been granted.
|
|
@@ -716,50 +655,49 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
|
|
|
private fun animateMapStatus(latLng: LatLng) {
|
|
|
Logger.d(TAG, "animateMapStatus")
|
|
|
- val position = CameraPosition.Builder()
|
|
|
- .target(latLng).zoom(15.0f).bearing(0f).tilt(0f).build()
|
|
|
- map!!.animateCamera(CameraUpdateFactory.newCameraPosition(position))
|
|
|
- }
|
|
|
-
|
|
|
-/* override fun onConnected(bundle: Bundle?) {
|
|
|
- if (ActivityCompat.checkSelfPermission(
|
|
|
- this,
|
|
|
- Manifest.permission.ACCESS_FINE_LOCATION
|
|
|
- ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
|
|
|
- this,
|
|
|
- Manifest.permission.ACCESS_COARSE_LOCATION
|
|
|
- ) != PackageManager.PERMISSION_GRANTED
|
|
|
- ) {
|
|
|
- // TODO: Consider calling
|
|
|
- // ActivityCompat#requestPermissions
|
|
|
- // here to request the missing permissions, and then overriding
|
|
|
- // public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
- // int[] grantResults)
|
|
|
- // to handle the case where the user grants the permission. See the documentation
|
|
|
- // for ActivityCompat#requestPermissions for more details.
|
|
|
- return
|
|
|
- }
|
|
|
- LocationServices.getFusedLocationProviderClient(this).lastLocation.addOnSuccessListener {
|
|
|
- mLastLocation = it
|
|
|
- if (needLocate) {
|
|
|
- startToLocate()
|
|
|
+ val update = MapStatusUpdateFactory.newLatLngZoom(latLng, 15f)
|
|
|
+ map?.setMapStatus(update)
|
|
|
+ map?.animateMapStatus(update)
|
|
|
+ }
|
|
|
+
|
|
|
+ /* override fun onConnected(bundle: Bundle?) {
|
|
|
+ if (ActivityCompat.checkSelfPermission(
|
|
|
+ this,
|
|
|
+ Manifest.permission.ACCESS_FINE_LOCATION
|
|
|
+ ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
|
|
|
+ this,
|
|
|
+ Manifest.permission.ACCESS_COARSE_LOCATION
|
|
|
+ ) != PackageManager.PERMISSION_GRANTED
|
|
|
+ ) {
|
|
|
+ // TODO: Consider calling
|
|
|
+ // ActivityCompat#requestPermissions
|
|
|
+ // here to request the missing permissions, and then overriding
|
|
|
+ // public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
+ // int[] grantResults)
|
|
|
+ // to handle the case where the user grants the permission. See the documentation
|
|
|
+ // for ActivityCompat#requestPermissions for more details.
|
|
|
+ return
|
|
|
+ }
|
|
|
+ LocationServices.getFusedLocationProviderClient(this).lastLocation.addOnSuccessListener {
|
|
|
+ mLastLocation = it
|
|
|
+ if (needLocate) {
|
|
|
+ startToLocate()
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- override fun onConnectionSuspended(i: Int) {}
|
|
|
- override fun onConnectionFailed(connectionResult: ConnectionResult) {
|
|
|
- Logger.e(
|
|
|
- TAG, "onConnectionFailed: ConnectionResult.getErrorCode() = "
|
|
|
- + connectionResult.errorCode
|
|
|
- )
|
|
|
- Toast.makeText(
|
|
|
- this,
|
|
|
- "Could not connect to Google API Client: Error " + connectionResult.errorCode,
|
|
|
- Toast.LENGTH_SHORT
|
|
|
- ).show()
|
|
|
- }
|
|
|
-*/
|
|
|
+ override fun onConnectionSuspended(i: Int) {}
|
|
|
+ override fun onConnectionFailed(connectionResult: ConnectionResult) {
|
|
|
+ Logger.e(
|
|
|
+ TAG, "onConnectionFailed: ConnectionResult.getErrorCode() = "
|
|
|
+ + connectionResult.errorCode
|
|
|
+ )
|
|
|
+ Toast.makeText(
|
|
|
+ this,
|
|
|
+ "Could not connect to Google API Client: Error " + connectionResult.errorCode,
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ }*/
|
|
|
|
|
|
companion object {
|
|
|
private val TAG = FenceSettingActivity::class.java.simpleName
|
|
@@ -772,39 +710,39 @@ class FenceSettingActivity : BaseNoActionBarActivity(), View.OnClickListener, On
|
|
|
}
|
|
|
|
|
|
private fun requestSelfLocation() {
|
|
|
-/* mLocationRequest = LocationRequest()
|
|
|
- mLocationRequest.interval = 1000
|
|
|
- mLocationRequest.fastestInterval = 1000
|
|
|
- mLocationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
|
|
|
- if (ContextCompat.checkSelfPermission(
|
|
|
- this,
|
|
|
- Manifest.permission.ACCESS_FINE_LOCATION
|
|
|
- ) == PackageManager.PERMISSION_GRANTED
|
|
|
- ) {
|
|
|
- mLocationCallback = object : LocationCallback() {
|
|
|
- override fun onLocationResult(result: LocationResult) {
|
|
|
- super.onLocationResult(result)
|
|
|
- val place = result.lastLocation ?: return
|
|
|
- val positon = LatLng(place.latitude, place.longitude)
|
|
|
- mDotsFenceInfoBean?.circleRadius = DEFAULT_RADIUS
|
|
|
- mSeekbar?.progress = DEFAULT_RADIUS / SEEKBAR_STEP
|
|
|
- mTvArea?.text = DEFAULT_RADIUS.toString()
|
|
|
- drawPolygon(positon, DEFAULT_RADIUS, mFenceType)
|
|
|
- mDotsFenceInfoBean?.lat = positon.latitude
|
|
|
- mDotsFenceInfoBean?.lng = positon.longitude
|
|
|
- getGeoAdress(positon)
|
|
|
- animateMapStatus(positon)
|
|
|
- if (mGoogleApiClient != null) {
|
|
|
- mFusedLocationClient?.removeLocationUpdates(mLocationCallback)
|
|
|
+ /* mLocationRequest = LocationRequest()
|
|
|
+ mLocationRequest.interval = 1000
|
|
|
+ mLocationRequest.fastestInterval = 1000
|
|
|
+ mLocationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
|
|
|
+ if (ContextCompat.checkSelfPermission(
|
|
|
+ this,
|
|
|
+ Manifest.permission.ACCESS_FINE_LOCATION
|
|
|
+ ) == PackageManager.PERMISSION_GRANTED
|
|
|
+ ) {
|
|
|
+ mLocationCallback = object : LocationCallback() {
|
|
|
+ override fun onLocationResult(result: LocationResult) {
|
|
|
+ super.onLocationResult(result)
|
|
|
+ val place = result.lastLocation ?: return
|
|
|
+ val positon = LatLng(place.latitude, place.longitude)
|
|
|
+ mDotsFenceInfoBean?.circleRadius = DEFAULT_RADIUS
|
|
|
+ mSeekbar?.progress = DEFAULT_RADIUS / SEEKBAR_STEP
|
|
|
+ mTvArea?.text = DEFAULT_RADIUS.toString()
|
|
|
+ drawPolygon(positon, DEFAULT_RADIUS, mFenceType)
|
|
|
+ mDotsFenceInfoBean?.lat = positon.latitude
|
|
|
+ mDotsFenceInfoBean?.lng = positon.longitude
|
|
|
+ getGeoAdress(positon)
|
|
|
+ animateMapStatus(positon)
|
|
|
+ if (mGoogleApiClient != null) {
|
|
|
+ mFusedLocationClient?.removeLocationUpdates(mLocationCallback)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
|
|
|
- mFusedLocationClient?.requestLocationUpdates(
|
|
|
- mLocationRequest,
|
|
|
- mLocationCallback,
|
|
|
- Looper.myLooper()
|
|
|
- )
|
|
|
- }*/
|
|
|
+ mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
|
|
|
+ mFusedLocationClient?.requestLocationUpdates(
|
|
|
+ mLocationRequest,
|
|
|
+ mLocationCallback,
|
|
|
+ Looper.myLooper()
|
|
|
+ )
|
|
|
+ }*/
|
|
|
}
|
|
|
-}
|
|
|
+}
|