Javascript-在另一个控制器作用域方法中获取变量的值

时间:2018-11-15 01:15:55

标签: javascript

使用google maps api(地理位置)。 这是有问题的代码。

    var lat;
    var lng;
    var moveLat;
    var moveLng;
    var moveMarker;
    var marker;

    $scope.initMap = () => {

        // TODO:Privilege Check!

        var checkPrivilege = false;

        console.log("Clicked Generate Map Pin");

        var x = document.getElementById("demo");

        if (navigator.geolocation) {

            navigator.geolocation.getCurrentPosition(function (position) {

                lat = parseFloat(position.coords.latitude);
                lng = parseFloat(position.coords.longitude);
                console.log("Lat " + lat + " Lng " + lng);

                // The location of current user
                var CMU_SV = {lat: parseFloat(lat), lng: parseFloat(lng)};

                // The map, centered at current user
                var map = new google.maps.Map(
                    document.getElementById('map'), {zoom: 4, center: CMU_SV});

                map.setOptions({ minZoom: 15, maxZoom: 20 });

                // True for admin user
                if (checkPrivilege) {

                    // The marker, positioned at current user's position
                    moveMarker = new google.maps.Marker({position: CMU_SV, flat: false, map: map, draggable: true});
                }
                // False for regular user
                else {

                    //var moveMarker = new google.maps.Marker({position: CMU_SV, flat: false, map: map, draggable: true});
                    marker = new google.maps.Marker({position: CMU_SV, map: map});
                }
            });
        } else {
            x.innerHTML = "Geolocation is not supported by this browser.";
        }
    };

    $scope.sendLocationInfo = () => {

        var checkPrivilege = false;

        if (checkPrivilege) {

            moveLat = parseFloat(moveMarker.getPosition().lat);
            moveLng = parseFloat(moveMarker.getPosition().lng);

            console.log(moveLat, moveLng);
            $scope.LocationInfo = moveLat + "," + moveLng;
            $scope.hasLocationInfo = true;
        }
        else {

            lat = parseFloat(marker.getPosition().lat);
            lng = parseFloat(marker.getPosition().lat);

            console.log(lat, lng);
            $scope.LocationInfo = lat + "," + lng;
            $scope.hasLocationInfo = true;
        }

        var currentUser = $cookies.get('userName');

        $scope.message = currentUser + " has shared their location!";

        client.sendLocationInfo(currentUser, $scope.LocationInfo, $scope.hasLocationInfo)
            .then(() => {
                $scope.LocationInfo = "";
            });

        client.sendPublicWallMessage($scope.message)
            .then(() => {
                $scope.message = '';
            })
    };

如您所见,我在initMap和sendLocationInfo之外的开头初始化了6个var。

还有两个地图标记(moveMarker和marker)。如果需要,可以由管理员用户移动moveMarker。

在第二个功能中,我试图调用每个地图标记以获取纬度和经度信息-将它们分配给各自的变量,然后将这些坐标保存在我的数据库中。

但是,对于console.log(moveLat,moveLng)和console.log(lat,lng),我得到了两个NaN值,而不是所需的坐标。

有什么办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的代码正确。但是返回到parseFloat中的moveMarker.getPosition().lat的值可能是字符串或其他不能解析为浮点值的值。