如何延迟watchPosition功能

时间:2013-12-29 18:48:29

标签: javascript jquery html5

我是javascript和地理定位的新手,每当用户访问我的页面时,我都会显示用户的实时位置,但我想将每次更新延迟30秒或更长时间。有没有我可以调用的函数或者如何创建一个可以实现此功能的函数?我尝试使用window.setTimeout,但无法使其工作。

<!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" href="map.css"/>

<!--<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /-->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?sensor=true">
</script>

<script type="text/javascript">
    var map;

    function initialize(location){
        console.log(location);

        //Assigns coordinates to the variable.
        var currentLocation = new google.maps.LatLng(location.coords.latitude, location.coords.longitude);

        //Displays the map on the canvas.                      
        var mapOptions = {
            //center: new google.maps.LatLng(25.753981, -80.375633),
            center: currentLocation,//Displays the location 
            zoom: 15,//How far or close
            mapTypeId: google.maps.MapTypeId.ROADMAP//Type of map
        };

        //Objects of this class define a single map on a page.
        map = new google.maps.Map(document.getElementById("map-canvas"),mapOptions);

        //Identifies user's location and tdisplays pin on a map.
        var marker = new google.maps.Marker({

            position: currentLocation,//Map location displayed
            //position: new google.maps.LatLng(25.753981, -80.375633),
            map: map,
            //icon : 'map_icons/person.png'//Set a custom icon
        });



    }
    //Jquery function that waits until the html is fully loaded to then execute pending JavaScript code.
    $(document).ready(function(){
        //navigator.geolocation.getCurrentPosition(initialize);
        //Constanly checks for cordinate changes and updates them
        navigator.geolocation.watchPosition(initialize);


        });
</script>
</head>


<body>
    <div id="map-canvas">

    </div>


</body>
</html>

0 个答案:

没有答案