index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { View, ViewPropTypes, requireNativeComponent } from 'react-native';
  4. const viewPropTypes = ViewPropTypes || View.propTypes;
  5. const propTypes = {
  6. ...viewPropTypes,
  7. /**
  8. * Callback that is called continuously when the user is dragging the map.
  9. */
  10. style: viewPropTypes.style,
  11. region: PropTypes.shape({
  12. latitude: PropTypes.number.isRequired,
  13. longitude: PropTypes.number.isRequired,
  14. latitudeDelta: PropTypes.number.isRequired,
  15. longitudeDelta: PropTypes.number.isRequired,
  16. zoom: PropTypes.number
  17. }),
  18. data: PropTypes.array,
  19. animation: PropTypes.bool,
  20. showUserLocation: PropTypes.bool,
  21. moveOnMarkerPress: PropTypes.bool,
  22. onMapReady: PropTypes.func,
  23. onMarkerPress: PropTypes.func
  24. };
  25. const VbeClusterView = requireNativeComponent('VbeClusterMap', propTypes);
  26. class VbeClusterMap extends Component {
  27. constructor(props) {
  28. super(props);
  29. this.state = {
  30. mapReady: false
  31. }
  32. this.map;
  33. this._onMapReady = this._onMapReady.bind(this);
  34. this._onMarkerPress = this._onMarkerPress.bind(this);
  35. this.moveCamera = this.moveCamera.bind(this);
  36. this.showUserLocation = this.showUserLocation.bind(this);
  37. }
  38. _onMapReady() {
  39. this.setState({
  40. mapReady: true
  41. })
  42. /*if (this.props.region) {
  43. this.map.setNativeProps({ region: this.props.region });
  44. }*/
  45. if (this.props.onMapReady) {
  46. this.props.onMapReady();
  47. }
  48. }
  49. _onMarkerPress(event) {
  50. if (this.props.onMarkerPress) {
  51. this.props.onMarkerPress(event.nativeEvent);
  52. }
  53. }
  54. moveCamera(region, zoom) {
  55. //console.log('moveCamera', region);
  56. if (region) {
  57. if (!region.zoom && zoom) {
  58. region.zoom = zoom;
  59. }
  60. this.map.setNativeProps({ region: region });
  61. }
  62. }
  63. showUserLocation() {
  64. this.map.setNativeProps({ showUserLocation: this.props.showUserLocation });
  65. }
  66. render() {
  67. return (
  68. <VbeClusterView
  69. ref={ref => this.map = ref}
  70. {...this.props}
  71. onMapReady={this._onMapReady}
  72. onMarkerPress={this._onMarkerPress}
  73. />
  74. );
  75. }
  76. }
  77. VbeClusterMap.propTypes = propTypes;
  78. module.exports = VbeClusterMap;