Maps3.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * 首页地图组件-使用ClusteringMapView和VbeClusterMap
  3. * @邠心vbe on 2021/03/18
  4. */
  5. import React, { useEffect, useRef } from 'react';
  6. import { PROVIDER_GOOGLE } from 'react-native-maps';
  7. import MapView from "react-native-map-clustering";
  8. import { MyCluster, MyMarker } from './Cluster';
  9. import VbeClusterMap from 'vbe-cluster-map'
  10. export default Maps3 = ({ region, onMapReady, stopList, showUserLocation=true, onMarkerPress }) => {
  11. const mapRef = useRef();
  12. const superClusterRef = useRef();
  13. useEffect(() => {
  14. if (isIOS) {
  15. //mapRef.current.animateToRegion(region, 500);
  16. mapRef.current.animateCamera({ center: region, zoom: 17 }, { duration: 500 });
  17. //mapRef.current.animateCamera({ center: region, zoom: 17 }, 500); //移动地图到当前位置并放大
  18. } else {
  19. if (mapRef.current.moveCamera) {
  20. mapRef.current.moveCamera(region, 17);
  21. //mapRef.current.showUserLocation();
  22. }
  23. }
  24. }, [region])
  25. /*componentDidMount() {
  26. this.mapRef = this.ref.current.mapRef ? this.ref.current.mapRef : this.ref.current;
  27. }*/
  28. const renderCluster = (props) => {
  29. //console.log("renderCluster", props);
  30. let hasAvailableConnector = false;
  31. if (props.geometry.hasAvailableConnector === undefined) {
  32. if (superClusterRef) {
  33. const points = superClusterRef.current.getLeaves(props.id, Infinity, 0);
  34. if (points) {
  35. for (let index = 0; index < points.length; index++) {
  36. const point = points[index];
  37. if (point.properties.coordinate?.available) {
  38. hasAvailableConnector = true;
  39. break;
  40. }
  41. }
  42. }
  43. }
  44. } else {
  45. hasAvailableConnector = props.geometry.hasAvailableConnector
  46. }
  47. props.geometry.hasAvailableConnector = hasAvailableConnector;
  48. const latlng = {latitude: props.geometry.coordinates[1], longitude: props.geometry.coordinates[0]}
  49. //InteractionManager.runAfterInteractions();
  50. return (
  51. <MyCluster
  52. {...props}
  53. key={props.id}
  54. location={latlng}
  55. available={hasAvailableConnector}
  56. onOpen1={latlng => openClusterMarker(latlng)}/>
  57. );
  58. }
  59. const openClusterMarker = async (latlng) => {
  60. var camera = await mapRef.current.getCamera();
  61. camera.center = latlng
  62. camera.zoom = camera.zoom + 2
  63. //console.log('camera', camera);
  64. mapRef.current.animateCamera(camera, 300);
  65. }
  66. return (
  67. isIOS
  68. ? <MapView
  69. ref={mapRef}
  70. superClusterRef={superClusterRef}
  71. style={ui.flex1}
  72. minZoom={10}
  73. maxZoom={15}
  74. radius={45}
  75. extent={512}
  76. nodeSize={64}
  77. provider={PROVIDER_GOOGLE}
  78. onMapReady={onMapReady}
  79. showsUserLocation={showUserLocation}
  80. initialRegion={region}
  81. renderCluster={(info) => renderCluster(info)}>
  82. { stopList.map((marker, index) => {
  83. return (
  84. <MyMarker
  85. key={index}
  86. {...marker}
  87. coordinate={marker}
  88. onPress={() => onMarkerPress(marker.id)}
  89. />
  90. );
  91. })}
  92. </MapView>
  93. : <VbeClusterMap
  94. ref={mapRef}
  95. style={ui.flex1}
  96. region={region}
  97. data={stopList}
  98. animation={true}
  99. onMapReady={onMapReady}
  100. onMarkerPress={e => onMarkerPress(e.id)}
  101. showUserLocation={showUserLocation}
  102. moveOnMarkerPress={true}
  103. />
  104. );
  105. }