2017-01-02 113 views
0

在我的项目中,我整合了谷歌地图。我遇到了一个问题:当我搜索某个特定位置时,标记不会显示在当前位置,而是显示在其他位置。但是,经纬度是正确的,你能帮我理解我失踪的是什么吗?谷歌地图标记没有显示在确切的位置

注意:对于某些位置,标记显示在正确的位置。

HTML代码:

<input ng-model="address" class="form-control" ng-map-autocomplete/> 
<ng-map zoom="18" center="{{address}}" style="width:650px; height:450px"> 
    <marker position="{{address}}" title="{{address}}" draggable="true"></marker> 
</ng-map> 

控制器:

$scope.$watch(function ($scope) { return $scope.chosenPlaceDetails }, function() { 
    if (angular.isDefined($scope.chosenPlaceDetails)) { 
     $scope.latitude= $scope.chosenPlaceDetails.geometry.location.lat(); 
     $scope.longitude=$scope.chosenPlaceDetails.geometry.location.lng(); 
    } 
}); 

回答

0

根据伍-MAP-自动完成文档,{{地址}}是仅用于自动填充的值。您可以添加一个“详细”值,其中包含您的位置的经纬度。

<input type="text" ng-map-autocomplete ng-model="autocomplete" options="options" details="details"/> 

然后,在你的NG-MAP指令,你可以访问细节lat和长

<ng-map zoom="18" center="{{details.geometry.location.lat}},{{details.geometry.location.lng}}" style="width:650px; height:450px"> 
    <marker position="{{details.geometry.location.lat}},{{details.geometry.location.lng}}" title="{{address}}" draggable="true"></marker> 
</ng-map> 

如果你想在你的控制器进行更新,你可以把$观察家

$scope.$watch('details', function (newValue, oldValue) { 
if (oldValue == undefined){ 
    $scope.latitude = 0 /*default value */ 
    $scope.longitude= 0 /*default value */ 
} 
$scope.latitude= newValue.geometry.location.lat; 
$scope.longitude=newValue.geometry.location.lng; 
}); 

然后你可以用你的新变量访问ng-map指令中的位置。

<ng-map zoom="18" center="{{latitude}},{{longitude}}" style="width:650px; height:450px"> 
    <marker position="{{latitude}},{{longitude}}" title="{{address}}" draggable="true"></marker> 
</ng-map> 

希望它有帮助。 来源:https://github.com/iazi/ngMapAutocomplete

PS:代码没有测试过,我只是试图重建我所做的一个月前

+0

如何更改控制器中的代码,然后.. –

+0

如果你想还以更新控制器,也许把$ details。$ watch('details',function(newValue,oldValue){ $ scope.latitude = newValue.geometry.location.lat; $ scope.longitude = newValue.geometry .location.lng } }); ' – Roux

+0

Hi Roux在我的控制器中,我给出了这样的初始值:\t $ scope.address =“12180 Park Ave S,Tacoma,WA 98447,USA”; \t $ scope.lat =“47.146427232895235”; \t $ scope.lng =“ - 122.4420750597717”; –