Commit 9a6deb84 authored by Qiu Xiang's avatar Qiu Xiang
Browse files

添加地图动画移动接口

parent 5a6b4ecf
Loading
Loading
Loading
Loading
+27 −1
Original line number Diff line number Diff line
@@ -2,14 +2,16 @@ package cn.qiuxiang.react.amap3d

import android.view.View
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.MapView
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.MyLocationStyle
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.events.RCTEventEmitter
import com.facebook.react.views.view.ReactViewGroup

class AMapView(context: ThemedReactContext) : MapView(context) {
    private val eventEmitter: RCTEventEmitter = context.getJSModule(RCTEventEmitter::class.java)
@@ -120,4 +122,28 @@ class AMapView(context: ThemedReactContext) : MapView(context) {
            }
        }
    }

    val animateCallback = object: AMap.CancelableCallback {
        override fun onCancel() {
            TODO("not implemented")
        }

        override fun onFinish() {
            TODO("not implemented")
        }
    }

    fun animateToCoordinate(args: ReadableArray?) {
        val coordinate = args?.getMap(0)!!
        val duration = args.getInt(1)
        val cameraUpdate = CameraUpdateFactory.newLatLng(LatLng(
                coordinate.getDouble("latitude"), coordinate.getDouble("longitude")))
        map.animateCamera(cameraUpdate, duration.toLong(), animateCallback)
    }

    fun animateToZoomLevel(args: ReadableArray?) {
        val zoomLevel = args?.getDouble(0)!!
        val duration = args.getInt(1)
        map.animateCamera(CameraUpdateFactory.zoomTo(zoomLevel.toFloat()), duration.toLong(), animateCallback)
    }
}
+19 −0
Original line number Diff line number Diff line
@@ -4,12 +4,18 @@ import android.view.View
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.LatLng
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.annotations.ReactProp

internal class AMapViewManager : ViewGroupManager<AMapView>() {
    companion object {
        val ANIMATE_TO_COORDINATE = 1
        val ANIMATE_TO_ZOOM_LEVEL = 2
    }

    override fun getName(): String {
        return "AMapView"
    }
@@ -18,6 +24,19 @@ internal class AMapViewManager : ViewGroupManager<AMapView>() {
        return AMapView(reactContext)
    }

    override fun getCommandsMap(): Map<String, Int> {
        return mapOf(
                "animateToCoordinate" to ANIMATE_TO_COORDINATE,
                "animateToZoomLevel" to ANIMATE_TO_ZOOM_LEVEL)
    }

    override fun receiveCommand(overlay: AMapView, commandId: Int, args: ReadableArray?) {
        when (commandId) {
            ANIMATE_TO_COORDINATE -> overlay.animateToCoordinate(args)
            ANIMATE_TO_ZOOM_LEVEL -> overlay.animateToZoomLevel(args)
        }
    }

    override fun addView(mapView: AMapView, child: View, index: Int) {
        super.addView(mapView, child, index)
        when (child) {
+22 −1
Original line number Diff line number Diff line
import React, {PropTypes, Component} from 'react'
import {requireNativeComponent, View} from 'react-native'
import {
  requireNativeComponent,
  findNodeHandle,
  View,
  UIManager,
} from 'react-native'
import Marker from './Marker'
import InfoWindow from './InfoWindow'
import Overlay from './Overlay'
@@ -142,6 +147,22 @@ class MapView extends Component {
    onLocation: React.PropTypes.func,
  }

  animateToCoordinate(coordinate, duration = 1000) {
    this._sendCommand('animateToCoordinate', [coordinate, duration])
  }

  animateToZoomLevel(zoomLevel, duration = 1000) {
    this._sendCommand('animateToZoomLevel', [zoomLevel, duration])
  }

  _sendCommand(command, params = null) {
    UIManager.dispatchViewManagerCommand(
      findNodeHandle(this),
      UIManager.AMapView.Commands[command],
      params,
    )
  }

  render() {
    return <AMapView {...this.props}/>
  }