简体   繁体   English

Google地图自动完成材料设计

[英]Google Maps autoComplete with material design

I have a question about implementing Google Maps auto complete function in 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>

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

How can I use material design's auto complete in google maps auto complete (angular)? 如何在谷歌地图自动完成(角度)中使用材料设计的自动完成?

Thanks in advance. 提前致谢。

I've found this solution: i overwrite only the css proprties: 我找到了这个解决方案:我只覆盖了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;
}

We had the exact same requirement for our app, and we did manage to do it the "proper" way, so here you have the solution with the md-autocomplete directive (using Angular Material ), taking advantage of the AutocompleteService API from Google. 我们对我们的应用程序有完全相同的要求,我们确实设法以“正确”的方式进行,因此您可以使用md-autocomplete指令(使用Angular Material )获得解决方案,利用Google提供的AutocompleteService API

View: 视图:

<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>

Controller: 控制器:

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;
}

Just remember to create the service on your controller activation: 只需记住在控制器激活上创建服务:

vm.gmapsService = new google.maps.places.AutocompleteService();

And to add the javascript dependency on your main app file: 并在主应用程序文件中添加javascript依赖项:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KE‌​Y&libraries=places"></script>

All the vm. 所有的vm。 stuff is because we use the John Papa Styleguide 东西是因为我们使用John Papa Styleguide

Hope it works for you! 希望这对你有用!

Yes I think you are onto something. 是的,我认为你正在做点什么。 The google auto-completion has it's own DOMrendered on the top (or bottom? sorry I cant remenber) of the document and event handlers binding to them. 谷歌自动完成它自己的DOMrendered在绑定到它们的文档和事件处理程序的顶部(或底部?抱歉,我不能重复)。

The easy way out is overwrite the css rules to meet your requirements. 简单的方法是覆盖css规则以满足您的要求。 But if that doesn't workout, you can always use google geocode API 但如果不进行锻炼,您可以随时使用谷歌地理编码API

https://developers.google.com/maps/documentation/geocoding/ to work with Material design md-autocomplete directive. https://developers.google.com/maps/documentation/geocoding/使用Material design md-autocomplete指令。 You get flexibility to work with just json format geo data from restful API and do whatever you want with it., But the trade off is you lose some capability (eg you cannot sort the match based on your current place, you have to use region or city to manually restrict them). 您可以灵活地使用来自restful API的json格式地理数据并随心所欲地执行任何操作。但是权衡的是您失去了一些功能(例如,您无法根据当前位置对匹配进行排序,您必须使用区域或城市手动限制它们)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM