返回从字符串位置

时间:2017-07-28 11:47:04

标签: javascript angularjs google-maps

我的问题是如何获取现有标记的纬度和经度?让我解释一下:当我进入浏览器页面时,我使用地址显示标记位置:

  <ng-map ng-if="null != evaluation" default-style="false" zoom="16" center="{{ '{{ getAddress() }}' }}" style="width: 100%; height: 430px;">
                <marker centered="true" position="{{ '{{ getMarkerPosition() }}' }}" draggable="true" on-dragend="onMarkerDragEnd($event)"></marker>
  </ng-map>

角度部分:

  $scope.getMarkerPosition = function () {
            if (!$scope.address.mapPinLocationLatitude || !$scope.address.mapPinLocationLongitude) {
                return $scope.getAddress();
            }
            return [$scope.address.mapPinLocationLatitude, $scope.address.mapPinLocationLongitude]
        };


   $scope.getAddress = function () {
            //building the adress by getting the objects that are used for creating an address object

            return streetString + ', ' + address; //streetString will be a string that has the complete address
        };

    $scope.onMarkerDragEnd = function ($event) {
            $scope.address.mapPinLocationLatitude = $event.latLng.lat();
            $scope.address.mapPinLocationLongitude = $event.latLng.lng();

        uptadeRegions();
        };

如果我移动标记lat和log存在,方法getMarkerPosition返回一个带有2的数组。但如果我不在地图上移动标记,如何获得lat和long?默认值为0,我不知道如何解决它。谢谢,

编辑:

app.controller('MapController',
['$scope', 'NgMap','$state', 'SelectOptionsService', '$rootScope', '$q','GeoCoder',
    function ($scope, NgMap, $state, SelectOptionsService, $rootScope, $q, GeoCoder) {

1 个答案:

答案 0 :(得分:1)

所以你使用一个地址作为标记位置,你想得到lat&amp; lng在它初始化期间?您可以使用GeoCoder服务(由ngMap提供)

类似的东西:

app.controller('MapController',
['$scope', 'NgMap','$state', 'SelectOptionsService', '$rootScope', '$q','GeoCoder',
    function ($scope, NgMap, $state, SelectOptionsService, $rootScope, $q, GeoCoder) {
        //hired by marker:geo-callback
        $scope.geoCallback = function() {
            GeoCoder.geocode({
              address: $scope.getAddress()
            }).then(function(result) {
              //explore this, you may got the position in: result[0].geometry.location
              console.log('A place object is: ', result);
            });
        };
       ...
       //your getAddress function
       $scope.getAddress = function() {
       };
}]);

并在html中,在标记中,您可以使用geo-callback属性

<marker ... geo-callback="geoCallback()"></marker>

阅读geo-callback here