2013-08-17 190 views
2

我正在使用Google Maps API v3位置(使用自动完成服务)搜索位置的项目。自动完成自定义结果?

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

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

回答

3

它可能对你来说有点晚,但我偶然发现了你的问题,然后才决定在我维护的Google Maps Autocomplete component for Angular JS中实现这个功能。希望有人发现它有帮助。

使用该组件,您可以指定自定义位置结果,该结果将混合到自动完成服务返回的结果中。

这里有一个工作示例:

<!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属性连接最多的指令。

+0

如何将您的图书馆与http://angular-ui.github.io/angular-google-maps – TWilly