【问题标题】:Getting Place ID based on Place Name, City Name and Address from Google Places API service从 Google Places API 服务获取基于地点名称、城市名称和地址的地点 ID
【发布时间】:2016-09-23 01:32:33
【问题描述】:

我正在使用 Google Places API 服务 (https://developers.google.com/places/web-service/) 根据位置名称获取地点 ID。用户键入一些位置名称并出现相关建议,用户可以从中选择并获取地点 ID。我有一个要求,我需要三个文本框和一个按钮。用户将输入 Place Name、City Name 和 Address 并单击按钮以获取 placeId。现在,我没有看到基于多个参数获取地点 ID 的选项。我怎样才能做到这一点?

【问题讨论】:

    标签: api google-places-api


    【解决方案1】:

    Text Search Requests 接受查询字符串。一种可能的解决方案是允许您的用户在不同的文本字段中输入地名、城市和地址,然后在发送您的 ajax 请求之前将它们全部连接成一个查询字符串。

    您的表单如下所示:

    <form id="form">
      <input type="text" name="place" value="">
      <input type="text" name="city" value="">
      <input type="text" name="address" value="">
      <input type="submit" value="Locate">
    </form>
    

    您的 javascript 将如下所示:

    $( "#form" ).submit(function( event ) {
    
      // concatenate places into a single query string
      var query = $('input[name="place"]').val() + $('input[name="city"]').val() + $('input[name="address"]').val();
    
      // convert spaces to '+' symbol for query
      query = encodeURIComponent(query);
    
      // send ajax request
      $.ajax({url: "https://maps.googleapis.com/maps/api/place/textsearch/xml?query=" + query + "&key=YOUR_API_KEY", success: function(result){
            alert('success, now retrieve your id from result variable');
        }});
    
      //prevent the submit button from reloading the page
      event.preventDefault();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多