Maps2.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * 首页地图组件-使用ClusteringMapView
  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. export default Maps2 = ({ region, onMapReady, stopList, onMarkerPress }) => {
  10. const mapRef = useRef();
  11. const superClusterRef = useRef();
  12. useEffect(() => {
  13. console.log('mapRef.current', mapRef.current);
  14. //mapRef.current.animateToRegion(region, 500);
  15. mapRef.current.animateCamera({ center: region, zoom: 17 }, { duration: 500 }); //移动地图到当前位置并放大
  16. }, [region])
  17. /*componentDidMount() {
  18. this.mapRef = this.ref.current.mapRef ? this.ref.current.mapRef : this.ref.current;
  19. }*/
  20. const renderCluster = (props) => {
  21. //console.log("renderCluster", props);
  22. let hasAvailableConnector = false;
  23. if (props.geometry.hasAvailableConnector === undefined) {
  24. if (superClusterRef) {
  25. const points = superClusterRef.current.getLeaves(props.id, Infinity, 0);
  26. if (points) {
  27. for (let index = 0; index < points.length; index++) {
  28. const point = points[index];
  29. if (point.properties.coordinate?.available) {
  30. hasAvailableConnector = true;
  31. break;
  32. }
  33. }
  34. }
  35. }
  36. } else {
  37. hasAvailableConnector = props.geometry.hasAvailableConnector
  38. }
  39. props.geometry.hasAvailableConnector = hasAvailableConnector;
  40. const latlng = {latitude: props.geometry.coordinates[1], longitude: props.geometry.coordinates[0]}
  41. //InteractionManager.runAfterInteractions();
  42. return (
  43. <MyCluster
  44. {...props}
  45. key={props.id}
  46. location={latlng}
  47. available={hasAvailableConnector}
  48. onOpen2={latlng => openClusterMarker(latlng)}/>
  49. );
  50. }
  51. const openClusterMarker = async (latlng) => {
  52. var camera = await mapRef.current.getCamera();
  53. camera.center = latlng
  54. camera.zoom = camera.zoom + 2
  55. //console.log('camera', camera);
  56. mapRef.current.animateCamera(camera, 300);
  57. }
  58. return (
  59. <MapView
  60. ref={mapRef}
  61. superClusterRef={superClusterRef}
  62. style={ui.flex1}
  63. minZoom={10}
  64. maxZoom={15}
  65. radius={45}
  66. extent={512}
  67. nodeSize={64}
  68. provider={PROVIDER_GOOGLE}
  69. onMapReady={onMapReady}
  70. showsUserLocation={true}
  71. initialRegion={region}
  72. renderCluster={(info) => renderCluster(info)}>
  73. { stopList.map((marker, index) => {
  74. return (
  75. <MyMarker
  76. key={index}
  77. {...marker}
  78. coordinate={marker}
  79. onPress={() => onMarkerPress(marker.id)}
  80. />
  81. );
  82. })}
  83. </MapView>
  84. );
  85. }