Commit 682c4685 authored by 7c00's avatar 7c00

Marker 新增 color、image 接口,原 props.icon 现只用于自定义 view

parent 3ca95954
...@@ -23,6 +23,7 @@ class AMapMarker(context: Context) : ReactViewGroup(context), AMapOverlay { ...@@ -23,6 +23,7 @@ class AMapMarker(context: Context) : ReactViewGroup(context), AMapOverlay {
) )
} }
private var bitmapDescriptor: BitmapDescriptor? = null
var infoWindow: AMapInfoWindow? = null var infoWindow: AMapInfoWindow? = null
var infoWindowEnabled: Boolean = true var infoWindowEnabled: Boolean = true
...@@ -92,14 +93,12 @@ class AMapMarker(context: Context) : ReactViewGroup(context), AMapOverlay { ...@@ -92,14 +93,12 @@ class AMapMarker(context: Context) : ReactViewGroup(context), AMapOverlay {
} }
} }
var customIcon: AMapMarkerIcon? = null var icon: AMapMarkerIcon? = null
set(value) { set(value) {
field = value field = value
value?.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> updateCustomIcon() } value?.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> updateIcon() }
} }
private var bitmapDescriptor: BitmapDescriptor? = null
override fun add(map: AMap) { override fun add(map: AMap) {
marker = map.addMarker(MarkerOptions() marker = map.addMarker(MarkerOptions()
.setFlat(flat) .setFlat(flat)
...@@ -132,8 +131,8 @@ class AMapMarker(context: Context) : ReactViewGroup(context), AMapOverlay { ...@@ -132,8 +131,8 @@ class AMapMarker(context: Context) : ReactViewGroup(context), AMapOverlay {
marker?.setIcon(bitmapDescriptor) marker?.setIcon(bitmapDescriptor)
} }
fun updateCustomIcon() { fun updateIcon() {
customIcon?.let { icon?.let {
val bitmap = Bitmap.createBitmap( val bitmap = Bitmap.createBitmap(
it.width, it.height, Bitmap.Config.ARGB_8888) it.width, it.height, Bitmap.Config.ARGB_8888)
it.draw(Canvas(bitmap)) it.draw(Canvas(bitmap))
...@@ -141,4 +140,10 @@ class AMapMarker(context: Context) : ReactViewGroup(context), AMapOverlay { ...@@ -141,4 +140,10 @@ class AMapMarker(context: Context) : ReactViewGroup(context), AMapOverlay {
marker?.setIcon(bitmapDescriptor) marker?.setIcon(bitmapDescriptor)
} }
} }
fun setImage(name: String) {
val drawable = context.resources.getIdentifier(name, "drawable", context.packageName)
bitmapDescriptor = BitmapDescriptorFactory.fromResource(drawable)
marker?.setIcon(bitmapDescriptor)
}
} }
...@@ -20,7 +20,7 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() { ...@@ -20,7 +20,7 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() {
override fun addView(marker: AMapMarker, view: View, index: Int) { override fun addView(marker: AMapMarker, view: View, index: Int) {
when (view) { when (view) {
is AMapMarkerIcon -> marker.customIcon = view is AMapMarkerIcon -> marker.icon = view
is AMapInfoWindow -> marker.infoWindow = view is AMapInfoWindow -> marker.infoWindow = view
} }
} }
...@@ -45,7 +45,7 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() { ...@@ -45,7 +45,7 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() {
override fun receiveCommand(marker: AMapMarker, commandId: Int, args: ReadableArray?) { override fun receiveCommand(marker: AMapMarker, commandId: Int, args: ReadableArray?) {
when (commandId) { when (commandId) {
UPDATE -> marker.updateCustomIcon() UPDATE -> marker.updateIcon()
} }
} }
...@@ -91,11 +91,16 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() { ...@@ -91,11 +91,16 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() {
marker.active = active marker.active = active
} }
@ReactProp(name = "icon") @ReactProp(name = "color")
fun setIcon(marker: AMapMarker, icon: String) { fun setIcon(marker: AMapMarker, icon: String) {
marker.setIconColor(icon) marker.setIconColor(icon)
} }
@ReactProp(name = "image")
fun setImage(marker: AMapMarker, image: String) {
marker.setImage(image)
}
@ReactProp(name = "infoWindowEnabled") @ReactProp(name = "infoWindowEnabled")
fun setEnabledInfoWindow(marker: AMapMarker, enabled: Boolean) { fun setEnabledInfoWindow(marker: AMapMarker, enabled: Boolean) {
marker.infoWindowEnabled = enabled marker.infoWindowEnabled = enabled
......
import React, {PropTypes} from 'react' import React, {PropTypes} from 'react'
import {Platform, requireNativeComponent, StyleSheet, View, ViewPropTypes} from 'react-native' import {Platform, requireNativeComponent, StyleSheet, ViewPropTypes} from 'react-native'
import Overlay from './Overlay' import Overlay from './Overlay'
import InfoWindow from './InfoWindow' import InfoWindow from './InfoWindow'
import {LatLng} from '../PropTypes' import {LatLng} from '../PropTypes'
...@@ -24,31 +24,38 @@ export default class Marker extends BaseComponent { ...@@ -24,31 +24,38 @@ export default class Marker extends BaseComponent {
*/ */
description: PropTypes.string, description: PropTypes.string,
/**
* 默认图标颜色
*/
color: Platform.select({
android: PropTypes.oneOf([
'azure',
'blue',
'cyan',
'green',
'magenta',
'orange',
'red',
'rose',
'violet',
'yellow',
]),
ios: PropTypes.oneOf([
'red',
'green',
'purple',
]),
}),
/** /**
* 自定义图标 * 自定义图标
*/ */
icon: PropTypes.oneOfType([ icon: PropTypes.func,
Platform.select({
android: PropTypes.oneOf([ /**
'azure', * 自定义图片
'blue', */
'cyan', image: PropTypes.string,
'green',
'magenta',
'orange',
'red',
'rose',
'violet',
'yellow',
]),
ios: PropTypes.oneOf([
'red',
'green',
'purple',
]),
}),
PropTypes.func,
]),
/** /**
* 透明度 [0, 1] * 透明度 [0, 1]
...@@ -119,23 +126,23 @@ export default class Marker extends BaseComponent { ...@@ -119,23 +126,23 @@ export default class Marker extends BaseComponent {
} }
} }
_renderCustomMarker(icon) {
if (icon) {
this._icon = <Overlay style={style.overlay}>{icon()}</Overlay>
return this._icon
}
}
componentDidUpdate() { componentDidUpdate() {
if (this._customMarker && Platform.OS === 'android') { if (this._icon && Platform.OS === 'android') {
setTimeout(() => this._sendCommand('update'), 0) setTimeout(() => this._sendCommand('update'), 0)
} }
} }
render() { render() {
const props = {...this.props} return <AMapMarker {...this.props}>
{this._renderCustomMarker(this.props.icon)}
if (typeof props.icon === 'function') { {this._renderInfoWindow(this.props.children)}
this._customMarker = <Overlay style={style.overlay}>{props.icon()}</Overlay>
delete props.icon
}
return <AMapMarker {...props}>
{this._customMarker}
{this._renderInfoWindow(props.children)}
</AMapMarker> </AMapMarker>
} }
......
{ {
"images" : [ "images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{ {
"idiom" : "iphone", "idiom" : "iphone",
"size" : "29x29", "size" : "29x29",
...@@ -29,6 +39,11 @@ ...@@ -29,6 +39,11 @@
"idiom" : "iphone", "idiom" : "iphone",
"size" : "60x60", "size" : "60x60",
"scale" : "3x" "scale" : "3x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
} }
], ],
"info" : { "info" : {
......
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "flag.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
...@@ -58,7 +58,7 @@ export default class MarkerExample extends Component { ...@@ -58,7 +58,7 @@ export default class MarkerExample extends Component {
coordinate={this._coordinates[0]} coordinate={this._coordinates[0]}
/> />
<Marker <Marker
icon='green' color='green'
onInfoWindowPress={this._onCustomInfoWindowPress} onInfoWindowPress={this._onCustomInfoWindowPress}
coordinate={this._coordinates[1]}> coordinate={this._coordinates[1]}>
<TouchableOpacity activeOpacity={0.9} onPress={this._onCustomInfoWindowPress}> <TouchableOpacity activeOpacity={0.9} onPress={this._onCustomInfoWindowPress}>
...@@ -68,11 +68,7 @@ export default class MarkerExample extends Component { ...@@ -68,11 +68,7 @@ export default class MarkerExample extends Component {
</TouchableOpacity> </TouchableOpacity>
</Marker> </Marker>
<Marker <Marker
icon={() => image='flag'
<View style={styles.customIcon}>
<Image style={styles.customIcon} source={require('../../images/flag.png')}/>
</View>
}
title='自定义图片' title='自定义图片'
description="Sometimes you'll have some special properties that you need to expose for the native component, but don't actually want them as part of the API for the associated React component." description="Sometimes you'll have some special properties that you need to expose for the native component, but don't actually want them as part of the API for the associated React component."
coordinate={this._coordinates[2]} coordinate={this._coordinates[2]}
......
import React, {Component} from 'react' import React, {Component} from 'react'
import {StyleSheet, Alert} from 'react-native' import {StyleSheet, Alert} from 'react-native'
import {MapView, MultiPoint, Marker} from 'react-native-amap3d' import {MapView, MultiPoint} from 'react-native-amap3d'
export default class MultiPointExample extends Component { export default class MultiPointExample extends Component {
static navigationOptions = { static navigationOptions = {
...@@ -17,7 +17,7 @@ export default class MultiPointExample extends Component { ...@@ -17,7 +17,7 @@ export default class MultiPointExample extends Component {
render() { render() {
return <MapView zoomLevel={12} style={StyleSheet.absoluteFill}> return <MapView zoomLevel={12} style={StyleSheet.absoluteFill}>
<MultiPoint <MultiPoint
image={'icon'} image='point'
points={this._points} points={this._points}
onItemPress={this._onItemPress} onItemPress={this._onItemPress}
/> />
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
MAPinAnnotationView *_pinView; MAPinAnnotationView *_pinView;
MAPinAnnotationColor _pinColor; MAPinAnnotationColor _pinColor;
MACustomCalloutView *_calloutView; MACustomCalloutView *_calloutView;
UIImage *_image;
AMapInfoWindow *_callout; AMapInfoWindow *_callout;
AMapView *_mapView; AMapView *_mapView;
BOOL _active; BOOL _active;
...@@ -39,11 +40,19 @@ ...@@ -39,11 +40,19 @@
_annotation.title = title; _annotation.title = title;
} }
- (void)setIcon:(MAPinAnnotationColor)color { - (void)setColor:(MAPinAnnotationColor)color {
_pinColor = color; _pinColor = color;
_pinView.pinColor = color; _pinView.pinColor = color;
} }
#pragma clang diagnostic ignored "-Woverriding-method-mismatch"
- (void)setImage:(NSString *)name {
_image = [UIImage imageNamed:name];
if (_image != nil) {
_pinView.image = _image;
}
}
- (void)setDescription:(NSString *)description { - (void)setDescription:(NSString *)description {
_annotation.subtitle = description; _annotation.subtitle = description;
} }
...@@ -102,6 +111,9 @@ ...@@ -102,6 +111,9 @@
_pinView.draggable = self.draggable; _pinView.draggable = self.draggable;
_pinView.pinColor = _pinColor; _pinView.pinColor = _pinColor;
_pinView.customCalloutView = _calloutView; _pinView.customCalloutView = _calloutView;
if (_image != nil) {
_pinView.image = _image;
}
} }
return _pinView; return _pinView;
} else { } else {
......
...@@ -23,7 +23,8 @@ RCT_EXPORT_VIEW_PROPERTY(draggable, BOOL) ...@@ -23,7 +23,8 @@ RCT_EXPORT_VIEW_PROPERTY(draggable, BOOL)
RCT_EXPORT_VIEW_PROPERTY(clickable, BOOL) RCT_EXPORT_VIEW_PROPERTY(clickable, BOOL)
RCT_EXPORT_VIEW_PROPERTY(infoWindowEnabled, BOOL) RCT_EXPORT_VIEW_PROPERTY(infoWindowEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(zIndex, NSInteger) RCT_EXPORT_VIEW_PROPERTY(zIndex, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(icon, MAPinAnnotationColor) RCT_EXPORT_VIEW_PROPERTY(color, MAPinAnnotationColor)
RCT_EXPORT_VIEW_PROPERTY(image, NSString)
RCT_EXPORT_VIEW_PROPERTY(onPress, RCTBubblingEventBlock) RCT_EXPORT_VIEW_PROPERTY(onPress, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onInfoWindowPress, RCTBubblingEventBlock) RCT_EXPORT_VIEW_PROPERTY(onInfoWindowPress, RCTBubblingEventBlock)
......
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