2017-03-15 3 views
1

を使用して都市を検索私は正常に動作している検索の検索ボックス(from that link)内の位置を表示するためにGoogleマップを使用しています:グーグルマップ

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

$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){ 
return item.formatted_address; 
}); 
}); 
}; 

<h4>Asynchronous results</h4> 
<pre>Model: {{asyncSelected | json}}</pre> 
<input type="text" ng-model="asyncSelected" placeholder="Locations loaded via $http" uib-typeahead="address for address in getLocation($viewValue)"  typeahead-loading="loadingLocations" typeahead-no-results="noResults" class="form-control"> 
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i> 
<div ng-show="noResults"> 
    <i class="glyphicon glyphicon-remove"></i> No Results Found 
</div> 

私は可能であれば私のカスタム都市を表示するために知っていただきたいと思います検索エントリの近くに? 私はbbCity、aaCity、ccCityを持っていて、ユーザがbbをタイプすると、彼女/彼は最も近いものを取得します(bbCity)。 ありがとう

答えて

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]; 
    }); 
    }); 
}; 
関連する問題