package com.fuban.driver.ui.main.add_order
|
|
import android.content.Context
|
import android.os.Bundle
|
import cn.sinata.xldutils.utils.clickDelay
|
import com.amap.api.fence.GeoFence
|
import com.amap.api.fence.GeoFenceListener
|
import com.amap.api.maps.AMap
|
import com.amap.api.maps.model.LatLng
|
import com.amap.api.maps.model.Marker
|
import com.amap.api.services.core.LatLonPoint
|
import com.amap.api.services.core.PoiItem
|
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.poisearch.PoiResult
|
import com.amap.api.services.poisearch.PoiSearch
|
import com.fuban.driver.R
|
import com.fuban.driver.base.BaseEvent
|
import com.fuban.driver.base.MyBaseActivity
|
import com.fuban.driver.base.gaode.AMapKit
|
import com.fuban.driver.bean.DistrictQueryBean
|
import com.fuban.driver.bean.QueryPositionBean
|
import com.fuban.driver.netUtls.Api
|
import com.fuban.driver.netUtls.callNet
|
import com.fuban.driver.netUtls.createView
|
import com.fuban.driver.netUtls.getMapByAny
|
import kotlinx.android.synthetic.main.activity_select_start_point.*
|
import org.greenrobot.eventbus.EventBus
|
import org.jetbrains.anko.startActivity
|
import org.jetbrains.anko.toast
|
|
/**
|
* @ClassName SelectStartPointActivity
|
* @Description 选择起点 选择终点
|
* @Author Administrator
|
* @Date 2020/10/8 16:00
|
* @Version 1.0
|
*/
|
class SelectStartPointActivity : MyBaseActivity() {
|
|
val endCityId by lazy {
|
intent.getStringExtra("endCityId")
|
}
|
|
val startCityId by lazy {
|
intent.getStringExtra("startCityId")
|
}
|
|
val name by lazy {
|
intent.getStringExtra("name")
|
}
|
|
|
var map: AMap? = null
|
|
var marker: Marker? = null
|
|
var currentPosition: LatLng? = null
|
|
var currentSiteStr: String = ""
|
|
override fun setContentView() {
|
setContentView(R.layout.activity_select_start_point)
|
}
|
|
override fun initView() {
|
if (!startCityId.isNullOrEmpty()) {
|
setTitleText("选择起点")
|
} else {
|
setTitleText("选择终点")
|
}
|
tv_city_name.text = name
|
callDistrict()
|
|
tv_sure.clickDelay {
|
if (currentPosition == null){
|
toast("加载中请稍等...")
|
return@clickDelay
|
}
|
AMapKit.getAddress(
|
this,
|
currentPosition!!.latitude,
|
currentPosition!!.longitude
|
) { adcode ->
|
if (adcode.isNullOrEmpty()){
|
toast("查询失败,请重试")
|
return@getAddress
|
}
|
var map = getMapByAny()
|
map["code"] = adcode
|
map["lonLat"] = currentPosition!!.longitude.toString() + "," + currentPosition!!.latitude.toString()
|
map["siteId"] = if (startCityId.isNullOrEmpty()) endCityId else startCityId
|
callNet(Api.areaMonitoring, map) {
|
var bean = gson.fromJson<QueryPositionBean>(it,QueryPositionBean::class.java)
|
if (bean.data == -1){
|
toast("您选择的地址不在服务范围内")
|
return@callNet
|
}else{
|
currentPosition?.let {
|
EventBus.getDefault().post(
|
BaseEvent(
|
BaseEvent.SELECT_POINT_SITE_DETAIL,
|
currentSiteStr,
|
currentPosition!!.latitude.toString(),
|
it.longitude.toString()
|
)
|
)
|
onBackPressed()
|
}
|
}
|
|
}
|
}
|
}
|
}
|
|
private fun moveLisener() {
|
AMapKit.addMapMoveListener(map!!, marker!!) { position ->
|
// AMapKit.getAddressReg(this,position.latitude,position.longitude){
|
// if (!it.aois.isNullOrEmpty()){
|
// currentPosition = position
|
// if(!isSelectFirst){
|
// tv_site.text = it.district
|
// tv_site_detail.text = it.aois[0].aoiName
|
// }else{
|
// isSelectFirst = false
|
// }
|
// currentSiteStr = tv_site.text.toString() + tv_site_detail.text.toString()
|
// dismissDialog()
|
// }
|
// }
|
AMapKit.searchPoint(
|
this,
|
position.latitude,
|
position.longitude,
|
object : PoiSearch.OnPoiSearchListener {
|
override fun onPoiItemSearched(p0: PoiItem?, p1: Int) {
|
|
}
|
|
override fun onPoiSearched(p0: PoiResult?, p1: Int) {
|
if (p0?.pois!!.size > 0) {
|
currentPosition = LatLng(
|
p0.pois[0].latLonPoint.latitude,
|
p0.pois[0].latLonPoint.longitude
|
)
|
if(!isSelectFirst){
|
tv_site.text = p0.pois[0].adName
|
tv_site_detail.text = p0.pois[0].title
|
}else{
|
isSelectFirst = false
|
}
|
currentSiteStr = tv_site.text.toString() + tv_site_detail.text.toString()
|
dismissDialog()
|
}
|
}
|
|
})
|
}
|
}
|
|
private fun callDistrict() {
|
var map = getMapByAny()
|
map["siteId"] = if (startCityId.isNullOrEmpty()) endCityId else startCityId
|
callNet(Api.queryLocation, map) {
|
var bean = gson.fromJson<DistrictQueryBean>(it, DistrictQueryBean::class.java)
|
for (item in bean.data) {
|
if (item.type == 1) { // integer($int32) 区域类型(1=行政区域,2=电子围栏)
|
var contentStr = ""
|
if (item.province.isNotEmpty()) {
|
contentStr = item.province
|
}
|
if (item.city.isNotEmpty()) {
|
contentStr = item.city
|
}
|
if (item.district.isNotEmpty()) {
|
contentStr = item.district
|
}
|
searchName(contentStr)
|
} else {
|
addPointList(item.coordinate)
|
}
|
}
|
}
|
}
|
|
private fun addPointList(coordinate: String) {
|
var latAll = coordinate.split(";")
|
if (latAll.size > 2){
|
var allLat = mutableListOf<LatLng>()
|
for (item in latAll) {
|
var point = item.split(",")
|
if (point.size == 2) {
|
var lat = LatLng(point[1].toDouble(), point[0].toDouble())
|
allLat.add(lat)
|
}
|
}
|
AMapKit.drawFace(this, map!!,allLat)
|
if (allLat.size > 0){
|
addMk(LatLng(allLat[0].latitude,allLat[0].longitude))
|
marker?.let {
|
it.position = LatLng(allLat[0].latitude,allLat[0].longitude)
|
AMapKit.moveCamera(map!!,it.position)
|
}
|
}
|
// AMapKit.drawFenceByPoint(this, allPoint, GeoFenceListener { p0, p1, p2 ->
|
// if (p1 == GeoFence.ADDGEOFENCE_SUCCESS) {
|
// for (item in p0) {
|
// for (itemP in item.pointList) {
|
// }
|
// }
|
// }
|
// })
|
}
|
if (latAll.size == 2){
|
var point = latAll[0].split(",")
|
var dpoint = LatLng(point[1].toDouble(), point[0].toDouble())
|
AMapKit.drawCricle(this,map!!,dpoint,latAll[1].toDouble())
|
AMapKit.moveCamera(map!!,dpoint)
|
addMk(LatLng(dpoint.latitude,dpoint.longitude))
|
marker?.let {
|
it.position = LatLng(dpoint.latitude,dpoint.longitude)
|
AMapKit.moveCamera(map!!,it.position)
|
}
|
}
|
|
}
|
|
fun searchName(str: String) {
|
AMapKit.drawFenceByCity(this, str, GeoFenceListener { p0, p1, p2 ->
|
if (p1 == GeoFence.ADDGEOFENCE_SUCCESS) {
|
for (item in p0) {
|
for (itemP in item.pointList) {
|
AMapKit.drawFace(this, map!!, AMapKit.LatByDpoint(itemP))
|
if (itemP.size > 0){
|
addMk(LatLng(itemP[0].latitude,itemP[0].longitude))
|
AMapKit.moveCamera(map!!,LatLng(itemP[0].latitude,itemP[0].longitude))
|
}
|
}
|
}
|
}
|
})
|
}
|
|
fun addFirstMarket() {
|
}
|
|
fun addMk(lat:LatLng){
|
if (marker == null){
|
var view = createView(R.layout.item_map_point, this)
|
marker = AMapKit.addMarker(map!!, lat.latitude, lat.longitude, view, "12")
|
currentPosition = marker?.position
|
moveLisener()
|
}
|
}
|
|
/***
|
* 逆地址编码
|
*/
|
fun getAddress(context: Context, lat:Double, lon:Double, function:(String)->Unit) {
|
var geocoderSearch = GeocodeSearch(context);
|
// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
|
var query = RegeocodeQuery(LatLonPoint(lat,lon), 500f, GeocodeSearch.AMAP);
|
geocoderSearch.getFromLocationAsyn(query);
|
geocoderSearch.setOnGeocodeSearchListener(object : GeocodeSearch.OnGeocodeSearchListener {
|
override fun onRegeocodeSearched(p0: RegeocodeResult?, p1: Int) {
|
p0?.let {
|
if (p1 != 1000){
|
function("")
|
}else{
|
function(it.regeocodeAddress.city)
|
}
|
}
|
}
|
|
override fun onGeocodeSearched(p0: GeocodeResult?, p1: Int) {
|
|
}
|
|
})
|
}
|
|
|
override fun setOnclick() {
|
iv_reset_map.clickDelay {
|
AMapKit.moveCamera(map!!, currentPosition)
|
}
|
|
tv_search.setOnClickListener {
|
startActivity<SearchSiteActivity>("currentCityName" to tv_city_name.text.toString(),"type" to if (!startCityId.isNullOrEmpty()) "1" else "2")
|
}
|
}
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
super.onCreate(savedInstanceState)
|
map = AMapKit.initMap(savedInstanceState, map_view)
|
}
|
|
override fun onEventMainThread(event: BaseEvent?) {
|
super.onEventMainThread(event)
|
when (event!!.code) {
|
BaseEvent.SELECT_SEARCH_SITE -> {
|
isSelectFirst = true
|
var list = event!!.msgthree.split(",")
|
if (list.size == 2){
|
tv_site.text = list[0]
|
tv_site_detail.text = list[1]
|
}
|
showDialog()
|
AMapKit.moveCamera(map!!, LatLng(event.msg.toDouble(), event.msgTwo.toDouble()))
|
}
|
}
|
}
|
|
var isSelectFirst = false
|
|
}
|