Maps3.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * 首页地图组件-使用ClusteringMapView和VbeClusterMap
  3. * @邠心vbe on 2021/03/18
  4. */
  5. import React, { useEffect, useRef } from 'react';
  6. import { StyleSheet } from 'react-native';
  7. import { PROVIDER_DEFAULT, PROVIDER_GOOGLE } from 'react-native-maps';
  8. import MapView from "@allisonadam81/react-native-super-clusters";
  9. import { MyCluster, MyMarker } from './Cluster';
  10. import VbeClusterMap from 'vbe-cluster-map';
  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. if (camera.zoom < 10) {
  64. camera.zoom = 10;
  65. }
  66. if (camera.zoom > 15) {
  67. camera.zoom += 1
  68. } else {
  69. camera.zoom += 2
  70. }
  71. console.log('camera', camera);
  72. mapRef.current.animateCamera(camera, 300);
  73. }
  74. return (
  75. isIOS
  76. ? <MapView
  77. ref={mapRef}
  78. superClusterRef={superClusterRef}
  79. style={StyleSheet.absoluteFillObject}
  80. minZoom={5}
  81. maxZoom={20}
  82. radius={45}
  83. extent={100}
  84. nodeSize={128}
  85. showsIndoors={false}
  86. showsTraffic={false}
  87. toolbarEnabled={false}
  88. initialRegion={region}
  89. tintColor={colorAccent}
  90. onMapReady={onMapReady}
  91. poiClickEnabled={false}
  92. zoomControlEnabled={false}
  93. tracksViewChanges={true}
  94. showsMyLocationButton={false}
  95. showsIndoorLevelPicker={false}
  96. showsUserLocation={showUserLocation}
  97. followsUserLocation={showUserLocation}
  98. renderCluster={info => renderCluster(info)}
  99. provider={useApplesMap ? PROVIDER_DEFAULT : PROVIDER_GOOGLE}>
  100. { stopList.map((marker, index) => {
  101. return (
  102. <MyMarker
  103. key={index}
  104. id={marker.id}
  105. coordinate={marker}
  106. onPress={() => onMarkerPress(marker.id)}
  107. />
  108. );
  109. })}
  110. </MapView>
  111. : <VbeClusterMap
  112. ref={mapRef}
  113. style={StyleSheet.absoluteFill}
  114. region={region}
  115. data={stopList}
  116. animation={true}
  117. showCompass={false}
  118. showTraffic={false}
  119. showIndoors={false}
  120. showIndoorLevelPicker={false}
  121. onMapReady={onMapReady}
  122. onMarkerPress={e => onMarkerPress(e.id)}
  123. showUserLocation={showUserLocation}
  124. moveOnMarkerPress={true}
  125. />
  126. );
  127. }