问题描述
我是googlemaps API的新手。我为我的手机写了一个小应用程序,它定期将其位置更新为SQL数据库。
我想在浏览器的googlemap上显示此信息。理想情况下,我想定期轮询数据库,如果有新的合作伙伴到达,请将它们添加到该行。
描述它的最佳方法是。
任何指针?我在哪里出错?
您似乎在正确的轨道上。 $ b
你的PHP脚本应该接受一个时间戳参数,并且应该检查在该时间戳之后数据库中是否插入了新的点。如果是,它应该返回最新条目的回复(或者在该时间戳之后的条目列表,如果您想在车辆移动时显示实时跟踪)。
在客户端,您可能想要向服务器端脚本启动AJAX请求,可以使用普通或:
var lastUpdate ='2000/01/01 00:00:00';
函数autoUpdate(){
$ .ajax({
类型:GET,
url:phpsqlajax_genxml.php?last_update =+ lastUpdate,
dataType:'xml',
success:function(xmlData){
// 1.检查xmlData是否为空,如果没有,我们会收到
// some新数据
// 2.使用时间戳从xmlData更新lastUpdate从
//服务器不要使用JavaScript更新时间戳
//因为客户端和服务器上的
//不会完全同步
// 3.在Google Map上移动标记
//重新启动autoUpdate()函数5秒
setTimeout(autoUpdate,5000);
}
});
}
I am new to the googlemaps API. I have written a small app for my mobile phone that periodically updates its location to an SQL databse.
I would like to display this information on a googlemap in my browser. Ideally i'd like to then poll the database periodically and if any new co-ords have arrived, add them to the line.
Best way of describing it is this.
In a quest to get to there, i've started on the documents on google and been modifying them to try and acheive what I want. It doesn't work - and i don't know enough to know why. I would love some advice as to why, and any pointers towards my ultimate goal would be very much welcomed. Google Maps AJAX + MySQL/PHP Example
<script type="text/javascript">
//<![CDATA[
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(47.614495, -122.341861), 13);
GDownloadUrl("phpsqlajax_genxml.php", function(data) {
var xml = GXml.parse(data);
var line = [];
var markers = xml.documentElement.getElementsByTagName("points");
for (var i = 0; i < points.length; i++) {
var point = points.item(i);
var lat = point.getAttribute("lat");
var lng = point.getAttribute("lng");
var latlng = new GLatLng(lat, lng);
line.push(latlng);
if (point.firstChild) {
var station = point.firstChild.nodeValue;
var marker = createMarker(latlng, station);
map.addOverlay(marker);
}
}
var polyline = new GPolyline(line, "#ff0000", 3, 1);
map.addOverlay(polyline);
});
}
//]]>
My php file is generating the following XML;
<?xml version="1.0" encoding="UTF-8" ?>
<points>
<point lng="-122.340141" lat="47.608940"/>
<point lng="-122.344391" lat="47.613590"/>
<point lng="-122.356445" lat="47.624561"/>
<point lng="-122.337654" lat="47.606365"/>
<point lng="-122.345673" lat="47.612823"/>
<point lng="-122.340363" lat="47.605961"/>
<point lng="-122.345467" lat="47.613976"/>
<point lng="-122.326584" lat="47.617214"/>
<point lng="-122.342834" lat="47.610126"/>
</points>
I have successfully worked through this; http://code.google.com/apis/maps/articles/phpsqlajax.html before attempting to customise the code.
Any pointers? Where am I go wrong?
You seem to be on the right track.
Your php script should accept a timestamp parameter, and should check if new points have been inserted in the database after that timestamp. If yes, it should return a response with the latest entry (or a list of entries after that timestamp, if you want to show a live trail as the vehicle moves).
On the client-side, you may want to initiate an AJAX request to the server-side script, either using normal or long polling, with the timestamp parameter of the last update.
When your AJAX request receives new information from the server, you would simply move your markers on the map. Then initiate a new AJAX request with the updated timestamp paramater.
Pseudocode-ish example using jQuery:
var lastUpdate = '2000/01/01 00:00:00';
function autoUpdate () {
$.ajax({
type: "GET",
url: "phpsqlajax_genxml.php?last_update=" + lastUpdate,
dataType: 'xml',
success: function(xmlData) {
// 1. Check if the xmlData is empty. If not we received
// some fresh data.
// 2. Update lastUpdate from the xmlData with the timestamp from
// the server. Don't use JavaScript to update the timestamp,
// because the time on the client and on the server will
// never be exactly in sync.
// 3. Move the markers on Google Map.
// Relaunch the autoUpdate() function in 5 seconds.
setTimeout(autoUpdate, 5000);
}
});
}
这篇关于使用php / mysql在Google地图上动态绘制多段线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!