刷新谷歌地图javascript ajax

时间:2013-07-04 16:08:45

标签: javascript ajax google-maps cordova

我正在为android安装两个带有phonegap的html5 / javascript应用程序,我让它们都通过php与ajax进行通信,在这两个应用程序中我使用谷歌地图api,我需要知道它们在哪里其他人是,当一个人在移动而另一个在等他时,我有两个问题:

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

  2. 我正在通过ajax加载,两个应用程序的mysql数据库中保存的坐标,所以我需要在每个地图中设置第二个标记,作为其他应用程序位置,并每隔10秒跟踪它的移动

  3. 这就是我在每个应用中加载地图的方式:

      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 POST获取其他应用的位置:

    $.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);
    }
    

1 个答案:

答案 0 :(得分:2)

为了更容易找到,这里是问题的解决方案。

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

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);
}