1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package cn.flightfeather.thirdappmodule.module.common
 
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.view.View
import cn.flightfeather.thirdappmodule.R
import cn.flightfeather.thirdappmodule.module.base.BaseMapActivity
import com.amap.api.maps.AMap.OnCameraChangeListener
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.MapView
import com.amap.api.maps.model.*
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 kotlinx.android.parcel.Parcelize
import kotlinx.android.synthetic.main.activity_map_location.*
import kotlinx.android.synthetic.main.layout_button_back.*
 
/**
 * @author riku
 * Date: 2020/6/9
 * 获取地图定位信息activity
 */
class MapLocationActivity : BaseMapActivity(), View.OnClickListener {
 
    companion object {
        const val RESULT_CODE_LOCATION = "location"
    }
 
    private lateinit var geocodeSearch: GeocodeSearch
 
    private val location = Location()
 
    private var firstMove = true
 
    override fun onCreate(savedInstanceState: Bundle?) {
        geocodeSearch = GeocodeSearch(this)
        super.onCreate(savedInstanceState)
        img_back.setOnClickListener(this)
        txt_submit.setOnClickListener(this)
        initLocation()
    }
 
    override fun onResume() {
        super.onResume()
        aMap.isMyLocationEnabled = true
    }
 
    override fun onPause() {
        super.onPause()
        aMap.isMyLocationEnabled = false
    }
 
    override fun onDestroy() {
        super.onDestroy()
        aMap.isMyLocationEnabled = false
    }
 
    override fun onMapInitStart() {
        //初始化详细地址描述查询功能
        geocodeSearch.setOnGeocodeSearchListener(object : GeocodeSearch.OnGeocodeSearchListener {
            @SuppressLint("SetTextI18n")
            override fun onRegeocodeSearched(p0: RegeocodeResult?, p1: Int) {
                //成功
                if (p1 == 1000) {
                    p0?.let {
                        it.regeocodeAddress.apply {
                            location.address = formatAddress
 
                            location.address = location.address.replace(township, "")
                            if (aois.isNotEmpty()) {
                                location.name = aois[0].aoiName
                                location.address = location.address.replace(location.name, "")
                            }
 
                            val part1 = if (province==city) province else province + city
                            part1+district
 
                            runOnUiThread {
                                txt_location_content.text = location.address
                                txt_latitude_content.text = "${location.latitude},  ${location.longitude}"
                            }
                        }
                    }
                }
            }
 
            override fun onGeocodeSearched(p0: GeocodeResult?, p1: Int) {
                p0?.geocodeQuery
            }
        })
    }
 
    override fun onMarkerClick(marker: Marker) = Unit
 
    override fun getMapView(): MapView = mapView
 
    override fun getLayoutId(): Int = R.layout.activity_map_location
 
    override fun initMap() {
        aMap.setRenderFps(60)
        aMap.uiSettings.isZoomControlsEnabled = false
        aMap.uiSettings.isScaleControlsEnabled = true
        aMap.setOnCameraChangeListener(object : OnCameraChangeListener {
            override fun onCameraChange(cameraPosition: CameraPosition) {
 
            }
 
            override fun onCameraChangeFinish(cameraPosition: CameraPosition) {
                location.clear()
                location.latitude = cameraPosition.target.latitude
                location.longitude = cameraPosition.target.longitude
 
                val query = RegeocodeQuery(LatLonPoint(location.latitude, location.longitude), 200F, GeocodeSearch.AMAP)
                geocodeSearch.getFromLocationAsyn(query)
            }
        })
        aMap.setOnMyLocationChangeListener {
            if (firstMove) {
                val desLatLng = LatLng(it.latitude, it.longitude)
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(desLatLng, 15f))
                firstMove = false
            }
        }
        aMap.setOnMapLoadedListener { addMarkerInScreenCenter() }
 
    }
 
    override fun initLocation() {
        val myLocationStyle: MyLocationStyle = MyLocationStyle() //初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。
 
        //   myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。
        //   myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。
        myLocationStyle.showMyLocation(true)
        myLocationStyle.radiusFillColor(resources.getColor(R.color.transparent))
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER) //连续定位、蓝点不会移动到地图中心点,定位点依照设备方向旋转,并且蓝点会跟随设备移动
 
        aMap.myLocationStyle = myLocationStyle //设置定位蓝点的Style
 
        aMap.uiSettings.isMyLocationButtonEnabled = false //设置默认定位按钮是否显示,非必需设置。
 
        aMap.isMyLocationEnabled = true // 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。
 
    }
 
    //在屏幕中心添加一个固定的marker
    private fun addMarkerInScreenCenter() {
        val latLng = aMap.cameraPosition.target
        val screenPosition = aMap.projection.toScreenLocation(latLng)
        val locationMarker = aMap.addMarker(MarkerOptions()
                .anchor(0.5f, 0.5f)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.purple_pin)))
        //设置Marker在屏幕上,不跟随地图移动
        locationMarker.setPositionByPixels(screenPosition.x, screenPosition.y)
        locationMarker.zIndex = 1f
    }
 
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.img_back -> onBackPressed()
            R.id.txt_submit -> {
                val intent = Intent()
                intent.putExtra(RESULT_CODE_LOCATION, location)
                setResult(RESULT_OK, intent)
                onBackPressed()
            }
        }
    }
 
    @Parcelize
    data class Location(
            var name: String = "",
            var address: String = "",
            var latitude: Double = 0.0,
            var longitude: Double = 0.0
    ) : Parcelable {
        fun clear() {
            name = ""
            address = ""
            latitude = 0.0
            longitude = 0.0
        }
    }
}