Maps1.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * 首页地图组件-使用ClusterMap
  3. * @邠心vbe on 2021/03/18
  4. */
  5. import React, { useEffect, useRef } from 'react';
  6. import { PROVIDER_GOOGLE } from 'react-native-maps';
  7. import { ClusterMap } from 'react-native-cluster-map';
  8. import { ClusterView, MyCluster, MyMarker } from './Cluster';
  9. export default Maps = ({ region, onMapReady, stopList, onMarkerPress }) => {
  10. const mapRef = useRef();
  11. const superClusterRef = useRef();
  12. useEffect(() => {
  13. console.log("mapRef", mapRef.current);
  14. //mapRef.current.animateCamera({ center: region, zoom: 17 }, 500); //移动地图到当前位置并放大
  15. }, [region])
  16. /*componentDidMount() {
  17. this.mapRef = this.ref.current.mapRef ? this.ref.current.mapRef : this.ref.current;
  18. }*/
  19. const renderCluster = (props) => {
  20. //InteractionManager.runAfterInteractions();
  21. return <MyCluster {...props} key={props.id} onOpen1={latlng => {openClusterMarker(latlng)}}/>
  22. }
  23. const openClusterMarker = async (latlng) => {
  24. var camera = await mapRef.current.getCamera();
  25. camera.center = latlng
  26. camera.zoom = camera.zoom + 2
  27. //console.log('camera', camera);
  28. mapRef.current.animateCamera(camera, 300);
  29. }
  30. getMyCluster = (props) => {
  31. return <ClusterView id={props.clusterId} pointCount={props.pointCount}/>
  32. }
  33. const superClusterOptions = {
  34. minZoom: 1,
  35. maxZoom: 15,
  36. radius: 25,
  37. extent: 512,
  38. nodeSize: 32
  39. }
  40. return (
  41. <ClusterMap
  42. ref={mapRef}
  43. style={ui.flex1}
  44. region={region}
  45. superClusterOptions={superClusterOptions}
  46. provider={PROVIDER_GOOGLE}
  47. onMapReady={onMapReady}
  48. showsUserLocation={true}
  49. renderClusterMarker={getMyCluster}>
  50. { stopList.map((marker, index) => {
  51. return (
  52. <MyMarker
  53. key={index}
  54. {...marker}
  55. coordinate={marker.location}
  56. onPress={() => onMarkerPress(marker.id)}
  57. />
  58. );
  59. })}
  60. </ClusterMap>
  61. );
  62. }