【问题标题】:AngularJs- TypeError: Cannot read property 'latitude' of undefinedAngularJs-TypeError:无法读取未定义的属性“纬度”
【发布时间】:2016-09-05 06:29:22
【问题描述】:

我想在我的页面中显示带有可拖动标记的 Google 地图。这是我的代码:

header.php:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&sensor=true"></script>

ma​​p.js:

    app.directive('mapDirective',function(){
    return {
        templateUrl: 'map.html',
        restrict: 'EA',
        require: '?ngModel',
        scope:{
            myModel: '=ngModel'
        },
        link: function(scope , element, attrs , ngModel){
            var mapOptions;
            var googleMap;
            var searchMarker;
            var searchLatLng;
            scope.searchLocation = {
                latitude: 48.137273,
                longitude: 11.575251
            };
            ngModel.$render = function(){
                console.log("hhh");
                searchLatLng = new google.maps.LatLng(scope.myModel.latitude, scope.myModel.longitude);

                mapOptions = {
                    center: searchLatLng,
                    zoom: 12,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                googleMap = new google.maps.Map(element[0],mapOptions);

                searchMarker = new google.maps.Marker({
                    position: searchLatLng,
                    map: googleMap,
                    draggable: true
                });

                google.maps.event.addListener(searchMarker, 'dragend', function(){
                    scope.$apply(function(){
                        scope.myModel.latitude = searchMarker.getPosition().lat();
                        scope.myModel.longitude = searchMarker.getPosition().lng();
                    });
                }.bind(this));

            };

            scope.$watch('myModel', function(value){
                var myPosition = new google.maps.LatLng(scope.myModel.latitude, scope.myModel.longitude);
                searchMarker.setPosition(myPosition);
            }, true);
        }
    }
});

map.html:

<div>
    <div style="display: block; height: 200px; width: 100%; ">
    </div>
</div>

index.html:

<map-directive ng-model="testModel"></map-directive>

实际上,我得到了这个错误:

TypeError: Cannot read property 'latitude' of undefined

如何解决?

已编辑: 我更改了我的 ma​​ps.js

    app.directive('mapDirective',function(){
    return {
        templateUrl: '/app/user/ngApp/templates/libsView/templates/directives/map.html',
        restrict: 'EA',
        require: '?ngModel',
        scope:{
            myModel: '=ngModel'
        },
        controller: function ($scope) {
            $scope.searchLocation = {
                latitude: 48.137273,
                longitude: 11.575251
            };
        },
        link: function(scope , element, attrs , ngModel){

            var mapOptions;
            var googleMap;
            var searchMarker;
            var searchLatLng;

            ngModel.$render = function(){
                console.log("hhh");
                searchLatLng = new google.maps.LatLng(scope.myModel.latitude, scope.myModel.longitude);

                mapOptions = {
                    center: searchLatLng,
                    zoom: 12,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                googleMap = new google.maps.Map(element[0],mapOptions);

                searchMarker = new google.maps.Marker({
                    position: searchLatLng,
                    map: googleMap,
                    draggable: true
                });

                google.maps.event.addListener(searchMarker, 'dragend', function(){
                    scope.$apply(function(){
                        scope.myModel.latitude = searchMarker.getPosition().lat();
                        scope.myModel.longitude = searchMarker.getPosition().lng();
                    });
                }.bind(this));

            };

            scope.$watch('myModel', function(value){
                var myPosition = new google.maps.LatLng(scope.myModel.latitude, scope.myModel.longitude);
                searchMarker.setPosition(myPosition);
            }, true);
        }
    }
});

但它不会改变。

【问题讨论】:

  • 请提供您的控制器代码\
  • testModel 包含什么?
  • @HardikPatel 什么都没有。我应该添加什么?
  • myModel 变量应该包含纬度和经度值。应该从 设置
  • @HardikPatel 请把它作为答案

标签: html angularjs google-maps google-maps-api-3 google-maps-markers


【解决方案1】:

myModel 变量应包含纬度和经度值。应该从

设置

&lt;map-directive ng-model="testModel"&gt;&lt;/map-directive&gt;

你需要在你的控制器中创建一个如下的对象,并将这个对象分配给上面 html 的指令。

var testModel = {
    latitude:1.2323,
    longitude:2.3434
};

【讨论】:

  • 我选择 但我收到此错误:[$parse:syntax]
  • 你可以试试这个
  • 我试过这个,但我得到了这个错误:angular.js: Error: [ngModel:nonassign] and angular.js Error: [$rootScope:infdig]
  • 你能创建一个 plunkr。
猜你喜欢
  • 2018-01-15
  • 2015-08-26
  • 2016-09-12
  • 2016-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 2015-03-28
相关资源
最近更新 更多