4
我正在为Android做两个html5/javascript应用程序和phonegap,我让他们都通过php与ajax通信,在他们两个我使用谷歌地图API,我需要知道在他们两人在那里的其他家伙,而一个正在和另一个正在等待他的话,我有2个问题:刷新谷歌地图javascript ajax
在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 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);
}
你几乎没有。发生什么事?什么没有发生? –
页面每次刷新地图时都会重新加载,现在我解决了这个问题,我使用不同的函数创建了代码,并且只更新了标记,并且它正在工作。感谢您的回复 – rmalta
很高兴听到你有它的工作。您应该将其作为答案提交,以便将来可以帮助其他人。 –