使用谷歌地图搜索城市

时间:2017-03-15 10:15:38

标签: angularjs

我使用谷歌地图在查找搜索框(from that link)中显示位置正常工作:

rel="icon"

我想知道是否有可能在搜索条目附近显示我的自定义城市? 让我们说我有bbCity,aaCity,ccCity,当用户输入bb时,她/他获得最近的一个(bbCity)。 感谢

1 个答案:

答案 0 :(得分:0)

我不知道这是否正确,但我通过添加link的答案解决了这个问题。

angular.module('ui.bootstrap.demo').controller('TypeaheadCtrl',function($scope, $http) {

 // Predefined areas
    var predefinedLocations = [[48,68435270497298,4,39454410970211, 'city 1'],[48,33375503137806,5,616522543132305, 'city 2']];
$scope.getLocation = function(val) { 
    return $http.get('//maps.googleapis.com/maps/api/geocode/json', {
      params: {
                address: val,
                sensor: false
      }
    }).then(function(response){
        return response.data.results.map(function(item){
             // Hold distances
                var distances = [];

                    var start = new google.maps.LatLng(item.geometry.location.lat, item.geometry.location.lng);

                    for (var i = 0; i < predefinedLocations.length; i++)
                    {
                        var point = new google.maps.LatLng(predefinedLocations[i][0],predefinedLocations[i][1]);

                        var distance = google.maps.geometry.spherical.computeDistanceBetween(start, point);
                        distances.push(distance);
                    }

                    var firstElement = distances[0];
                var index = 0;
                for (var i = 1; i < distances.length; i++)
                {
                    if(distances[i] < firstElement)
                    {
                        firstElement = distances[i];
                        index = i;
                    }
                }

                    return predefinedLocations[index][2];
    });
  });
};