spinkus
spinkus

Reputation: 8560

Weird response from google maps javascript places API searchByText

I'm using the Maps JavaScript API Text Search (New) searchByText() function to get a list of places in a web app. It's supposed to return google.maps.places.Place[] according to the @types/google.maps typings, and according to the documentation linked above. But I get a weird object back with intermediate keys Eg, Fg, Hg, Ig, Jg, Kg, Lg, Mg, Ng, Og, Pg, Qg, Rg, Sg, Tg, Ug, Vg with the actual response Place object in the Eg key and some other duplicate crap in some of the other keys (mostly undefined though). What's going on? The app is Vue and this result was obtained while running it via vite dev server. Haven't tested any other build.

Code:

  const loader = new Loader({
    apiKey,
    version: '3.58'
  });
  const places: google.maps.PlacesLibrary = await loader.importLibrary('places');
  const request = {
    includedType: 'locality',
    textQuery: 'Bogota',
    fields: ['location', 'formattedAddress', 'types', 'id']
  }
  const result = await places.Place.searchByText(request);
  console.log(result);

Gives (undefined keys omitted):

{
  "places": [
    {
      "id": "ChIJQycDzk1B1moR7LsQHWG_MKs",
      "Eg": {
        "id": "ChIJQycDzk1B1moR7LsQHWG_MKs",
        "location": {...},
        "formattedAddress": "...",
        "types": [
          "doctor",
          "point_of_interest",
          "health",
          "establishment"
        ]
      },
      "Ng": {},
      "Ug": [
        "doctor",
        "point_of_interest",
        "health",
        "establishment"
      ],
      "Fg": {}
    }
  ]
}

Upvotes: 0

Views: 62

Answers (1)

Sudhanshu_007
Sudhanshu_007

Reputation: 1

I think u can extract meaningful data from responses manually like:

const sanitizePlaces = (response: any) => {
  return response.places.map((place: any) => ({
    id: place.Eg?.id,
    location: place.Eg?.location,
    formattedAddress: place.Eg?.formattedAddress,
    types: place.Eg?.types || []
  }));
};


const sanitized = sanitizePlaces(result);
console.log(sanitized);

I think you should once run in development server like vite dev which might expose the problems that occurs in production.

const request = {
  includedType: 'locality',
  textQuery: 'Bogota',
  fields: ['geometry.location', 'formatted_address', 'types', 'place_id'] 
};

Upvotes: 0

Related Questions