我可以在Google Places API调用中使用格式化地址

时间:2017-01-25 20:40:19

标签: javascript google-maps google-places-api

我希望在我的应用程序中提供特定功能。我希望能够输入地址,或使用存储在数据库中的地址,例如“119 Harley Street,London”或“119 Harley Street”(这只是一个随机地址,用于说明我对Google Places'附近'领域有关“伦敦诊所眼科中心”业务的要求。)

根据输入的地址,我希望能够在此地址识别商家名称(如果有),以及地点JSON结果中的“名称”字段,并确定是否有最近的评论客户,日期/时间和评论本身,照片等。

这是完全可能的,还是我在找一些不可能的东西?我有使用其他Google API(Geolocation& JS)的经验,但对我来说这是一个新的,并且无法从文档中看到任何实现这一点的方法 - 我希望我遗漏了一些东西。

有人可以帮忙吗?另外一种实现我需要的方式也将非常感激。

非常感谢提前。

2 个答案:

答案 0 :(得分:1)

您希望在Google地图(example)中找到类似此位置的内容:

enter image description here

在Places API中,这种方式并不完全相同,但我认为您可以使用Nearby Search非常接近:

  1. 使用地方自动填充(或地理编码)获取地址(或商家,无关紧要)的latlng。
  2. 使用“附近的位置搜索”查找该位置的所有商家信息,方法是使用该文件查询location,不keywordrankby=distance
  3. 仅保留最接近的结果(前几个)和/或(可能)按地址过滤。
  4. 附近的示例搜索“Bahnhofstrasse 30 Zurich”的请求,该地址编码为47.3705904,8.5391694:

    https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=47.3705904,8.5391694&rankby=distance&key=YOUR_API_KEY

    结果(姓名,附近):

    1. Griederbar,Bahnhofstrasse 30,Zürich
    2. Coiffure Valentino,Bahnhofstrasse 30,Zürich
    3. Grieder G.Point,Bahnhofstrasse 30,Zürich
    4. Shanty Beauty Design,Bahnhofstrasse 30,Zürich
    5. Louis Vuitton,Bahnhofstrasse 30,Zürich
    6. Brunschwig& Cie Sa,Bahnhofstrasse 30,Zürich
    7. Clariden Leu,班霍夫大街 32 ,苏黎世
    8. Gen Del Sa,班霍夫大街 32 ,苏黎世
    9. Abegg& Co AG,Zürich,Bahnhofstrasse 30,Zürich
    10. Wohlfahrtsfonds der Clariden Leu AG,班霍夫大街 32 ,苏黎世
    11. UhrenshopZürichJaeger-LeCoultre,班霍夫大街 32 ,苏黎世
    12. TOD'S Boutique,班霍夫大街 32 ,苏黎世
    13. Audemars Piguet,苏黎世精品店,班霍夫大街 32 ,苏黎世
    14. Grieder,Bahnhofstrasse 30,Zürich
    15. ─20。没有更多结果在Bahnhofstrasse 30,Zürich

答案 1 :(得分:0)

这是一个直接来自Google的小片段,向您展示如何使用“预测”API获取建议的地点列表:

function initCallback() {
    var getDetailsCallback = function(place, status) {
        if (status != google.maps.places.PlacesServiceStatus.OK) {
            alert(status);
            return;
        }
	    // write details of place to console
        console.log('Place');
        console.log(place);
    };
    var getQueryPredictionsCallback = function(predictions, status) {
        if (status != google.maps.places.PlacesServiceStatus.OK) {
            alert(status);
            return;
        }
        // write predictions to the console
        console.log('Predictions');
        console.log(predictions);

        // get details of each prediction
        var map = new google.maps.Map(document.createElement('div'));
        var placesService = new google.maps.places.PlacesService(map);
        predictions.forEach(function(prediction) {
            placesService.getDetails({ placeId: prediction.place_id }, getDetailsCallback);  
        });
    };

    var autocompleteService = new google.maps.places.AutocompleteService();
    autocompleteService.getQueryPredictions({ input: '119 Harley Street' }, getQueryPredictionsCallback);
}
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?&libraries=places&callback=initCallback" async defer></script>