自动完成自定义结果?

时间:2013-08-17 15:28:24

标签: google-maps-api-3 autocomplete

我正在开展一个项目,我正在使用Google Maps API v3搜索位置(使用自动完成服务)。

它运行良好,允许您从许多不同的国家/地区进行搜索,我希望如何在结果窗格中添加一些自定义结果(例如“Mikes place”)(如果有人开始输入“Mike”)。

是否可以将自己的搜索结果添加到Google地图搜索结果中,或者我应该使用某些jQuery自动完成功能并尝试使用我自己添加的Google搜索结果?

1 个答案:

答案 0 :(得分:3)

对你而言可能有点迟了但我在你决定自己实施这个问题之前偶然发现了我自己在Google Maps Autocomplete component for Angular JS中实施的问题。希望有人发现它有用。

使用该组件,您可以指定自定义位置结果,这些结果会混合到从AutocompleteService返回的结果中。

这是一个有效的例子:

<!DOCTYPE html>
<html lang="en" ng-app="example">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Injecting Custom Place Predictions</title>

    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="../src/autocomplete.css">

    <!-- Required dependencies -->
    <script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
    <script src="lib/underscore/underscore.js"></script>
    <script src="lib/angular/angular.js"></script>
    <script src="lib/angular-touch/angular-touch.js"></script>

    <!-- Google Places Autocomplete directive -->
    <script src="../src/autocomplete.js"></script>

    <script>
        angular.module('example', ['google.places'])

                // Setup a basic controller
                .controller('MainCtrl', function ($scope) {
                    $scope.place = null;

                    $scope.myPlaces = [
                        toGooglePlacesResult({
                            label: 'International Airport - T1, Sydney, New South Wales',
                            address: {
                                street: 'International Airport - T1',
                                suburb: 'Sydney',
                                state: 'NSW'
                            },
                            location: { latitude: -33.936722, longitude: 151.164266 }
                        }),
                        toGooglePlacesResult({
                            label: 'Domestic Airport - T2, Sydney, New South Wales',
                            address: {
                                street: 'Domestic Airport - T2',
                                suburb: 'Sydney',
                                state: 'NSW'
                            },
                            location: { latitude: -33.933617, longitude: 151.181630 }
                        }),
                        toGooglePlacesResult({
                            label: 'Domestic Airport - T3, Sydney, New South Wales',
                            address: {
                                street: 'Domestic Airport - T3',
                                suburb: 'Sydney',
                                state: 'NSW'
                            },
                            location: { latitude: -33.933076, longitude: 151.181270 }
                        })
                    ];

                    function toGooglePlacesResult(config) {
                        return {
                            formatted_address: config.label,
                            address_components: [
                                {
                                    long_name: config.address.street,
                                    short_name : config.address.street,
                                    types: [ 'route' ]
                                },
                                {
                                    long_name: config.address.suburb,
                                    short_name: config.address.suburb,
                                    types: [ 'locality' ]
                                },
                                {
                                    long_name: config.address.state,
                                    short_name: config.address.state,
                                    types: [ 'administrative_area_level_1' ]
                                }
                            ],
                            geometry: {
                                location: {
                                    lat: function () { return config.location.latitude },
                                    lng: function () { return config.location.longitude }
                                }
                            }
                        };
                    }
                });
    </script>
</head>
<body ng-controller="MainCtrl">
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h1>Injecting Custom Place Predictions</h1>

            <form class="form">
                <input class="form-control" g-places-autocomplete custom-places="myPlaces" ng-model="place"/>
            </form>

            <h5>Result:</h5>
            <pre>{{place | json}}</pre>
        </div>
    </div>
</div>
</body>
</html>

重要的部分是确保您的自定义位置结果实际上看起来与实际位置结果最低,然后使用custom-places属性将结果连接到指令。