Maps1.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 MapView from "react-native-map-clustering";
  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. <MapView
  42. ref={mapRef}
  43. superClusterRef={superClusterRef}
  44. style={ui.flex1}
  45. minZoom={10}
  46. maxZoom={15}
  47. radius={45}
  48. extent={512}
  49. nodeSize={64}
  50. provider={PROVIDER_GOOGLE}
  51. onMapReady={onMapReady}
  52. showsUserLocation={true}
  53. initialRegion={region}
  54. renderCluster={(info) => renderCluster(info)}>
  55. { stopList.map((marker, index) => {
  56. return (
  57. <MyMarker
  58. key={index}
  59. {...marker}
  60. coordinate={marker.location}
  61. onPress={() => onMarkerPress(marker.id)}
  62. />
  63. );
  64. })}
  65. </MapView>
  66. );
  67. }