Angularjs 角度指令md自动完成与谷歌地图自动完成服务
提前感谢您的帮助 我一直在尝试设置一个自动完成搜索框,以便使用angular与google AutoCompleteServiceAPI交互 目前,我的Angular AutocompleteService运行良好,并根据搜索栏中键入的内容检索一系列建议的预测 这是我的自动完成服务:Angularjs 角度指令md自动完成与谷歌地图自动完成服务,angularjs,autocomplete,md-autocomplete,google-maps-autocomplete,Angularjs,Autocomplete,Md Autocomplete,Google Maps Autocomplete,提前感谢您的帮助 我一直在尝试设置一个自动完成搜索框,以便使用angular与google AutoCompleteServiceAPI交互 目前,我的Angular AutocompleteService运行良好,并根据搜索栏中键入的内容检索一系列建议的预测 这是我的自动完成服务: angular .module('LocalKnowledgeApp') .service('AutocompleteService', [ function() { var self = t
angular
.module('LocalKnowledgeApp')
.service('AutocompleteService', [ function() {
var self = this;
var suggestions = [];
self.initPredict = function(searchInput) {
var predict = new google.maps.places.AutocompleteService();
predict.getQueryPredictions({ input: searchInput }, self.displaySuggestions);
};
self.displaySuggestions = function(predictions, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
console.log(status);
return;
}
predictions.forEach(function(prediction) {
suggestions.push(prediction.description);
});
};
self.makeSuggestions = function(){
return suggestions.slice(0,10);
};
该服务被注入一个控制器,该控制器控制我试图连接到自动完成的表单。(我只包含了autocomplete位中涉及的并与AutocompleteService交互的功能)
这就是我的网页的样子
[
非常感谢您的帮助!Angular directive:
'use strict';
angular.module('locationApp', ['ngAnimate', 'ngMaterial']);
angular.module('locationApp')
.controller('HomeCtrl', function ($scope) {
// $scope.location={};
});
/**
* @ngdoc directive
* @name locationApp.directive:placeAutocomplete
* @description
*
* # An element directive that provides a dropdown of
* location suggestions based on the search string.
* When an item is selected, the location's latitude
* and longitude are determined.
*
* This directive depends on the Google Maps API
* with the places library
*
* <script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
*
* Usage:
* <place-autocomplete ng-model="selectedLocation"></place-autocomplete>
*
* Credit:
* http://stackoverflow.com/a/31510437/293847
*/
angular.module('locationApp')
.directive('placeAutocomplete', function () {
return {
templateUrl: 'place-autocomplete.html',
restrict: 'AEC',
replace: true,
scope: {
'ngModel': '='
},
controller: function ($scope, $q) {
if (!google || !google.maps) {
throw new Error('Google Maps JS library is not loaded!');
} else if (!google.maps.places) {
throw new Error('Google Maps JS library does not have the Places module');
}
console.log('google.maps.places ', google.maps.places);
var autocompleteService = new google.maps.places.AutocompleteService();
var map = new google.maps.Map(document.createElement('div'));
var placeService = new google.maps.places.PlacesService(map);
console.log('google.maps.places ', placeService);
$scope.ngModel = {};
/**
* @ngdoc function
* @name getResults
* @description
*
* Helper function that accepts an input string
* and fetches the relevant location suggestions
*
* This wraps the Google Places Autocomplete Api
* in a promise.
*
* Refer: https://developers.google.com/maps/documentation/javascript/places-autocomplete#place_autocomplete_service
*/
var getResults = function (address) {
var deferred = $q.defer();
console.log('address ', address, autocompleteService)
autocompleteService.getQueryPredictions({
input: address,
component: {
country: 'IN'
}
}, function (data) {
deferred.resolve(data);
});
return deferred.promise;
};
/**
* @ngdoc function
* @name getDetails
* @description
* Helper function that accepts a place and fetches
* more information about the place. This is necessary
* to determine the latitude and longitude of the place.
*
* This wraps the Google Places Details Api in a promise.
*
* Refer: https://developers.google.com/maps/documentation/javascript/places#place_details_requests
*/
var getDetails = function (place) {
var deferred = $q.defer();
placeService.getDetails({
'placeId': place.place_id
}, function (details) {
deferred.resolve(details);
});
return deferred.promise;
};
$scope.search = function (input) {
if (!input) {
return;
}
return getResults(input).then(function (places) {
return places;
});
};
/**
* @ngdoc function
* @name getLatLng
* @description
* Updates the scope ngModel variable with details of the selected place.
* The latitude, longitude and name of the place are made available.
*
* This function is called every time a location is selected from among
* the suggestions.
*/
$scope.getLatLng = function (place) {
if (!place) {
$scope.ngModel = {};
return;
}
getDetails(place).then(function (details) {
$scope.ngModel = {
'name': place.description,
'latitude': details.geometry.location.lat(),
'longitude': details.geometry.location.lng(),
};
});
}
}
};
});
“严格使用”;
角度模块('locationApp',['ngAnimate','ngMaterial']);
角度模块('locationApp')
.controller('HomeCtrl',函数($scope){
//$scope.location={};
});
/**
*@ngdoc指令
*@name locationApp.directive:placeAutocomplete
*@说明
*
*#一个元素指令,提供
*基于搜索字符串的位置建议。
*选择项目时,位置的纬度
*经度是确定的。
*
*此指令取决于谷歌地图API
*与地方图书馆
*
*
*
*用法:
*
*
*学分:
* http://stackoverflow.com/a/31510437/293847
*/
角度模块('locationApp')
.指令('placeAutocomplete',函数(){
返回{
templateUrl:“place autocomplete.html”,
限制:“AEC”,
替换:正确,
范围:{
“ngModel”:“=”
},
控制器:功能($scope,$q){
如果(!google | |!google.maps){
抛出新错误('Google Maps JS库未加载!');
}否则如果(!google.maps.places){
抛出新错误(“Google Maps JS库没有Places模块”);
}
log('google.maps.places',google.maps.places);
var autocompleteService=new google.maps.places.autocompleteService();
var map=newgoogle.maps.map(document.createElement('div');
var placeService=new google.maps.places.PlacesService(地图);
log('google.maps.places',placeService);
$scope.ngModel={};
/**
*@ngdoc函数
*@name getResults
*@说明
*
*接受输入字符串的Helper函数
*并获取相关的位置建议
*
*这包装了Google Places自动完成Api
*在承诺中。
*
*参考:https://developers.google.com/maps/documentation/javascript/places-autocomplete#place_autocomplete_service
*/
var getResults=函数(地址){
var deferred=$q.deferred();
console.log('地址',地址,自动完成服务)
autocompleteService.getQueryPredictions({
输入:地址,
组成部分:{
国家:'在'
}
},函数(数据){
延迟。解析(数据);
});
回报。承诺;
};
/**
*@ngdoc函数
*@name getDetails
*@说明
*接受位置并获取数据的帮助函数
*关于这个地方的更多信息。这是必要的
*确定该地点的纬度和经度。
*
*这将Google Places Details Api包装在承诺中。
*
*参考:https://developers.google.com/maps/documentation/javascript/places#place_details_requests
*/
var getDetails=函数(位置){
var deferred=$q.deferred();
placeService.getDetails({
“placeId”:place.place\u id
},功能(详情){
延期。解决(细节);
});
回报。承诺;
};
$scope.search=函数(输入){
如果(!输入){
返回;
}
返回getResults(输入)。然后返回(函数(位置){
返回地点;
});
};
/**
*@ngdoc函数
*@name getLatLng
*@说明
*使用所选位置的详细信息更新scope ngModel变量。
*提供了纬度、经度和地点名称。
*
*每次从中选择位置时,都会调用此函数
*这些建议。
*/
$scope.getLatLng=功能(位置){
如果(!地点){
$scope.ngModel={};
返回;
}
getDetails(位置)。然后(函数(细节){
$scope.ngModel={
“名称”:place.description,
“纬度”:details.geometry.location.lat(),
“经度”:details.geometry.location.lng(),
};
});
}
}
};
});
HTML部分:
<!DOCTYPE html>
<html ng-app="locationApp">
<head>
<!-- <link rel="stylesheet" href="place-autocomplete.css" /> -->
<link rel="stylesheet" href="//rawgit.com/angular/bower-material/master/angular-material.css" />
<script data-require="angular.js@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script>
<!-- <script data-require="angular-route.js@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular-route.js"></script> -->
<script data-require="angular-aria@1.4.0" data-semver="1.4.0" src="https://code.angularjs.org/1.4.0/angular-aria.js"></script>
<script data-require="angular-animate@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular-animate.js"></script>
<script src="//rawgit.com/angular/bower-material/master/angular-material.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<script src="place-autocomplete.js"></script>
</head>
<body>
<md-content>
<md-toolbar>
<div class="md-toolbar-tools">
<h2>
<span>Google places/location suggestions with md-autocomplete</span>
</h2>
<span flex></span>
</div>
</md-toolbar>
</md-content>
<md-content layout-padding>
<p>Start typing the name of a place/location below</p>
<div class="md-padding">
<place-autocomplete ng-model="location"></place-autocomplete>
<place-autocomplete ng-model="location1"></place-autocomplete>
<place-autocomplete ng-model="location2"></place-autocomplete>
<place-autocomplete ng-model="location3"></place-autocomplete>
{{location}}
{{location1}}
{{location2}}
{{location3}}
<script type="text/ng-template" id="place-autocomplete.html">
<div>
<md-autocomplete md-no-cache="false" md-selected-item="location" md-selected-item-change="getLatLng(item)" ng-model-options="{debounce: 600}" md-search-text-change="search(searchText)" md-search-text="searchText" md-items="item in search(searchText)" md-item-text="item.description" md-min-length="2" md-max-length="50" md-floating-label="Location (place, city or region)">
<md-item-template>
<span md-highlight-text="searchText" md-highlight-flags="^i">{{item.description}}</span>
</md-item-template>
<md-not-found>
No matches found for "{{searchText.description}}".
</md-not-found>
</md-autocomplete>
</div>
</script>
<div ng-if="location.name">
<h4>Selected location</h4>
<pre>{{location}}</pre>
</div>
</div>
</md-content>
</body>
</html>
使用md autocomplete搜索地点/位置建议
(function() {
'use strict';
angular
.module('LocalKnowledgeApp', ['ngResource', 'ngMaterial']);
}());
'use strict';
angular.module('locationApp', ['ngAnimate', 'ngMaterial']);
angular.module('locationApp')
.controller('HomeCtrl', function ($scope) {
// $scope.location={};
});
/**
* @ngdoc directive
* @name locationApp.directive:placeAutocomplete
* @description
*
* # An element directive that provides a dropdown of
* location suggestions based on the search string.
* When an item is selected, the location's latitude
* and longitude are determined.
*
* This directive depends on the Google Maps API
* with the places library
*
* <script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
*
* Usage:
* <place-autocomplete ng-model="selectedLocation"></place-autocomplete>
*
* Credit:
* http://stackoverflow.com/a/31510437/293847
*/
angular.module('locationApp')
.directive('placeAutocomplete', function () {
return {
templateUrl: 'place-autocomplete.html',
restrict: 'AEC',
replace: true,
scope: {
'ngModel': '='
},
controller: function ($scope, $q) {
if (!google || !google.maps) {
throw new Error('Google Maps JS library is not loaded!');
} else if (!google.maps.places) {
throw new Error('Google Maps JS library does not have the Places module');
}
console.log('google.maps.places ', google.maps.places);
var autocompleteService = new google.maps.places.AutocompleteService();
var map = new google.maps.Map(document.createElement('div'));
var placeService = new google.maps.places.PlacesService(map);
console.log('google.maps.places ', placeService);
$scope.ngModel = {};
/**
* @ngdoc function
* @name getResults
* @description
*
* Helper function that accepts an input string
* and fetches the relevant location suggestions
*
* This wraps the Google Places Autocomplete Api
* in a promise.
*
* Refer: https://developers.google.com/maps/documentation/javascript/places-autocomplete#place_autocomplete_service
*/
var getResults = function (address) {
var deferred = $q.defer();
console.log('address ', address, autocompleteService)
autocompleteService.getQueryPredictions({
input: address,
component: {
country: 'IN'
}
}, function (data) {
deferred.resolve(data);
});
return deferred.promise;
};
/**
* @ngdoc function
* @name getDetails
* @description
* Helper function that accepts a place and fetches
* more information about the place. This is necessary
* to determine the latitude and longitude of the place.
*
* This wraps the Google Places Details Api in a promise.
*
* Refer: https://developers.google.com/maps/documentation/javascript/places#place_details_requests
*/
var getDetails = function (place) {
var deferred = $q.defer();
placeService.getDetails({
'placeId': place.place_id
}, function (details) {
deferred.resolve(details);
});
return deferred.promise;
};
$scope.search = function (input) {
if (!input) {
return;
}
return getResults(input).then(function (places) {
return places;
});
};
/**
* @ngdoc function
* @name getLatLng
* @description
* Updates the scope ngModel variable with details of the selected place.
* The latitude, longitude and name of the place are made available.
*
* This function is called every time a location is selected from among
* the suggestions.
*/
$scope.getLatLng = function (place) {
if (!place) {
$scope.ngModel = {};
return;
}
getDetails(place).then(function (details) {
$scope.ngModel = {
'name': place.description,
'latitude': details.geometry.location.lat(),
'longitude': details.geometry.location.lng(),
};
});
}
}
};
});
<!DOCTYPE html>
<html ng-app="locationApp">
<head>
<!-- <link rel="stylesheet" href="place-autocomplete.css" /> -->
<link rel="stylesheet" href="//rawgit.com/angular/bower-material/master/angular-material.css" />
<script data-require="angular.js@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script>
<!-- <script data-require="angular-route.js@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular-route.js"></script> -->
<script data-require="angular-aria@1.4.0" data-semver="1.4.0" src="https://code.angularjs.org/1.4.0/angular-aria.js"></script>
<script data-require="angular-animate@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular-animate.js"></script>
<script src="//rawgit.com/angular/bower-material/master/angular-material.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<script src="place-autocomplete.js"></script>
</head>
<body>
<md-content>
<md-toolbar>
<div class="md-toolbar-tools">
<h2>
<span>Google places/location suggestions with md-autocomplete</span>
</h2>
<span flex></span>
</div>
</md-toolbar>
</md-content>
<md-content layout-padding>
<p>Start typing the name of a place/location below</p>
<div class="md-padding">
<place-autocomplete ng-model="location"></place-autocomplete>
<place-autocomplete ng-model="location1"></place-autocomplete>
<place-autocomplete ng-model="location2"></place-autocomplete>
<place-autocomplete ng-model="location3"></place-autocomplete>
{{location}}
{{location1}}
{{location2}}
{{location3}}
<script type="text/ng-template" id="place-autocomplete.html">
<div>
<md-autocomplete md-no-cache="false" md-selected-item="location" md-selected-item-change="getLatLng(item)" ng-model-options="{debounce: 600}" md-search-text-change="search(searchText)" md-search-text="searchText" md-items="item in search(searchText)" md-item-text="item.description" md-min-length="2" md-max-length="50" md-floating-label="Location (place, city or region)">
<md-item-template>
<span md-highlight-text="searchText" md-highlight-flags="^i">{{item.description}}</span>
</md-item-template>
<md-not-found>
No matches found for "{{searchText.description}}".
</md-not-found>
</md-autocomplete>
</div>
</script>
<div ng-if="location.name">
<h4>Selected location</h4>
<pre>{{location}}</pre>
</div>
</div>
</md-content>
</body>
</html>