| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- /**
- * 首页地图组件-使用ClusteringMapView和VbeClusterMap
- * @邠心vbe on 2021/03/18
- */
- import React, { useEffect, useRef } from 'react';
- import { StyleSheet } from 'react-native';
- import { PROVIDER_DEFAULT, PROVIDER_GOOGLE } from 'react-native-maps';
- import MapView from "@allisonadam81/react-native-super-clusters";
- import { MyCluster, MyMarker } from './Cluster';
- import VbeClusterMap from 'vbe-cluster-map';
- 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 (
- <MyCluster
- {...props}
- key={props.id}
- location={latlng}
- available={hasAvailableConnector}
- onOpen1={latlng => openClusterMarker(latlng)}/>
- );
- }
- const openClusterMarker = async (latlng) => {
- var camera = await mapRef.current.getCamera();
- camera.center = latlng
- if (camera.zoom < 10) {
- camera.zoom = 10;
- }
- if (camera.zoom > 15) {
- camera.zoom += 1
- } else {
- camera.zoom += 2
- }
- console.log('camera', camera);
- mapRef.current.animateCamera(camera, 300);
- }
- return (
- isIOS
- ? <MapView
- ref={mapRef}
- superClusterRef={superClusterRef}
- style={StyleSheet.absoluteFillObject}
- minZoom={5}
- maxZoom={20}
- radius={45}
- extent={100}
- nodeSize={128}
- showsIndoors={false}
- showsTraffic={false}
- toolbarEnabled={false}
- initialRegion={region}
- tintColor={colorAccent}
- onMapReady={onMapReady}
- poiClickEnabled={false}
- zoomControlEnabled={false}
- tracksViewChanges={true}
- showsMyLocationButton={false}
- showsIndoorLevelPicker={false}
- showsUserLocation={showUserLocation}
- followsUserLocation={showUserLocation}
- renderCluster={info => renderCluster(info)}
- provider={useApplesMap ? PROVIDER_DEFAULT : PROVIDER_GOOGLE}>
- { stopList.map((marker, index) => {
- return (
- <MyMarker
- key={index}
- id={marker.id}
- coordinate={marker}
- onPress={() => onMarkerPress(marker.id)}
- />
- );
- })}
- </MapView>
- : <VbeClusterMap
- ref={mapRef}
- style={StyleSheet.absoluteFill}
- region={region}
- data={stopList}
- animation={true}
- showCompass={false}
- showTraffic={false}
- showIndoors={false}
- showIndoorLevelPicker={false}
- onMapReady={onMapReady}
- onMarkerPress={e => onMarkerPress(e.id)}
- showUserLocation={showUserLocation}
- moveOnMarkerPress={true}
- />
- );
- }
|