我正在使用opencart进行开发http://www.goskitz.com.au/index.php?route=information/dealers
在dealers.php文件中,有一个名为query()的函数,它将从数据库中搜索邮政编码,以获得商店位置结果,并将其输出为XML格式。例如,如果我键入3000并选择Melbourne,xml输出将是:
函数query():
public function query(){
// the query coding part is removed
// Output to XML
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
foreach($result as $key => $li){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name",$li['name']);
$newnode->setAttribute("address", $li['address']);
$newnode->setAttribute("lat", $li['lat']);
$newnode->setAttribute("lng", $li['lng']);
$newnode->setAttribute("distance", $li['distance']);
}
echo $dom->saveXML();
}
}
输出
<markers><marker name="Harvey Norman BIG BUYS" address="Shop 9&11 Ground Level Springvale Homemaker Centre 917 Princes Highway Springval VIC 3171" lat="-37.927456" lng="145.143845" distance="19.74128375361586"></marker>....blahblah... <markers>
然后我发现downloadurl功能不起作用,无论我放什么url,都不会发生任何事情。为什么会发生这种情况?
任何想法都将不胜感激。
我已经将javascript粘贴到http://pastebin.com/yu0bX3vz
此处显示的代码段负责生成XML。生成的XML包含标记数据,需要对其进行解析并将其移交给Google Maps API,以便绘制标记。
你可能想做的第一件事是,让一个标记工作,然后升级到多个标记。只需尝试正确地使用XML并在地图上绘制标记即可。在尝试添加标记之前,必须初始化映射。
如果成功,请继续通过jQuery动态加载标记XML。因为您已经在使用jQuery,所以我建议放弃downloadURL()函数,而使用添加了成功函数的简单jQuery Ajax.get()请求来解析传入响应XML。
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "markers.xml", // xml source: either static xml file or xml generated by php
dataType: "xml",
success: parseXml // parse the marker xml
});
});
// the idea is: from xml -> to var "location" -> addMarker(location)
function parseXml(xml)
{
// foreach marker extract the xml data (lat,long,...)
$(xml).find("marker").each(function(index){
// and assign to a new location var
var location.lat = $(this).find('lat').text();
var location.long = $(this).find('long').text();
var location.name = $(this).find('name').text();
// then use this data to add the marker
addMarker(location);
)
}
function addMarker(location)
{
// the marker is a "point" on the map defined by it's lat/long
var point = new google.maps.LatLng(location.lat, location.lng);
var marker = new google.maps.Marker({
position:point,
map: map,
title: location.name
});
};
如果你需要绑定一个信息窗口或其他什么东西,当点击标记图标时,它会弹出,然后请在本文中搜索bindInfoWindow()https://developers.google.com/maps/articles/phpsqlajax_v3
我想,这就是你得到的这个downloadURL()函数的起源,但在本教程中,他们不像你那样使用jQuery。
这个问题可能也有帮助:通过jQuery为Google Maps加载XML API V3