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

Too many suggestions breaks my website #1143

Open
KanoDekouBillyBrown opened this issue Jun 18, 2024 · 0 comments
Open

Too many suggestions breaks my website #1143

KanoDekouBillyBrown opened this issue Jun 18, 2024 · 0 comments

Comments

@KanoDekouBillyBrown
Copy link

Coding Questions

When i start typing into the input field i get soo many suggestions because it is not limited to the area around the user so much so that my app breaks and freezes eventually it just crashes and i need to hard restart the server , I tried using locationBias inorder to limit the range of suggesstion to 5km around the user but it doesn't work

Here i am currently getting the users location

import { useState } from 'react';



const useCurrentLocation = () => {
    const [location, setLocation] = useState<GeolocationPosition | null>(null);
    const [error, setError] = useState<string | null>(null);

    const requestLocation = () => {
        if (!navigator.geolocation) {
            setError('Geolocation is not supported by your browser');
            return;
        }

        navigator.geolocation.getCurrentPosition(
            (position) => {
                setLocation(position);
            },
            (err) => {
                setError(err.message);
            }
        );
    };

    return { location, error, requestLocation };
};

export default useCurrentLocation;

Here i use it in my modal form to population locationBias with lat and Lng

 const { isOpen, onClose } = useDialog()
  const [isLoading, setIsLoading] = useState(false)
  const { location, error, requestLocation } = useCurrentLocation()
  const [bounds, setBounds] = useState<google.maps.LatLngBounds | undefined>(
    undefined
  )
  const [locationBias, setLocationBias] = useState<
    google.maps.LatLngLiteral | undefined
  >(undefined)

  useEffect(() => {
    if (location) {
      const { latitude, longitude } = location.coords // Ensure proper typing here
      setLocationBias({ lat: latitude, lng: longitude })
    }
  }, [location])

Here i pass it in the usePlacesHook Instance

const {
  ready,
  value,
  setValue,
  suggestions: { status, data },
  clearSuggestions
} = usePlacesAutocomplete({
  callbackName: 'mapApi',

  requestOptions: {
    types: ['sublocality_level_1', 'route'],
    componentRestrictions: { country: 'CM' },
    locationBias: locationBias
      ? new google.maps.Circle({
          center: locationBias,
          radius: 5000 // 5 km radius
        })
      : undefined
  }
})

This is the part of my form that handles geocoding

 <Input
          value={value}
          onChange={e => {
            setValue(e.target.value)
          }}
          disabled={!ready}
          onFocus={requestLocation}
          className={'py-2'}
          placeholder={'Enter Address'}
        />
        {status === 'OK' && (
          <ul
            className={
              'relative bottom-0  w-full gap-2 rounded-xl border bg-white p-4'
            }
          >
            {data?.map(suggestion => {
              const {
                place_id,
                structured_formatting: { main_text, secondary_text }
              } = suggestion
              console.log(suggestion)
              const address = `${main_text}, ${secondary_text}`
              setFormValue('address', address)
              trigger('address')
              return (
                <li
                  key={place_id}
                  className={'cursor-pointer  border-b p-2'}
                  onClick={handleSelect(suggestion)}
                >
                  <strong>{main_text} </strong>
                  <small>{secondary_text}</small>
                </li>
              )
            })}
          </ul>
        )}
        {error && <p>{error}</p>}
        ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant