Skip to content

Can Not Use react.map.gl.gecoder with Typescript #99

@LAYTHJABBAR

Description

@LAYTHJABBAR

Hello Team,

I have tried a lot to use the gecoder with type script but it is keep showing errors my file is as below

import mapboxgl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';
import React, { useRef } from 'react';
import ReactMapGL, { Marker } from 'react-map-gl';
import Geocoder from 'react-map-gl-geocoder';
import 'react-map-gl-geocoder/dist/mapbox-gl-geocoder.css';
import DropoffPinWithLayer from '../../pages/Routing/Components/Pins/dropoffWithLayer';
import './map.scss';
import { IMap } from './type';

const Map = ({ 
  customerToMark, 
  customerToZoom, 
  isSearchOutOfMap, 
  itsDraggable, 
  geocoder, 
  handleOnDragEnd, 
  handleOnResult, 
  handleOnClear,
  handleOnClickEnd,
  handleViewportChange,
  height,
  marker, 
  position,
  radius,
  width,
  viewport,
}: IMap) => {
  let ZoomCustomer: React.ReactNode, MarkeAllCustomers: React.ReactNode, GeocoderTag: React.ReactNode, SearchBarRefrence: React.ReactNode;
  const geocoderContainerRef: any = useRef();
  const mapRef: any = useRef();
 
   if(isSearchOutOfMap === true) {
    SearchBarRefrence = (<div ref={geocoderContainerRef} />);
   }

  if (geocoder == true) {
    GeocoderTag = (
      <Geocoder
        containerRef={geocoderContainerRef}
        mapRef={mapRef}
        mapboxApiAccessToken={mapboxgl.accessToken}
        position={position}
        onResult={handleOnResult}
        ReverseGeoCode={true}
        onClear={handleOnClear}
      />
    );
  }
  if (customerToZoom == true) {
    ZoomCustomer = (
      <Marker latitude={customerToMark.lat} longitude={customerToMark.lon} >
        {marker}
      </Marker>
    );
  }
  if (customerToZoom == true && itsDraggable === true) {
     
    ZoomCustomer = (
      <DropoffPinWithLayer
        location={ customerToMark }
        onDragEnd={handleOnDragEnd}
        radius={radius}
      />
    );
  }

  return (
    <div>
      {SearchBarRefrence}
      <ReactMapGL
        {...viewport}
        ref={mapRef}
        width={width}
        onClick={(event) => {
          handleOnClickEnd(event)
        }}
        height={height}
        onViewportChange={handleViewportChange}
        mapboxApiAccessToken={mapboxgl.accessToken}
        mapStyle="mapbox://styles/mapbox/outdoors-v11"
      >
        {ZoomCustomer}
        {MarkeAllCustomers}
        {GeocoderTag}
      </ReactMapGL>
    </div>
  );
};

export default Map;

the Error messages that I am getting are TypeError: Cannot read property 'remove' of undefined && TypeError: Cannot read property 'addControl' of null

is there a way to fix that

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions