Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
刷新google地图javascript ajax_Javascript_Ajax_Google Maps_Cordova - Fatal编程技术网

刷新google地图javascript ajax

刷新google地图javascript ajax,javascript,ajax,google-maps,cordova,Javascript,Ajax,Google Maps,Cordova,我正在使用phonegap for android开发两个html5/javascript应用程序,它们都通过php与ajax进行通信,在这两个应用程序中,我都使用google maps api,我需要知道另一个家伙在哪里,一个在移动,另一个在等他,然后我有两个问题: 在app1(正在移动的那个)中,我需要在地图上每隔10秒刷新一次标记,而不加载整个页面 我正在通过ajax加载,这两个应用程序的坐标都保存在mysql数据库中,因此我需要在每个地图中设置第二个标记,作为其他应用程序的位置,并每10

我正在使用phonegap for android开发两个html5/javascript应用程序,它们都通过php与ajax进行通信,在这两个应用程序中,我都使用google maps api,我需要知道另一个家伙在哪里,一个在移动,另一个在等他,然后我有两个问题:

  • 在app1(正在移动的那个)中,我需要在地图上每隔10秒刷新一次标记,而不加载整个页面

  • 我正在通过ajax加载,这两个应用程序的坐标都保存在mysql数据库中,因此我需要在每个地图中设置第二个标记,作为其他应用程序的位置,并每10秒跟踪一次移动

  • 以下是我在每个应用程序中加载地图的方式:

      function getPos() {
             navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy:true});
             setTimeout(getPos, 10000); //call function after 10 seconds
     }
    
     function onSuccess(position) {
        lat = position.coords.latitude;
        lon = position.coords.longitude;
        console.log("Found - LAT: ", lat, "LON: ", lon);
    
        var currentposition = new google.maps.LatLng(lat,lon);
        var mapoptions = {
            zoom: 16,
            center: currentposition,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            icon: image
        };
        var map = new google.maps.Map(document.getElementById("map"), mapoptions);
        var marker = new google.maps.Marker({
            position: currentposition,
            map: map
        });
        save_position_in_bd();
    }
    
    function onError(error) {
       console.log('code: '    + error.code, 'message: ' + error.message);
    }
    
    我通过一篇ajax帖子获得了另一个应用程序的位置:

    $.ajax({ 
       type: "POST", 
       url: "http://localhost/call.php", 
       data: "name=rui",
       dataType: 'json',
       success: function(data){
         lat = data.lat;//get other apps lat
         lon = data.lon;//get other apps lon
       },
       error: function(jqXHR, textStatus, errorThrown ){
             console.log("POST: ", jqXHR, textStatus, errorThrown);
       }
     });
    
    我能做些什么来解决我的问题?我尝试用不同的函数刷新地图,并尝试只加载标记,但不起作用。 此外,我使用的确切api是:

    http://maps.googleapis.com/maps/api/js?sensor=false 
    
    解决它: 答案是将代码拆分为不同的函数,并仅调用标记,以便对其进行更新:

    function getPos() {//initial function to read the position
             estado = "livre";
             navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy:true});
             setTimeout(keep_alive, 10000); //read every 10 seconds
     }
    
     function onSuccess(position) {//read map and mark it in the map
        lat = position.coords.latitude;
        lon = position.coords.longitude;
        console.log("Found - LAT: ", lat, "LON: ", lon);
    
        var image = '/img/taxi_green.png';
        var mapoptions = {
            zoom: 16,
            center: new google.maps.LatLng(lat,lon),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            icon: image
        };
        map = new google.maps.Map(document.getElementById("map"), mapoptions);
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat,lon),
            map: map
        });
        save_position();
    }
    
    function keep_alive() {//read position and mark it in the map
       navigator.geolocation.getCurrentPosition(onRefresh, onError, {enableHighAccuracy:true});
       save_position();
       setTimeout(keep_alive, 10000); //read every 10 seconds   
    }
    
    //refresh only the marker
    function onRefresh(position) {
       lat = position.coords.latitude;
       lon = position.coords.longitude;
    
       console.log("Found - LAT: ", lat, "LON: ", lon);
    
       marker.setPosition(new google.maps.LatLng(lat, lon));//refresh marker
       map.setCenter(new google.maps.LatLng(lat, lon));//resfresh center of the map
    }
    
    function trace_client() {//mark clients position in the map
       //clientMarker.setPosition(new google.maps.LatLng(client_lat, client_lon));
       clientMarker = new google.maps.Marker({
            position: new google.maps.LatLng(client_lat,client_lon),
            map: map
        });
       console.log("client marked in the map");
    }
    
    function onError(error) {
       console.log('code: '    + error.code, 'message: ' + error.message);
    }
    
    function getPos() {//initial function to read the position
             estado = "livre";
             navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy:true});
             setTimeout(keep_alive, 10000); //read every 10 seconds
     }
    
     function onSuccess(position) {//read map and mark it in the map
        lat = position.coords.latitude;
        lon = position.coords.longitude;
        console.log("Found - LAT: ", lat, "LON: ", lon);
    
        var image = '/img/taxi_green.png';
        var mapoptions = {
            zoom: 16,
            center: new google.maps.LatLng(lat,lon),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            icon: image
        };
        map = new google.maps.Map(document.getElementById("map"), mapoptions);
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat,lon),
            map: map
        });
        save_position();
    }
    
    function keep_alive() {//read position and mark it in the map
       navigator.geolocation.getCurrentPosition(onRefresh, onError, {enableHighAccuracy:true});
       save_position();
       setTimeout(keep_alive, 10000); //read every 10 seconds   
    }
    
    //refresh only the marker
    function onRefresh(position) {
       lat = position.coords.latitude;
       lon = position.coords.longitude;
    
       console.log("Found - LAT: ", lat, "LON: ", lon);
    
       marker.setPosition(new google.maps.LatLng(lat, lon));//refresh marker
       map.setCenter(new google.maps.LatLng(lat, lon));//resfresh center of the map
    }
    
    function trace_client() {//mark clients position in the map
       //clientMarker.setPosition(new google.maps.LatLng(client_lat, client_lon));
       clientMarker = new google.maps.Marker({
            position: new google.maps.LatLng(client_lat,client_lon),
            map: map
        });
       console.log("client marked in the map");
    }
    
    function onError(error) {
       console.log('code: '    + error.code, 'message: ' + error.message);
    }
    

    为了更容易找到,以下是问题的解决方案

    答案是将代码拆分为不同的函数,并仅调用标记,以便对其进行更新:

    function getPos() {//initial function to read the position
             estado = "livre";
             navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy:true});
             setTimeout(keep_alive, 10000); //read every 10 seconds
     }
    
     function onSuccess(position) {//read map and mark it in the map
        lat = position.coords.latitude;
        lon = position.coords.longitude;
        console.log("Found - LAT: ", lat, "LON: ", lon);
    
        var image = '/img/taxi_green.png';
        var mapoptions = {
            zoom: 16,
            center: new google.maps.LatLng(lat,lon),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            icon: image
        };
        map = new google.maps.Map(document.getElementById("map"), mapoptions);
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat,lon),
            map: map
        });
        save_position();
    }
    
    function keep_alive() {//read position and mark it in the map
       navigator.geolocation.getCurrentPosition(onRefresh, onError, {enableHighAccuracy:true});
       save_position();
       setTimeout(keep_alive, 10000); //read every 10 seconds   
    }
    
    //refresh only the marker
    function onRefresh(position) {
       lat = position.coords.latitude;
       lon = position.coords.longitude;
    
       console.log("Found - LAT: ", lat, "LON: ", lon);
    
       marker.setPosition(new google.maps.LatLng(lat, lon));//refresh marker
       map.setCenter(new google.maps.LatLng(lat, lon));//resfresh center of the map
    }
    
    function trace_client() {//mark clients position in the map
       //clientMarker.setPosition(new google.maps.LatLng(client_lat, client_lon));
       clientMarker = new google.maps.Marker({
            position: new google.maps.LatLng(client_lat,client_lon),
            map: map
        });
       console.log("client marked in the map");
    }
    
    function onError(error) {
       console.log('code: '    + error.code, 'message: ' + error.message);
    }
    
    function getPos() {//initial function to read the position
             estado = "livre";
             navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy:true});
             setTimeout(keep_alive, 10000); //read every 10 seconds
     }
    
     function onSuccess(position) {//read map and mark it in the map
        lat = position.coords.latitude;
        lon = position.coords.longitude;
        console.log("Found - LAT: ", lat, "LON: ", lon);
    
        var image = '/img/taxi_green.png';
        var mapoptions = {
            zoom: 16,
            center: new google.maps.LatLng(lat,lon),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            icon: image
        };
        map = new google.maps.Map(document.getElementById("map"), mapoptions);
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat,lon),
            map: map
        });
        save_position();
    }
    
    function keep_alive() {//read position and mark it in the map
       navigator.geolocation.getCurrentPosition(onRefresh, onError, {enableHighAccuracy:true});
       save_position();
       setTimeout(keep_alive, 10000); //read every 10 seconds   
    }
    
    //refresh only the marker
    function onRefresh(position) {
       lat = position.coords.latitude;
       lon = position.coords.longitude;
    
       console.log("Found - LAT: ", lat, "LON: ", lon);
    
       marker.setPosition(new google.maps.LatLng(lat, lon));//refresh marker
       map.setCenter(new google.maps.LatLng(lat, lon));//resfresh center of the map
    }
    
    function trace_client() {//mark clients position in the map
       //clientMarker.setPosition(new google.maps.LatLng(client_lat, client_lon));
       clientMarker = new google.maps.Marker({
            position: new google.maps.LatLng(client_lat,client_lon),
            map: map
        });
       console.log("client marked in the map");
    }
    
    function onError(error) {
       console.log('code: '    + error.code, 'message: ' + error.message);
    }
    

    你快到了。发生了什么事?没有发生什么?每次我刷新地图时,页面都在重新加载,我现在解决了它,我用不同的函数编写了代码,其中一个只用于更新标记,并且它工作正常。谢谢你的回复,很高兴听到你的回复。你应该把它作为一个答案提交,这样将来它可能会帮助别人。