Commit 4043765d authored by 7c00's avatar 7c00

Add MapView#locationStyle

#151
parent 5d59eed3
...@@ -16,11 +16,10 @@ import com.facebook.react.uimanager.events.RCTEventEmitter ...@@ -16,11 +16,10 @@ import com.facebook.react.uimanager.events.RCTEventEmitter
class AMapView(context: Context) : TextureMapView(context) { class AMapView(context: Context) : TextureMapView(context) {
private val eventEmitter: RCTEventEmitter = (context as ThemedReactContext).getJSModule(RCTEventEmitter::class.java) private val eventEmitter: RCTEventEmitter = (context as ThemedReactContext).getJSModule(RCTEventEmitter::class.java)
private val markers = HashMap<String, AMapMarker>() private val markers = HashMap<String, AMapMarker>()
private val polylines = HashMap<String, AMapPolyline>() private val lines = HashMap<String, AMapPolyline>()
private var locationType = MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER
private val locationStyle by lazy { private val locationStyle by lazy {
val locationStyle = MyLocationStyle() val locationStyle = MyLocationStyle()
locationStyle.myLocationType(locationType) locationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER)
locationStyle locationStyle
} }
...@@ -94,7 +93,7 @@ class AMapView(context: Context) : TextureMapView(context) { ...@@ -94,7 +93,7 @@ class AMapView(context: Context) : TextureMapView(context) {
} }
map.setOnPolylineClickListener { polyline -> map.setOnPolylineClickListener { polyline ->
emit(polylines[polyline.id]?.id, "onPress") emit(lines[polyline.id]?.id, "onPress")
} }
map.setOnMultiPointClickListener { item -> map.setOnMultiPointClickListener { item ->
...@@ -137,7 +136,7 @@ class AMapView(context: Context) : TextureMapView(context) { ...@@ -137,7 +136,7 @@ class AMapView(context: Context) : TextureMapView(context) {
markers.put(child.marker?.id!!, child) markers.put(child.marker?.id!!, child)
} }
if (child is AMapPolyline) { if (child is AMapPolyline) {
polylines.put(child.polyline?.id!!, child) lines.put(child.polyline?.id!!, child)
} }
} }
} }
...@@ -149,7 +148,7 @@ class AMapView(context: Context) : TextureMapView(context) { ...@@ -149,7 +148,7 @@ class AMapView(context: Context) : TextureMapView(context) {
markers.remove(child.marker?.id) markers.remove(child.marker?.id)
} }
if (child is AMapPolyline) { if (child is AMapPolyline) {
polylines.remove(child.polyline?.id) lines.remove(child.polyline?.id)
} }
} }
} }
...@@ -205,13 +204,12 @@ class AMapView(context: Context) : TextureMapView(context) { ...@@ -205,13 +204,12 @@ class AMapView(context: Context) : TextureMapView(context) {
} }
fun setLocationEnabled(enabled: Boolean) { fun setLocationEnabled(enabled: Boolean) {
map.myLocationStyle = locationStyle
map.isMyLocationEnabled = enabled map.isMyLocationEnabled = enabled
map.myLocationStyle = locationStyle
} }
fun setLocationInterval(interval: Long) { fun setLocationInterval(interval: Long) {
locationStyle.interval(interval) locationStyle.interval(interval)
map.myLocationStyle = locationStyle
} }
private fun latLngBoundsFromReadableMap(region: ReadableMap): LatLngBounds { private fun latLngBoundsFromReadableMap(region: ReadableMap): LatLngBounds {
...@@ -224,4 +222,24 @@ class AMapView(context: Context) : TextureMapView(context) { ...@@ -224,4 +222,24 @@ class AMapView(context: Context) : TextureMapView(context) {
LatLng(latitude + latitudeDelta / 2, longitude + longitudeDelta / 2) LatLng(latitude + latitudeDelta / 2, longitude + longitudeDelta / 2)
) )
} }
fun setLocationStyle(style: ReadableMap) {
if (style.hasKey("fillColor")) {
locationStyle.radiusFillColor(style.getInt("fillColor"))
}
if (style.hasKey("stockColor")) {
locationStyle.strokeColor(style.getInt("stockColor"))
}
if (style.hasKey("stockWidth")) {
locationStyle.strokeWidth(style.getDouble("stockWidth").toFloat())
}
if (style.hasKey("image")) {
val drawable = context.resources.getIdentifier(
style.getString("image"), "drawable", context.packageName)
locationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(drawable))
}
}
} }
...@@ -189,4 +189,9 @@ internal class AMapViewManager : ViewGroupManager<AMapView>() { ...@@ -189,4 +189,9 @@ internal class AMapViewManager : ViewGroupManager<AMapView>() {
fun setLocationInterval(view: AMapView, interval: Int) { fun setLocationInterval(view: AMapView, interval: Int) {
view.setLocationInterval(interval.toLong()) view.setLocationInterval(interval.toLong())
} }
@ReactProp(name = "locationStyle")
fun setLocationStyle(view: AMapView, style: ReadableMap) {
view.setLocationStyle(style)
}
} }
// @flow // @flow
import React from 'react' import React from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { requireNativeComponent, ViewPropTypes } from 'react-native' import { processColor, requireNativeComponent, ViewPropTypes } from 'react-native'
import { LatLng, Region } from '../PropTypes' import { LatLng, Region } from '../PropTypes'
import Component from '../Component' import Component from '../Component'
...@@ -12,6 +12,13 @@ export type MapStatus = { ...@@ -12,6 +12,13 @@ export type MapStatus = {
rotation?: number, rotation?: number,
} }
export const LocationStyle = PropTypes.shape({
image: PropTypes.string,
fillColor: PropTypes.string,
strokeColor: PropTypes.string,
strokeWidth: PropTypes.number,
})
export default class MapView extends Component<any> { export default class MapView extends Component<any> {
static propTypes = { static propTypes = {
...ViewPropTypes, ...ViewPropTypes,
...@@ -27,6 +34,11 @@ export default class MapView extends Component<any> { ...@@ -27,6 +34,11 @@ export default class MapView extends Component<any> {
*/ */
mapType: PropTypes.oneOf(['standard', 'satellite', 'navigation', 'night', 'bus']), mapType: PropTypes.oneOf(['standard', 'satellite', 'navigation', 'night', 'bus']),
/**
* 设置定位图标的样式
*/
locationStyle: LocationStyle,
/** /**
* 是否启用定位 * 是否启用定位
*/ */
...@@ -240,7 +252,16 @@ export default class MapView extends Component<any> { ...@@ -240,7 +252,16 @@ export default class MapView extends Component<any> {
} }
render() { render() {
return <AMapView {...this.props} /> const props = { ...this.props }
if (props.locationStyle) {
if (props.locationStyle.strokeColor) {
props.locationStyle.strokeColor = processColor(props.locationStyle.strokeColor)
}
if (props.locationStyle.fillColor) {
props.locationStyle.fillColor = processColor(props.locationStyle.fillColor)
}
}
return <AMapView {...props} />
} }
} }
......
...@@ -21,12 +21,12 @@ export default class Marker extends Component<any> { ...@@ -21,12 +21,12 @@ export default class Marker extends Component<any> {
coordinate: LatLng.isRequired, coordinate: LatLng.isRequired,
/** /**
* 标题 * 标题,作为默认的选中弹出显示
*/ */
title: PropTypes.string, title: PropTypes.string,
/** /**
* 描述 * 描述,显示在标题下方
*/ */
description: PropTypes.string, description: PropTypes.string,
...@@ -54,12 +54,12 @@ export default class Marker extends Component<any> { ...@@ -54,12 +54,12 @@ export default class Marker extends Component<any> {
}), }),
/** /**
* 自定义图标,慎用,目前存在一些已知的 bug * 自定义图标
*/ */
icon: PropTypes.func, icon: PropTypes.func,
/** /**
* 自定义图片 * 自定义图片,对应原生图片名称
*/ */
image: PropTypes.string, image: PropTypes.string,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment