Maps3.js 3.5 KB

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