sanity-io / google-maps-input

Sanity plugin providing input handlers for geo-related input types using Google Maps
MIT License
9 stars 4 forks source link

Google API address data support #32

Open toddpadwick opened 1 year ago

toddpadwick commented 1 year ago

It would be great to support google address locations API for this.

We are developing a 'dealer' locator tool for one of our clients and they need to input their dealer's address and ensure the address data is returned in the map pin on google maps.

so a postcode/address search would be required in the sanity studio, and the data stored would need to be a full google maps location/address object from the API.

jmurphy-dev commented 1 year ago

I too am implementing a feature with a similar requirement, full access to the location/address object would be highly useful.

toddpadwick commented 1 year ago

Hi @summer-mute did you find an alternative solution for this? Not really sure how best to handle it. I'm surprised we're the first to request such support.

jess-small commented 7 months ago

@toddpadwick I used the google maps api geocode to take the lat and long and decode to get the full address. Not sure why this isnt implemented as part of the package already 🤷🏼‍♀️

`export async function POST(req: Request) {
  try {
    const body = await req.json();

    const { latitude, longitude } = body;

    if (!latitude || !longitude) {
      throw new Error('Latitude and longitude parameters are required.');
    }

    const response = await fetch(
      `https://maps.googleapis.com/maps/api/geocode/json?latlng=${latitude},${longitude}&key=${googleMapsApiKey}`,
    );
    const data: GeocodeResponse = await response.json();

    if (data.status === 'OK' && data.results && data.results.length > 0) {
      return NextResponse.json({
        status: 200,
        address: data.results[0].formatted_address,
        fullAddress: createStructuredAddress(
          data.results[0].address_components,
        ),
      });
    } else {
      throw new Error('Address not found');
    }
  } catch (error) {
    console.error('Error fetching address:', error);
  }
}`