Google地图自动完成材料设计

ald*_*122 7 javascript css google-maps google-maps-api-3 material-design

我有一个关于在材料设计中实施Google Maps自动完成功能的问题:

<md-autocomplete flex="" md-no-cache="ctrl.noCache" md-selected-item="ctrl.selectedItem" md-search-text="ctrl.searchText" md-items="item in ctrl.querySearch(ctrl.searchText)" md-item-text="item.display" md-floating-label="Favorite state">
    <span md-highlight-text="ctrl.searchText">{{item.display}}</span>
</md-autocomplete>
Run Code Online (Sandbox Code Playgroud)

角度材质自动完成 | Google地图自动填充功能

如何在谷歌地图自动完成(角度)中使用材料设计的自动完成?

提前致谢.

我找到了这个解决方案:我只覆盖了css proprties:

/*maps autocomplete*/
.pac-item{
    font-family:RobotoDraft,Roboto,'Helvetica Neue',sans-serif !important;
    font-weight: 300 !important;
    color: rgb(33,33,33) !important;
    line-height: 40px !important;
    /*font-weight: 800;*/
}

.pac-item-query{
    font-family:RobotoDraft,Roboto,'Helvetica Neue',sans-serif !important;
    font-size: 16px;
}

.pac-item:hover{
    background-color: #eeeeee !important;
    transition: background .15s linear;
}

.pac-container{
    color: rgb(33,33,33) !important;
    background-color: #fafafa;
    /*font-weight: 800;*/
}

.pac-icon, .pac-icon-marker{
    background: none !important;
    background-image: none !important;
}
Run Code Online (Sandbox Code Playgroud)

Leo*_*zes 11

我们对我们的应用程序有完全相同的要求,我们确实设法以"正确"的方式进行,因此您可以使用md-autocomplete指令(使用Angular Material)获得解决方案,利用Google 提供的AutocompleteService API.

视图:

<md-autocomplete md-no-cache="true"
                 md-selected-item="vm.selectedItem"
                 md-search-text-change="vm.search(vm.searchText)"
                 md-search-text="vm.searchText"
                 md-items="item in vm.search(vm.searchText)"
                 md-item-text="item"
                 md-min-length="0"
                 placeholder="Type your address">
  <md-item-template>
    <span md-highlight-text="vm.searchText" md-highlight-flags="^i">{{item}}</span>
  </md-item-template>
  <md-not-found>
    No matches found for "{{vm.searchText}}".
  </md-not-found>
</md-autocomplete>
Run Code Online (Sandbox Code Playgroud)

控制器:

vm.search = search;

function search(address) {
  var deferred = $q.defer();
  getResults(address).then(
    function (predictions) {
      var results = [];
      for (var i = 0, prediction; prediction = predictions[i]; i++) {
        results.push(prediction.description);
      }
      deferred.resolve(results);
    }
  );
 return deferred.promise;
}   

function getResults(address) {
  var deferred = $q.defer();
  vm.gmapsService.getQueryPredictions({input: address}, function (data) {
    deferred.resolve(data);
  });
  return deferred.promise;
}
Run Code Online (Sandbox Code Playgroud)

只需记住在控制器激活上创建服务:

vm.gmapsService = new google.maps.places.AutocompleteService();
Run Code Online (Sandbox Code Playgroud)

并在主应用程序文件中添加javascript依赖项:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KE??Y&libraries=places"></script>
Run Code Online (Sandbox Code Playgroud)

所有的vm.东西是因为我们使用John Papa Styleguide

希望这对你有用!


小智 0

是的,我认为你正在做某事。谷歌自动完成功能在文档的顶部(或底部?抱歉我不记得了)有自己的 DOMrendered 和绑定到它们的事件处理程序。

最简单的方法是覆盖 css 规则以满足您的要求。但如果这不起作用,您可以随时使用 google geocode API

https://developers.google.com/maps/documentation/geocoding/使用 Material design md-autocomplete 指令。您可以灵活地使用来自 Restful API 的 json 格式地理数据,并用它做任何您想做的事情。但是,代价是您失去了一些功能(例如,您无法根据当前位置对匹配进行排序,您必须使用区域或城市手动限制它们)。