我花了好几个小时来研究如何将 google 地理定位服务与 Angular 模块 angular-google-maps 结合使用。使用这个模块让它变得很奇怪,并且不能像普通的谷歌地图 javascript v3 那样进行地理定位。
例如:
var geocoder = new google.maps.Geocoder();
Google 未定义..
这是我到目前为止的工作:
我的 angular.module:
...'uiGmapgoogle-maps'])
.config(['uiGmapGoogleMapApiProvider', function (GoogleMapApi) {
GoogleMapApi.configure({
key: 'X',
v: '3.20',
libraries: 'weather,geometry,visualization'
});
}]);
我的 Angular Controller :
.controller(...., function (.., uiGmapGoogleMapApi, GoogleMapApi){
//Map setup stuff
var geocoder = new GoogleMapApi.Geocoder();
geocoder.geocode( { 'address': 'something'}, function(results, status) {
if (status == GoogleMapApi.google.maps.GeocoderStatus.OK) {
$scope.map.control.getGMap().setCenter(results[0].geometry.location);
$scope.map.marker.latitude = results[0].geometry.location.lat();
$scope.map.marker.longitude = results[0].geometry.location.lng();
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
表示 Geocoder 未定义。 非常困惑要使用什么对象,uiGmapGoogleMapApi 或 GoogleMapApi 来引用 Geocoder()。 geocoder.geocode.. 应该如何设置? 请帮忙
最佳答案
意识到我可以通过使用 http 请求来避免所有问题:
$http.get('https://maps.googleapis.com/maps/api/geocode/json?address=' +
address + '&key=X')
.then(function(coord_results){
$scope.queryResults = coord_results.data.results;
$scope.geodata = $scope.queryResults[0].geometry;
},
function error(_error){
$scope.queryError = _error;
});
如果没有人确实知道如何解决上述问题,请仍然留下帖子。
关于javascript - 如何使用 angular-google-maps 进行地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32452757/