|
|
@@ -13,70 +13,70 @@ 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.embedding.engine.plugins.activity.ActivityAware
|
|
|
+import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
|
|
|
+import io.flutter.plugin.common.BinaryMessenger
|
|
|
+import io.flutter.plugin.common.MethodCall
|
|
|
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,
|
|
|
+class AMapView(private val context: Context, messenger: BinaryMessenger, id: Int, params: Any?) : PlatformView, MethodChannel.MethodCallHandler,
|
|
|
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 frameLayout: FrameLayout? = null
|
|
|
+ private var mapView: MapView? = null
|
|
|
+ private var aMap: AMap? = null
|
|
|
+ private var infoWindow: View? = null
|
|
|
+ private 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)
|
|
|
+ private var methodChannel: MethodChannel? = null
|
|
|
+
|
|
|
+ init {
|
|
|
+ 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)
|
|
|
+ methodChannel = MethodChannel(messenger, "com.i2edu.mapView/map_view_$id")
|
|
|
+ methodChannel?.setMethodCallHandler(this)
|
|
|
}
|
|
|
|
|
|
override fun getView(): View {
|
|
|
- setUpView(context)
|
|
|
return frameLayout!!
|
|
|
}
|
|
|
|
|
|
+ override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
|
|
|
+ when (call.method) {
|
|
|
+ // map view
|
|
|
+ "onCreate" -> {
|
|
|
+ createMapView()
|
|
|
+ result.success(true)
|
|
|
+ }
|
|
|
+ "onPause" -> {
|
|
|
+ mapView?.onPause()
|
|
|
+ result.success(true)
|
|
|
+ }
|
|
|
+ "onResume" -> {
|
|
|
+ mapView?.onResume()
|
|
|
+ result.success(true)
|
|
|
+ }
|
|
|
+ "onDestroy" -> {
|
|
|
+ dispose()
|
|
|
+ result.success(true)
|
|
|
+ }
|
|
|
+ "setMarkers" -> {
|
|
|
+ val data = call.argument<List<Map<String, Any>>>("markers")!!
|
|
|
+ setMarkers(data)
|
|
|
+ result.success(true)
|
|
|
+ }
|
|
|
+ else -> {
|
|
|
+ result.notImplemented()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
override fun dispose() {
|
|
|
firstMove = false
|
|
|
mMarkers = null
|
|
|
@@ -116,6 +116,31 @@ class AMapView(private val context: Context, private val channel: MethodChannel)
|
|
|
return infoWindow!!
|
|
|
}
|
|
|
|
|
|
+ private fun createMapView() {
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+
|
|
|
+ private 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)
|
|
|
+ }
|
|
|
+
|
|
|
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)
|
|
|
@@ -135,7 +160,7 @@ class AMapView(private val context: Context, private val channel: MethodChannel)
|
|
|
tvTel?.text = "电话:${map["tel"].toString()}"
|
|
|
tvGuide?.setOnClickListener {
|
|
|
// return data to flutter
|
|
|
- channel.invokeMethod("guide", map)
|
|
|
+ methodChannel?.invokeMethod("guide", map)
|
|
|
}
|
|
|
}
|
|
|
}
|