简体   繁体   English

Google地图标记未在确切位置显示

[英]Google map marker is not showing in exact location

In my project, I have integrated google map. 在我的项目中,我已经集成了Google地图。 I am having one problem : when I search for a particular location, the marker is not displaying at the current point, it is showing somewhere else. 我遇到一个问题:搜索特定位置时,标记未显示在当前点,而是显示在其他位置。 However, latitude and longitude are correct, could you help me to understand what I am missing ? 但是,经度和纬度是正确的,您能帮我了解我所缺少的吗?

Note: For some locations, the marker is displayed on the correct location. 注意: 对于某些位置,标记会显示在正确的位置。

Html code : 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>

Controller: 控制器:

$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();
    }
});

According to Ng-map-autocomplete doc, {{address}} is only the value used for the autocomplete. 根据Ng-map-autocomplete文档,{{address}}仅是用于自动完成的值。 You can attach a "detail" value which will contain the lat and long of your location. 您可以附加一个“详细信息”值,其中包含您所在位置的经度和纬度。

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

Then, in your ng-map directive, you can access the lat and long 然后,在ng-map指令中,您可以访问经纬度

<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>

If you want to update in your controller, you can put a $watcher on details 如果要在控制器中进行更新,可以在详细信息上放置一个$ watcher

$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;
});

then you can access the position in the ng-map directive with your new variables. 那么您可以使用新变量访问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>

Hope it helps. 希望能帮助到你。 Source: https://github.com/iazi/ngMapAutocomplete 来源: https : //github.com/iazi/ngMapAutocomplete

Ps: Code not tested, I merely try to recreate what I did month ago 附:代码未经测试,我只是尝试重新创建一个月前所做的工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM