1

我知道,已经有很多关于“商店定位器”的博客,但我找不到答案。我使用了来自https://developers.google.com/maps/articles/phpsqlsearch_v3的教程。事情不正常,那么有人对那个教程让商店定位器工作有问题吗?我的问题是,没有生成有效的 xml 文件。我已经更改了 xml 输出,因为原来的那个根本不起作用。以下是这两个文件的代码:

标题##1。http://umwelt-und-information.com/maps/ASPE_Adressen_kodieren_2a.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>Example</title>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"
        type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var map;
var markers = [];
var infoWindow;
var locationSelect;

function initialize() {
  map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(52, 8),
    zoom: 8,
    mapTypeId: 'roadmap',
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
  });
  infoWindow = new google.maps.InfoWindow();

locationSelect = document.getElementById("locationSelect");
  locationSelect.onchange = function() {
    var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
    if (markerNum != "none"){
      google.maps.event.trigger(markers[markerNum], 'click');
    }
  };
}

function searchLocations() {
 var address = document.getElementById("addressInput").value;
 var geocoder = new google.maps.Geocoder();
 geocoder.geocode({address: address}, function(results, status) {
   if (status == google.maps.GeocoderStatus.OK) {
    searchLocationsNear(results[0].geometry.location);
   } else {
     alert(address + ' not found');
   }
 });
}

function clearLocations() {
 infoWindow.close();
 for (var i = 0; i < markers.length; i++) {
   markers[i].setMap(null);
 }
 markers.length = 0;

 locationSelect.innerHTML = "";
 var option = document.createElement("option");
 option.value = "none";
 option.innerHTML = "See all results:";
 locationSelect.appendChild(option);
}

function searchLocationsNear(center) {
 clearLocations();

 var radius = document.getElementById('radiusSelect').value;
 var searchUrl = 'ASPE_Adressen_kodieren_3a.php?lat=' + center.lat() + '&lng=' +  center.lng() + '&radius=' + radius;
 downloadUrl(searchUrl, function(data) {
   var xml = parseXml(data);
   var markerNodes = xml.documentElement.getElementsByTagName("marker");
   var bounds = new google.maps.LatLngBounds();
   for (var i = 0; i < markerNodes.length; i++) {
     var nachname = markerNodes[i].getAttribute("Nachname");
     var strasse = markerNodes[i].getAttribute("strasse");
     var distance = parseFloat(markerNodes[i].getAttribute("distance"));
     var latlng = new google.maps.LatLng(
          parseFloat(markerNodes[i].getAttribute("lat")),
          parseFloat(markerNodes[i].getAttribute("lng")));

     createOption(nachname, distance, i);
     createMarker(latlng, nachname, strasse);
     bounds.extend(latlng);
   }
   map.fitBounds(bounds);
   locationSelect.style.visibility = "visible";
   locationSelect.onchange = function() {
     var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
     google.maps.event.trigger(markers[markerNum], 'click');
   };
  });
}

function createMarker(latlng, nachname, strasse) {
  var html = "<b>" + nachname + "</b> <br/>" + strasse;
  var marker = new google.maps.Marker({
    map: map,
    position: latlng
  });
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
  markers.push(marker);
}

function createOption(name, distance, num) {
  var option = document.createElement("option");
  option.value = num;
  option.innerHTML = nachname + "(" + distance.toFixed(1) + ")";
  locationSelect.appendChild(option);
}

function downloadUrl(url, callback) {
  var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request.responseText, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

function parseXml(str) {
  if (window.ActiveXObject) {
    var doc = new ActiveXObject('Microsoft.XMLDOM');
    doc.loadXML(str);
    return doc;
  } else if (window.DOMParser) {
    return (new DOMParser).parseFromString(str, 'text/xml');
  }
}

function doNothing() {}

//]]>
</script>
</head>

<body style="margin:0px; padding:0px;" onload="initialize()">
<div>
 <input type="text" id="addressInput" size="10"/>
<select id="radiusSelect">
  <option value="25" selected>25mi</option>
  <option value="100">100mi</option>
  <option value="200">200mi</option>
</select>

<input type="button" onclick="searchLocations()" value="Search"/>
</div>
<div><select id="locationSelect" style="width:100%;visibility:hidden"></select></div>
<div id="map" style="width: 100%; height: 80%"></div>
</body>
</html>

标题 ## 2. http://umwelt-und-information.com/maps/ASPE_Adressen_kodieren_3a.php

<?php
error_reporting(E_ALL); 
require("dbpass.php");
if (PHP_VERSION>='5')
require_once('domxml-php4-to-php5.php');
// Get parameters from URL
$center_lat = $_GET['lat'];
$center_lng = $_GET['lng'];
$radius = $_GET['radius'];

// Start XML file, create parent node
$doc = domxml_new_doc("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);

$connection=mysql_connect ($dbhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}

// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}

// Search the rows in the ASPE table
$query = sprintf("SELECT strasse, nachname, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM ASPE HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));
$result = mysql_query($query);

$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}

//header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
$node = $dom->create_element("marker");
$newnode = $parnode->append_child($node);
$newnode->set_attribute("nachname", utf8_encode ($row['nachname']));
$newnode->set_attribute("strasse", utf8_encode ($row['strasse']));
$newnode->set_attribute("lat", $row['lat']);
$newnode->set_attribute("lng", $row['lng']);
$newnode->set_attribute("radius", $row['radius']);
}
//echo $dom->saveXML();

$xmlfile = $doc->dump_file("codierung.xml");
?>

这可能是正确的,但是:它将我发送到 lat/lng 0.000000/0.000000(南太平洋),这可能意味着地理编码无法正常工作。但此外,不会在 xml 中填充任何数据集,即使在 URL 中添加 lat、lng 和半径(例如http://umwelt-und-information.com/maps/ASPE_Adressen_kodieren_3a.php?lat=56.45&lng=6.45&radius =25 )。

有人可以验证代码吗?或者任何人都可以向我展示一个使用该商店定位器的工作示例,如果可能的话,使用脚本代码?

提前谢谢了!:-)

4

2 回答 2

0

考虑使用国际字符格式 ASCII 而不是 header("Content-type: text/xml"); 把这个头(“内容类型:文本/xml字符utf-8”);当您仅运行第二个文件并将值分配给 lat、lng 和 radius 时,将生成 xml 文件

于 2014-03-02T06:59:20.080 回答
0

您的网址显示无效查询:“字段列表”中的未知列“地址”如果所有节点都等同于字段名称,则应生成 XML 桌子

您的表格是这样的还是您更改了字段名称?我有一个基于教程的演示。它基于英国(尝试伦敦),但如果尝试英国以外的地址(尝试巴黎),则默认安全。

如果没有找到记录,地图将默认为 0,0(中太平洋)。要默认到该位置,请使用以下代码。

if (mysql_num_rows($result)==0) {
    $node = $dom->createElement("marker");
    $newnode = $parnode->appendChild($node);
    $newnode->setAttribute("nachname", "No Records Found");
    $newnode->setAttribute("lat", $center_lat);
    $newnode->setAttribute("lng", $center_lng);
    }
else {
    Iterate through the rows, adding XML nodes for each
    while ($row = @mysql_fetch_assoc($result)){
    $node = $dom->create_element("marker");
    $newnode = $parnode->append_child($node);
    $newnode->set_attribute("nachname", utf8_encode ($row['nachname']));
    $newnode->set_attribute("strasse", utf8_encode ($row['strasse']));
    $newnode->set_attribute("lat", $row['lat']);    
    $newnode->set_attribute("lng", $row['lng']);
    $newnode->set_attribute("radius", $row['radius']);
   }
于 2012-09-21T20:40:01.347 回答