1

嗨,我正在尝试使用 Google Maps API 创建一个网站并从 MySQL db 获取坐标。到目前为止一切顺利,但仅限于 Chrome 或 Safari标记不会显示在 Internet Explorer中。我可以使用一些帮助。我遵循了 Google https://developers.google.com/maps/articles/phpsqlajax_v3的本指南 无论如何,这是我的代码。任何帮助将不胜感激,谢谢。

var customIcons = {
  sport: {
    icon: 'images/markers/sport.png',
  },
  bar: {
    icon: 'images/markers/bar.png',
  },
  poi: {
    icon: 'images/markers/poi.png',
  },
  skola: {
    icon: 'images/markers/skola.png',
  },
  kino: {
    icon: 'images/markers/kino.png',
  },
  divadlo: {
    icon: 'images/markers/divadlo.png',
  }
};

function nactimapu() {
  var map = new google.maps.Map(document.getElementById("mapa"), {
    center: new google.maps.LatLng(49.068299, 17.460907),
    zoom: 15,
    mapTypeId: 'hybrid'
  });
  var infoWindow = new google.maps.InfoWindow;

  // Change this depending on the name of your PHP file
  downloadUrl("querymapa.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var nazev = markers[i].getAttribute("nazev");
      var popis = markers[i].getAttribute("popis");
      var typ = markers[i].getAttribute("typ");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = "<b>" + nazev + "</b> <br/>" + popis;
      var icon = customIcons[typ] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        animation: google.maps.Animation.DROP,
        icon: icon.icon
      });
      bindInfoWindow(marker, map, infoWindow, html);
    }
  });
}

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}

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, request.status);
    }
  };

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

function doNothing() {}

这是php中的xml输出,可能有错误。对不起,我没有早点发布。

<?php  

require("dbconfig.php"); 

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr); 
return $xmlStr; 
} 

// Opens a connection to a MySQL server
$connection=mysql_connect ($dbserver, $dbuser, $dbheslo);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

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

// Select all the rows in the markers table
$query = "SELECT * FROM lokace WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

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

// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'nazev="' . parseToXML($row['nazev']) . '" ';
  echo 'popis="' . parseToXML($row['popis']) . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'typ="' . $row['typ'] . '" ';
  echo '/>';


// End XML file
echo '</markers>';

?>
4

2 回答 2

2

您的数组中有额外的逗号:

sport: {
     icon: 'images/markers/sport.png',  <-- Right here (among other places)
  }

Internet Explorer 不喜欢这样,而其他浏览器则不介意。您可以安全地删除它们。

于 2012-11-22T15:11:05.560 回答
0
  sport: {icon: 'images/markers/sport.png',},

Explorer 期望(其他浏览器忽略这一点)您向其中添加另一个命令,例如纬度或经度坐标以及标记中指定的其他项目。删除右括号前的逗号,你应该没问题

例如,我在下面发布了我自己工作的标记,所以你可以看到,当我关闭括号时,我不再使用逗号

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(data.latitude, data.longitude),
    map: yay,
    title: title,
    icon: icon
});  
于 2012-11-22T15:15:51.670 回答