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

[Bug]: Event handlers not called in web environment #3597

Open
Jaza opened this issue Aug 26, 2024 · 3 comments
Open

[Bug]: Event handlers not called in web environment #3597

Jaza opened this issue Aug 26, 2024 · 3 comments
Labels
bug 🪲 Something isn't working

Comments

@Jaza
Copy link

Jaza commented Aug 26, 2024

Mapbox Implementation

Mapbox

Mapbox Version

default

React Native Version

0.74.1

Platform

Android

@rnmapbox/maps version

10.1.28

Standalone component to reproduce

import React from 'react';
import { useState } from 'react';
import {
  MapState,
  MapView,
  Camera,
} from '@rnmapbox/maps';

const BugReportExample = () => {
  const [_mapState, setMapState] = useState<MapState>({
    properties: {
      center: [0, 0],
      bounds: {
        ne: [0, 0],
        sw: [0, 0],
      },
      zoom: 0,
      heading: 0,
      pitch: 0,
    },
    gestures: {
      isGestureActive: false,
    },
  });

  return (
    <MapView
      style={{flex: 1}}
      onCameraChanged={(_state) => {
        setMapState(_state);
        alert(`${_mapState.properties.center}`);
      }}
      onMapIdle={(_state) => {
        setMapState(_state);
        alert(`${_mapState.properties.center}`);
      }}
    >
      <Camera centerCoordinate={[-74.00597, 40.71427]} zoomLevel={14} />
    </MapView>
  );
};

Observed behavior and steps to reproduce

In Android (Expo app, development build - not Expo Go, not EAS - running in Android Emulator, or running on physical device via USB debugging / adb): alerts appear with correct map location on initial load and on pan / zoom, onCameraChanged / onMapIdle appear to be getting triggered.

In Expo Web: no alerts appear, onCameraChanged / onMapIdle appear to NOT be getting triggered.

Expected behavior

Should be the same in Expo Web as it is in Android, i.e. onCameraChanged / onMapIdle should get triggered.

Notes / preliminary analysis

No response

Additional links and references

Also reported (not by me) at: https://stackoverflow.com/questions/78625575/mapbox-from-rnmapbox-event-handlers-are-not-called-on-web

@ydrea
Copy link

ydrea commented Sep 28, 2024

+1

@Jaza
Copy link
Author

Jaza commented Jan 11, 2025

I ended up switching to maplibre-react-native instead, and falling back to react-map-gl with maplibre-gl-js for web. See my Expo MapLibre native + web demo for details.

@yuliswe
Copy link

yuliswe commented Jan 13, 2025

@Jaza
You can get the mapboxgl.Map instance from ref. From there, you can hook up anything you want from the mapbox-gl pacakge.

There's an example

import WebMapView from '@rnmapbox/maps/src/web/components/MapView';

  const mapRef = useRef<Mapbox.MapView>(null);

  useEffect(() => {
    const eventHandler = onCameraChangedWeb;
    if (Platform.OS === 'web' && mapRef.current) {
      const webMap = mapRef.current as unknown as WebMapView;
      const mapNode = webMap.map as mapboxgl.Map;
      mapNode.on('moveend', eventHandler);
    }
    return () => {
      if (Platform.OS === 'web' && mapRef.current) {
        const webMap = mapRef.current as unknown as WebMapView;
        const mapNode = webMap.map as mapboxgl.Map;
        mapNode.off('moveend', eventHandler);
      }
    };
  }, [mapRef.current]);

return <Mapbox.MapView
        ref={mapRef}
        ...
/>;

Read the source code in @rnmapbox/maps/src/web/components/MapView. It's pretty easy to understand how this pacakge is just a thin wrapper over mapbox-gl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🪲 Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants
@Jaza @yuliswe @ydrea and others