Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Map Panning issue #32

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions src/MapView/MapView.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
import {forwardRef, useEffect, useImperativeHandle, useMemo, useRef} from 'react';
import Mapbox, {MarkerView} from '@rnmapbox/maps';
import {forwardRef, useEffect, useImperativeHandle, useRef, useState} from 'react';
import Mapbox, {MapState, MarkerView} from '@rnmapbox/maps';
import {View} from 'react-native';
import {MapViewProps, MapViewHandle} from './MapViewTypes';
import Direction from './Direction';
import Utils from './utils';

const MapView = forwardRef<MapViewHandle, MapViewProps>(function MapView(
{accessToken, style, styleURL, pitchEnabled, mapPadding, initialState, waypoints, directionCoordinates, directionStyle, logoEnabled},
{accessToken, style, styleURL, pitchEnabled, mapPadding, initialState, waypoints, directionCoordinates, directionStyle, isFocused, logoEnabled},
ref,
) {
const cameraRef = useRef<Mapbox.Camera>(null);
const [isIdle, setIsIdle] = useState(false);

const bounds = useMemo(() => {
if (!waypoints || waypoints.length === 0) {
return undefined;
}
useEffect(() => {
if (isFocused) return;
setIsIdle(false);
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
}, [isFocused]);

useEffect(() => {
if (!waypoints || !waypoints.length || !isIdle || !isFocused) return;

if (waypoints.length === 1) {
cameraRef.current?.flyTo(waypoints[0].coordinate);
cameraRef.current?.zoomTo(15);
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
return undefined;
cameraRef.current?.setCamera({
zoomLevel: 15,
centerCoordinate: waypoints[0].coordinate,
});
} else {
const {southWest, northEast} = Utils.getBounds(waypoints.map((waypoint) => waypoint.coordinate));
cameraRef.current?.fitBounds(northEast, southWest, mapPadding, 1000);
}
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved

const {southWest, northEast} = Utils.getBounds(waypoints.map((waypoint) => waypoint.coordinate));
return {
ne: northEast,
sw: southWest,
paddingTop: mapPadding,
paddingRight: mapPadding,
paddingBottom: mapPadding,
paddingLeft: mapPadding,
};
}, [waypoints]);
}, [mapPadding, waypoints, isFocused, isIdle]);

useImperativeHandle(
ref,
Expand All @@ -41,6 +39,11 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(function MapView(
[],
);

const setMapIdle = (e: MapState) => {
if (e.gestures.isGestureActive) return;
setIsIdle(true);
}

// Initialize Mapbox on first mount
useEffect(() => {
Mapbox.setAccessToken(accessToken);
Expand All @@ -52,6 +55,7 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(function MapView(
styleURL={styleURL}
pitchEnabled={pitchEnabled}
style={{flex: 1}}
onMapIdle={setMapIdle}
logoEnabled={logoEnabled}
>
<Mapbox.Camera
Expand All @@ -60,7 +64,6 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(function MapView(
centerCoordinate: initialState?.location,
zoomLevel: initialState?.zoom,
}}
bounds={bounds}
/>
{waypoints &&
waypoints.map(({coordinate, markerComponent: MarkerComponent}) => (
Expand Down
2 changes: 1 addition & 1 deletion src/MapView/MapView.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'mapbox-gl/dist/mapbox-gl.css';
import Direction from './Direction';
import {DEFAULT_INITIAL_STATE} from './CONST';

const MapView = forwardRef<MapViewHandle, MapViewProps>(function MapView(
const MapView = forwardRef<MapViewHandle, Omit<MapViewProps, "isFocused">>(function MapView(
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
{accessToken, waypoints, style, mapPadding, directionCoordinates, initialState = DEFAULT_INITIAL_STATE, styleURL, directionStyle},
ref,
) {
Expand Down
2 changes: 2 additions & 0 deletions src/MapView/MapViewTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export type MapViewProps = {
accessToken: string;
// Style applied to MapView component. Note some of the View Style props are not available on ViewMap
style: StyleProp<ViewStyle>;
// Focus state of the screen
isFocused: boolean
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
// Link to the style JSON document.
styleURL?: string;
// Whether map can tilt in the vertical direction.
Expand Down
Loading