|
|
@@ -0,0 +1,142 @@
|
|
|
+package com.i2edu.amap_location
|
|
|
+
|
|
|
+import android.content.Context
|
|
|
+import android.graphics.Paint
|
|
|
+import android.location.Location
|
|
|
+import android.view.LayoutInflater
|
|
|
+import android.view.View
|
|
|
+import android.view.ViewGroup
|
|
|
+import android.widget.FrameLayout
|
|
|
+import android.widget.ImageView
|
|
|
+import android.widget.TextView
|
|
|
+import com.amap.api.maps.AMap
|
|
|
+import com.amap.api.maps.CameraUpdateFactory
|
|
|
+import com.amap.api.maps.MapView
|
|
|
+import com.amap.api.maps.model.*
|
|
|
+import io.flutter.plugin.common.MethodChannel
|
|
|
+import io.flutter.plugin.platform.PlatformView
|
|
|
+import java.util.*
|
|
|
+
|
|
|
+
|
|
|
+class AMapView(private val context: Context, private val channel: MethodChannel) : PlatformView,
|
|
|
+ AMap.OnMyLocationChangeListener, AMap.InfoWindowAdapter {
|
|
|
+ private val TAG = "AMapView"
|
|
|
+ var frameLayout: FrameLayout? = null
|
|
|
+ var mapView: MapView? = null
|
|
|
+ var aMap: AMap? = null
|
|
|
+ var infoWindow: View? = null
|
|
|
+ var mMarkers: List<Map<String, Any>>? = null
|
|
|
+ private var firstMove: Boolean = false
|
|
|
+
|
|
|
+ private fun setUpView(context: Context) {
|
|
|
+ if (frameLayout == null) {
|
|
|
+ frameLayout = FrameLayout(context)
|
|
|
+ frameLayout?.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
|
|
+ mapView = MapView(context)
|
|
|
+ mapView?.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
|
|
+ frameLayout?.addView(mapView)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fun onCreate() {
|
|
|
+ // TODO SaveInstance restore
|
|
|
+ mapView?.onCreate(null)
|
|
|
+ if (aMap == null) {
|
|
|
+ aMap = mapView?.map
|
|
|
+ }
|
|
|
+ val locationStyle = MyLocationStyle()
|
|
|
+ locationStyle.showMyLocation(true)
|
|
|
+ locationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.dog))
|
|
|
+ locationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER)
|
|
|
+ aMap?.myLocationStyle = locationStyle
|
|
|
+ aMap?.isMyLocationEnabled = true
|
|
|
+ aMap?.uiSettings?.isMyLocationButtonEnabled = true
|
|
|
+ aMap?.setOnMyLocationChangeListener(this)
|
|
|
+ aMap?.setInfoWindowAdapter(this)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun onPause() {
|
|
|
+ mapView?.onPause()
|
|
|
+ }
|
|
|
+
|
|
|
+ fun onResume() {
|
|
|
+ mapView?.onResume()
|
|
|
+ }
|
|
|
+
|
|
|
+ fun setMarkers(markers: List<Map<String, Any>>) {
|
|
|
+ this.mMarkers = markers
|
|
|
+
|
|
|
+ aMap?.addMarkers(mMarkers?.map {
|
|
|
+ MarkerOptions().position(LatLng(it["lat"].toString().toDouble(), it["lon"].toString().toDouble()))
|
|
|
+ .icon(BitmapDescriptorFactory.fromResource(R.drawable.i2)).title(it["title"].toString())
|
|
|
+ }?.toList() as ArrayList<MarkerOptions>, false)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun getView(): View {
|
|
|
+ setUpView(context)
|
|
|
+ return frameLayout!!
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun dispose() {
|
|
|
+ firstMove = false
|
|
|
+ mMarkers = null
|
|
|
+ if (frameLayout != null) {
|
|
|
+ frameLayout?.removeView(mapView)
|
|
|
+ frameLayout = null
|
|
|
+ }
|
|
|
+ if (mapView != null) {
|
|
|
+ mapView?.removeAllViews()
|
|
|
+ infoWindow = null
|
|
|
+ mapView = null
|
|
|
+ }
|
|
|
+ aMap?.setOnMyLocationChangeListener(null)
|
|
|
+ if (aMap != null) {
|
|
|
+ aMap = null
|
|
|
+ }
|
|
|
+ mapView?.onDestroy()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onMyLocationChange(p0: Location) {
|
|
|
+ if (!firstMove) {
|
|
|
+ aMap?.moveCamera(CameraUpdateFactory.newCameraPosition(CameraPosition(LatLng(p0.latitude, p0.longitude), 14f, 0f, 0f)))
|
|
|
+ firstMove = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun getInfoContents(p0: Marker?): View? {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun getInfoWindow(p0: Marker?): View {
|
|
|
+ if (infoWindow == null) {
|
|
|
+ infoWindow = LayoutInflater.from(context).inflate(
|
|
|
+ R.layout.custom_info_window, null)
|
|
|
+ }
|
|
|
+ render(p0, infoWindow);
|
|
|
+ return infoWindow!!
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun render(marker: Marker?, view: View?) {
|
|
|
+ val tvTitle = view?.findViewById<TextView>(R.id.tv_title)
|
|
|
+ val tvContent = view?.findViewById<TextView>(R.id.tv_content)
|
|
|
+ val tvTel = view?.findViewById<TextView>(R.id.tv_tel)
|
|
|
+ val closeImg = view?.findViewById<ImageView>(R.id.img_close)
|
|
|
+ closeImg?.setOnClickListener {
|
|
|
+ marker?.hideInfoWindow()
|
|
|
+ }
|
|
|
+ val tvGuide = view?.findViewById<TextView>(R.id.tv_guide)
|
|
|
+ tvGuide?.paint?.flags = Paint.UNDERLINE_TEXT_FLAG;
|
|
|
+
|
|
|
+ val map: Map<String, Any>? = mMarkers?.first { it["title"].toString() == marker?.title }
|
|
|
+ if (map != null) {
|
|
|
+ //TODO 中英文
|
|
|
+ tvTitle?.text = map["title"].toString()
|
|
|
+ tvContent?.text = "地址:${map["content"].toString()}"
|
|
|
+ tvTel?.text = "电话:${map["tel"].toString()}"
|
|
|
+ tvGuide?.setOnClickListener {
|
|
|
+ // return data to flutter
|
|
|
+ channel.invokeMethod("guide", map)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|