Maps.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import React, { useEffect, useRef } from 'react';
  2. import { PROVIDER_DEFAULT, PROVIDER_GOOGLE } from 'react-native-maps';
  3. import MapView from "@allisonadam81/react-native-super-clusters";
  4. //import Maps1 from './Maps1'
  5. import Maps2 from './Maps2'
  6. import Maps3 from './Maps3'
  7. import { MyCluster, MyMarker } from './Cluster';
  8. import { StyleSheet } from 'react-native';
  9. const MapOnly= React.memo(({
  10. ready=false,
  11. region, onMapReady, stopList, useApplesMap, showUserLocation=true, onMarkerPress
  12. }) => {
  13. const mapRef = useRef();
  14. const superClusterRef = useRef();
  15. useEffect(() => {
  16. console.log("MapOnly useEffect", "地图初始化");
  17. if (mapRef?.current) {
  18. //mapRef.current.animateToRegion(region, 500);
  19. mapRef.current.animateCamera({ center: region, zoom: 17 }, { duration: 500 }); //移动地图到当前位置并放大
  20. }
  21. }, [region])
  22. const renderCluster = (props) => {
  23. //console.log("renderCluster", props);
  24. let hasAvailableConnector = false;
  25. if (props.geometry.hasAvailableConnector === undefined) {
  26. if (superClusterRef) {
  27. const points = superClusterRef.current.getLeaves(props.id, Infinity, 0);
  28. if (points) {
  29. for (let index = 0; index < points.length; index++) {
  30. const point = points[index];
  31. if (point.properties.coordinate?.available) {
  32. hasAvailableConnector = true;
  33. break;
  34. }
  35. }
  36. }
  37. }
  38. } else {
  39. hasAvailableConnector = props.geometry.hasAvailableConnector
  40. }
  41. props.geometry.hasAvailableConnector = hasAvailableConnector;
  42. const latlng = {latitude: props.geometry.coordinates[1], longitude: props.geometry.coordinates[0]}
  43. //InteractionManager.runAfterInteractions();
  44. return (
  45. <MyCluster
  46. {...props}
  47. key={props.id}
  48. location={latlng}
  49. available={hasAvailableConnector}
  50. onOpen={latlng => openClusterMarker(latlng)}/>
  51. );
  52. }
  53. const openClusterMarker = async (latlng) => {
  54. var camera = await mapRef.current.getCamera();
  55. camera.center = latlng
  56. if (camera.zoom < 10) {
  57. camera.zoom = 10;
  58. }
  59. if (camera.zoom > 15) {
  60. camera.zoom += 1
  61. } else {
  62. camera.zoom += 2
  63. }
  64. console.log('camera', camera);
  65. mapRef.current.animateCamera(camera, 300);
  66. }
  67. return (
  68. <MapView
  69. ref={mapRef}
  70. superClusterRef={superClusterRef}
  71. style={StyleSheet.absoluteFillObject}
  72. minZoom={5}
  73. maxZoom={20}
  74. radius={45}
  75. extent={100}
  76. nodeSize={128}
  77. showsIndoors={false}
  78. showsTraffic={false}
  79. toolbarEnabled={false}
  80. initialRegion={region}
  81. tintColor={colorAccent}
  82. onMapReady={onMapReady}
  83. poiClickEnabled={false}
  84. zoomControlEnabled={false}
  85. tracksViewChanges={true}
  86. showsMyLocationButton={!ready}
  87. showsIndoorLevelPicker={false}
  88. showsUserLocation={showUserLocation}
  89. followsUserLocation={showUserLocation}
  90. renderCluster={info => renderCluster(info)}
  91. provider={useApplesMap ? PROVIDER_DEFAULT : PROVIDER_GOOGLE}>
  92. { stopList.map((marker, index) => {
  93. return (
  94. <MyMarker
  95. key={index}
  96. id={marker.id}
  97. coordinate={marker}
  98. onPress={() => onMarkerPress(marker.id)}
  99. />
  100. );
  101. })}
  102. </MapView>
  103. );
  104. });
  105. export default {
  106. Maps1: Maps2,
  107. Maps2: Maps2,
  108. Maps3: Maps3,
  109. MapOnly: MapOnly
  110. }