/** * 首页地图组件-使用ClusteringMapView和VbeClusterMap * @邠心vbe on 2021/03/18 */ import React, { useEffect, useRef } from 'react'; import { PROVIDER_DEFAULT, PROVIDER_GOOGLE } from 'react-native-maps'; import MapView from "react-native-map-clustering"; import { MyCluster, MyMarker } from './Cluster'; import VbeClusterMap from 'vbe-cluster-map'; import app from '../../../../app.json'; export default Maps3 = ({ region, onMapReady, stopList, useApplesMap, showUserLocation=true, onMarkerPress }) => { const mapRef = useRef(); const superClusterRef = useRef(); useEffect(() => { if (isIOS) { //mapRef.current.animateToRegion(region, 500); mapRef.current.animateCamera({ center: region, zoom: 17 }, { duration: 500 }); //mapRef.current.animateCamera({ center: region, zoom: 17 }, 500); //移动地图到当前位置并放大 } else { if (mapRef.current.moveCamera) { mapRef.current.moveCamera(region, 17); //mapRef.current.showUserLocation(); } } }, [region]) /*componentDidMount() { this.mapRef = this.ref.current.mapRef ? this.ref.current.mapRef : this.ref.current; }*/ const renderCluster = (props) => { //console.log("renderCluster", props); let hasAvailableConnector = false; if (props.geometry.hasAvailableConnector === undefined) { if (superClusterRef) { const points = superClusterRef.current.getLeaves(props.id, Infinity, 0); if (points) { for (let index = 0; index < points.length; index++) { const point = points[index]; if (point.properties.coordinate?.available) { hasAvailableConnector = true; break; } } } } } else { hasAvailableConnector = props.geometry.hasAvailableConnector } props.geometry.hasAvailableConnector = hasAvailableConnector; const latlng = {latitude: props.geometry.coordinates[1], longitude: props.geometry.coordinates[0]} //InteractionManager.runAfterInteractions(); return ( openClusterMarker(latlng)}/> ); } const openClusterMarker = async (latlng) => { var camera = await mapRef.current.getCamera(); camera.center = latlng camera.zoom = camera.zoom + 2 //console.log('camera', camera); mapRef.current.animateCamera(camera, 300); } return ( isIOS ? renderCluster(info)}> { stopList.map((marker, index) => { return ( onMarkerPress(marker.id)} /> ); })} : onMarkerPress(e.id)} showUserLocation={showUserLocation} moveOnMarkerPress={true} /> ); }