我试图使用谷歌汽车完成获取清真寺的名称只在英国,我正在收到数据,但它是全球的,它不是清真寺, 以下是我的代码:

import axios from "axios";

export default async function GetMosques(req, res) {
  console.log(req.body.input);
  const input = req.body.input;
  const apiKey = "mykey"; // Replace with your actual API key
  const apiUrl = "https://maps.googleapis.com/maps/api/place/autocomplete/json";

  try {
    const response = await axios.get(apiUrl, {
      params: {
        input,
        key: apiKey,
        types: "mosque", // Set the type to "mosque" to filter results to mosques
        componentRestrictions: {
          country: "uk",
        },
      },
    });

    console.log("server:", response.data.predictions);
    

    const predictions = response.data.predictions.map((prediction) => ({
      id: prediction.place_id,
      name: prediction.description,
    }));

    res.json(predictions);
  } catch (error) {
    console.error("Error fetching autocomplete suggestions:", error.message);
    res.status(500).json({ error: "Internal Server Error" });
  }
}

我得到的数据如下:

{
    description: 'Sayedabad Bus Terminal Masjid, Dhaka - Mawa Hwy, Dhaka, Bangladesh',
    matched_substrings: [ [Object] ],
    place_id: 'ChIJubFsVbO5VTcRRpdyaZ6sXjE',
    reference: 'ChIJubFsVbO5VTcRRpdyaZ6sXjE',
    structured_formatting: {
      main_text: 'Sayedabad Bus Terminal Masjid',
      main_text_matched_substrings: [Array],
      secondary_text: 'Dhaka - Mawa Hwy, Dhaka, Bangladesh'
    },
    terms: [ [Object], [Object], [Object], [Object] ],
    types: [
      'mosque',
      'place_of_worship',
      'point_of_interest',
      'establishment'
    ]
  },
  {
    description: 'Sharjah Mosque - Sharjah - United Arab Emirates',
    matched_substrings: [ [Object] ],
    place_id: 'ChIJGb39ZXqL9T4ReU5jj0N0q6c',
    reference: 'ChIJGb39ZXqL9T4ReU5jj0N0q6c',
    structured_formatting: {
      main_text: 'Sharjah Mosque',
      main_text_matched_substrings: [Array],
      secondary_text: 'Sharjah - United Arab Emirates'
    },
    terms: [ [Object], [Object], [Object] ],
    types: [
      'mosque',
      'place_of_worship',
      'point_of_interest',
      'establishment'
    ]
  }

类型是正确的,因为它包括清真寺,但其他一切都很奇怪.清真寺的名字并不存在,数据来自其他国家

推荐答案

您使用的参数错误,以下是正确的版本:

const response = await axios.get(apiUrl, {
      params: {
        input: input,
        key: apiKey,
        types: "mosque", 
        components: "country:uk", // Set the country to the UK
      },
    });

Javascript相关问答推荐

无法在nextjs应用程序中通过id从mongoDB删除'

显示图—如何在图例项上添加删除线效果?

Chart.js-显示值应该在其中的引用区域

Nextjs 13.4 Next-Auth 4.2登录(&Quot;凭据&,{});不工作

获取';无法解决导入和导入";slick-carousel/slick/slick-theme.css";';错误

在SHINY R中的嵌套模块中,不能使用Java代码

无法读取未定义的属性(正在读取合并)-react RTK

是否可以将异步调用与useState(UnctionName)一起使用

如何组合Multer上传?

如何根据输入数量正确显示alert ?

MongoDB中的嵌套搜索

Clip-Path在网页浏览器(Mozilla、Edge、Chrome)上不能正常工作,但在预览版Visual Code Studio HTML、CSS、JS上却能很好地工作

重新呈现-react -筛选数据过多

我为什么要使用回调而不是等待?

如何在Web项目中同步语音合成和文本 colored颜色 更改

无法向甜甜圈图表上的ChartJSImage添加可见标签

如何使用useparams从react路由中提取id

我想为我的Reaction项目在画布上加载图像/视频,图像正在工作,但视频没有

JavaScript -如何跳过某个字符(S)来打乱字符串中的字符

如何格式化API的响应