简体   繁体   中英

Google Maps Markers not showing in IE

Hi, I'm trying to make a website with Google Maps API and get coords from MySQL db . So far so good, but only in Chrome or Safari . Markers won't show in Internet Explorer . I could use some help. I followed this guide from Google https://developers.google.com/maps/articles/phpsqlajax_v3 Anyway, here's my code. Any help would be appreciated, thanks.

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() {}

Here is xml output in php, maybe there is a mistake. Sorry that I didn't post it earlier.

<?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>';

?>

You have extra commas in your arrays:

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

Internet Explorer doesn't like that, while other browsers don't mind them. You can safely remove them.

  sport: {icon: 'images/markers/sport.png',},

Explorer expects (other browsers ignore this) that you add another command to this, such as latitude - or longitude coordinates and other items specified in your marker. Remove the comma before the closing bracket and you should be fine

as example, i posted below here a marker from my own work, so you can see, when i close the bracket, i dont use a comma anymore

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(data.latitude, data.longitude),
    map: yay,
    title: title,
    icon: icon
});  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM