我正在构建一个应用程序,它从mysql获取用户数据并在地图上显示它们, 从我的Android应用程序,我每10秒更新一次表(lat,lon),现在我想在谷歌地图(网络应用程序)更新他们的位置而不刷新地图或页面
这是我的代码:
此功能加载页面刷新
function initMap() {
var mapOptions = {
zoom: 11,
center: {lat: 33.7167, lng: 73.0667},
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
makeRequest('get_locations.php', function(data) {
var data = JSON.parse(data.responseText);
for (var i = 0; i < data.length; i++) {
//display(data[i]);
displayLocation(data[i]);
}
});
//window.setInterval(initMap, 15000);
}
makeRequest功能
function makeRequest(url, callback) {
var request;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
} else {
request = new ActiveXObject("Microsoft.XMLHTTP"); // IE6, IE5
}
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
callback(request);
}
}
request.open("GET", url, true);
request.send();
}
get_location.php
<?php
include('connection.php');
$l= array();
$result = mysqli_query($con,"SELECT * FROM users");
while ($row = mysqli_fetch_assoc($result)) {
$l[] = $row;
}
$j = json_encode($l, true);
echo $j;
//return $j;
?>
显示位置功能
function displayLocation(location) {
var contentString ='<div class=\"chapter-bubble\">'
+'<strong>Name: ' + location.username + '</strong>'+'<br/>'+'<strong>Phone:
</strong>'+location.phone_number +'</strong>'+'<br/>'+'<strong><a
href=\"sendsms.php?phone_number=' + location.phone_number + '&username=' +
location.username +'\"> Send SMS</a> </strong>'+'<br/></div>' ;
//window.alert(location.name);
var position = new google.maps.LatLng(parseFloat(location.lat),
parseFloat(location.lon));
//window.alert(position);
var image;
if (location.is_safe=="0")
{
image = new
google.maps.MarkerImage('placer/not_safe.png', null, null, null, new
google.maps.Size(40, 40));
}
else if (location.is_safe=="1")
{
image = new
google.maps.MarkerImage('placer/yes_safe.png', null, null, null, new
google.maps.Size(40, 40));
}
//window.alert(image);
var marker = new google.maps.Marker({
position: position,
map: map,
icon: image,
animation: google.maps.Animation.DROP,
title: location.username
});
var infowindow = new google.maps.InfoWindow({
content:contentString
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
我所做的是在initMap函数的末尾添加这行 window.setInterval(initMap,5000); ,但是它加载了地图,我只想更改位置而不刷新页面或地图。 对此有何解决方案?
答案 0 :(得分:1)
您需要创建一个数组并将标记添加到数组中。在更新新标记之前,需要从地图中删除标记。这可以通过使用下面给出的clearMarkers方法来完成
在js文件的顶部声明这个
var markerArray = new Array();
在js文件中添加以下方法
function clearMarkers()
{
for(var j=0, len = markerArray.length; j<len; j++)
{
markerArray[j].setMap(null);
}
markerArray = new Array();
}
将代码与initMap分开,并创建一个名为updateLocation
的新方法function updateLocation()
{
makeRequest('get_locations.php', function(data) {
clearMarkers();
var data = JSON.parse(data.responseText);
for (var i = 0; i < data.length; i++) {
//display(data[i]);
displayLocation(data[i]);
}
});
window.setInterval(updateLocation, 15000);
}
将从initMap方法
调用它function initMap() {
var mapOptions = {
zoom: 11,
center: {lat: 33.7167, lng: 73.0667},
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
updateLocation();
}
在显示位置功能中,在创建后将标记添加到数组中:
var marker = new google.maps.Marker({
position: position,
map: map,
icon: image,
animation: google.maps.Animation.DROP,
title: location.username
});
markerArray.push(marker);