Maps3.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. import { StyleSheet } from 'react-native';
  12. export default Maps3 = ({ region, onMapReady, stopList, useApplesMap, showUserLocation=true, onMarkerPress }) => {
  13. const mapRef = useRef();
  14. const superClusterRef = useRef();
  15. useEffect(() => {
  16. if (isIOS) {
  17. //mapRef.current.animateToRegion(region, 500);
  18. mapRef.current.animateCamera({ center: region, zoom: 17 }, { duration: 500 });
  19. //mapRef.current.animateCamera({ center: region, zoom: 17 }, 500); //移动地图到当前位置并放大
  20. } else {
  21. if (mapRef.current.moveCamera) {
  22. mapRef.current.moveCamera(region, 17);
  23. //mapRef.current.showUserLocation();
  24. }
  25. }
  26. }, [region])
  27. /*componentDidMount() {
  28. this.mapRef = this.ref.current.mapRef ? this.ref.current.mapRef : this.ref.current;
  29. }*/
  30. const renderCluster = (props) => {
  31. //console.log("renderCluster", props);
  32. let hasAvailableConnector = false;
  33. if (props.geometry.hasAvailableConnector === undefined) {
  34. if (superClusterRef) {
  35. const points = superClusterRef.current.getLeaves(props.id, Infinity, 0);
  36. if (points) {
  37. for (let index = 0; index < points.length; index++) {
  38. const point = points[index];
  39. if (point.properties.coordinate?.available) {
  40. hasAvailableConnector = true;
  41. break;
  42. }
  43. }
  44. }
  45. }
  46. } else {
  47. hasAvailableConnector = props.geometry.hasAvailableConnector
  48. }
  49. props.geometry.hasAvailableConnector = hasAvailableConnector;
  50. const latlng = {latitude: props.geometry.coordinates[1], longitude: props.geometry.coordinates[0]}
  51. //InteractionManager.runAfterInteractions();
  52. return (
  53. <MyCluster
  54. {...props}
  55. key={props.id}
  56. location={latlng}
  57. available={hasAvailableConnector}
  58. onOpen1={latlng => openClusterMarker(latlng)}/>
  59. );
  60. }
  61. const openClusterMarker = async (latlng) => {
  62. var camera = await mapRef.current.getCamera();
  63. camera.center = latlng
  64. if (camera.zoom < 10) {
  65. camera.zoom = 10;
  66. }
  67. if (camera.zoom > 15) {
  68. camera.zoom += 1
  69. } else {
  70. camera.zoom += 2
  71. }
  72. console.log('camera', camera);
  73. mapRef.current.animateCamera(camera, 300);
  74. }
  75. return (
  76. isIOS
  77. ? <MapView
  78. ref={mapRef}
  79. superClusterRef={superClusterRef}
  80. style={StyleSheet.absoluteFillObject}
  81. minZoom={5}
  82. maxZoom={20}
  83. radius={45}
  84. extent={100}
  85. nodeSize={128}
  86. showsIndoors={false}
  87. showsTraffic={false}
  88. toolbarEnabled={false}
  89. initialRegion={region}
  90. tintColor={colorAccent}
  91. onMapReady={onMapReady}
  92. poiClickEnabled={false}
  93. zoomControlEnabled={false}
  94. tracksViewChanges={true}
  95. showsMyLocationButton={!ready}
  96. showsIndoorLevelPicker={false}
  97. showsUserLocation={showUserLocation}
  98. followsUserLocation={showUserLocation}
  99. renderCluster={info => renderCluster(info)}
  100. provider={useApplesMap ? PROVIDER_DEFAULT : PROVIDER_GOOGLE}>
  101. { stopList.map((marker, index) => {
  102. return (
  103. <MyMarker
  104. key={index}
  105. id={marker.id}
  106. coordinate={marker}
  107. onPress={() => onMarkerPress(marker.id)}
  108. />
  109. );
  110. })}
  111. </MapView>
  112. : <VbeClusterMap
  113. ref={mapRef}
  114. style={StyleSheet.absoluteFill}
  115. region={region}
  116. data={stopList}
  117. animation={true}
  118. showCompass={false}
  119. showTraffic={false}
  120. showIndoors={false}
  121. showIndoorLevelPicker={false}
  122. onMapReady={onMapReady}
  123. onMarkerPress={e => onMarkerPress(e.id)}
  124. showUserLocation={showUserLocation}
  125. moveOnMarkerPress={true}
  126. />
  127. );
  128. }