【问题标题】:Google Maps Geolocation refreshes page谷歌地图地理位置刷新页面
【发布时间】:2015-11-05 17:56:27
【问题描述】:

我正在尝试创建谷歌地图。该地图应保持居中且永不移动。我正在创建实时跟踪,但我遇到了跟​​踪问题。每隔几秒钟,跟踪功能就会每隔几秒钟刷新一次页面。

    .controller('mapCtrl', function ($scope, $ionicSideMenuDelegate, $ionicLoading, $compile, $ionicPopup, $cordovaGeolocation) {

$ionicSideMenuDelegate.canDragContent(false);
var onSuccess = function(position) {


    var myLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    var CSUS_CENTER = (38.55914, -121.423473);

            var mapOptions = {
                center: CSUS_CENTER,
                zoom: 16,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                zoomControl: true,
                disableDefaultUI: true
            };
            var map = new google.maps.Map(document.getElementById("map"),
                mapOptions);

            //Marker + infowindow + angularjs compiled ng-click
            var contentString = "<div><a ng-click='clickTest()'>Click me!</a></div>";
            var compiled = $compile(contentString)($scope);

            var infowindow = new google.maps.InfoWindow({
                content: compiled[0]
            });

            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: 'Sac State'
            });

            google.maps.event.addListener(marker, 'click', function () {
                infowindow.open(map, marker);
            });

            $scope.map = map;
            $scope.map.setCenter(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
};

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}

navigator.geolocation.watchPosition(onSuccess, onError, {maximumAge: 3000, timeout: 5000, enableHighAccuracy: true });

});

所以,我的问题是地图每次刷新时都会重新居中,我希望将其保持在原始纬度和经度的中心。然后另一个问题是页面不断刷新

【问题讨论】:

    标签: javascript google-maps geolocation apache-cordova


    【解决方案1】:

    如果只需要获取一次位置,请使用getCurrentPosition,而不是watchPosition

    【讨论】:

    • 将 watchPoistion 更改为 getCurrentPosition 确实会阻止它刷新,但我需要它来有争议地跟踪我
    猜你喜欢
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多